"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Please repost your current code.
Jim
Having learnt a lesson, turn on as many warnings as you can.Code:$ gcc -Wall foo.c foo.c:29:6: warning: return type of ‘main’ is not ‘int’ [-Wmain] foo.c: In function ‘main’: foo.c:57:7: warning: ‘fd’ is used uninitialized in this function [-Wuninitialized]
> Another prob is, suppose i get this output from the device "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" but unfortunately not. Here what i get: Got �.
You need to do things like
printf("%02x\n", ch );
Printing binary data with %c will display random glyphs.
You also need to read in multiple chars, not just one.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Here it is
Code:#include <stdio.h> /* Standard input/output definitions */ #include <string.h> /* String function definitions */ #include <unistd.h> /* UNIX standard function definitions */ #include <fcntl.h> /* File control definitions */ #include <errno.h> /* Error number definitions */ #include <termios.h> /* POSIX terminal control definitions */ /* * 'open_port()' - Open serial port 1. * * Returns the file descriptor on success or -1 on error. */ int open_port(void) { int fd; /* File descriptor for the port */ fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { /* Could not open the port */ fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n", strerror(errno)); } return (fd); } void main() { int mainfd=0,fd; /* File descriptor */ char chout, result, cnt; struct termios options; mainfd = open_port(); fcntl(mainfd, F_SETFL, FNDELAY); /* Configure port reading */ /* Get the current options for the port */ tcgetattr(mainfd, &options); cfsetispeed(&options, B19200); /* Set the baud rates to 19200 */ cfsetospeed(&options, B19200); /* Enable the receiver and set local mode */ options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */ options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; /* Select 8 data bits */ options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */ /* Enable data to be processed as raw input */ options.c_lflag &= ~(ICANON | ECHO | ISIG); /* Set the new options for the port */ tcsetattr(mainfd, TCSANOW, &options); result = tcsetattr(mainfd, TCSANOW, &options); if ( result == -1 ) { perror("error: tcsetattr"); } if ( result != 0 ) { fprintf(stderr,"error in tcsetattr, result = %d\n", result); } result = write(mainfd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8); if ( result == -1 ) { perror("error: write"); } if ( result != 8 ) { fprintf(stderr,"error in write, result = %d\n", result); } usleep(5000000); /* wait for 5 second*/ result = read(mainfd, &chout, sizeof(chout)); /* Read character from ABU (Auto buffering Unit) */ if ( result == -1 ) { perror("error: read"); } if ( result != 1 ) { fprintf(stderr,"error in read, result = %d\n", result); } if (chout != 0) printf("Got %c.\n", chout); chout=0; /* Close the serial port */ close(mainfd); }
Your only reading a single signed character, not a string. Also 0xF5 is outside the size that can be held in a signed character, you should be using an unsigned char not a char.Another prob is, suppose i get this output from the device "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" but unfortunately not. Here what i get: Got �.
If you want to read more than this single character I recommend a loop.
Jim
Yep. But you can start by receiving and printing each character as you receive it.u mean like looping while storing them into an unsigned char array?
You've already done harder, give it a try and if you have problems post your new code and ask specific questions.urm, my C prog not really good, mind giving me an example?
Jim
Hey Jim,i did the unsigned array and it does display the CORRECT data. ^^, Thnx to you bro. Any further question i'll update here. Thanks alot man and also all other member. peace.
Code:#include <stdio.h> /* Standard input/output definitions */ #include <string.h> /* String function definitions */ #include <unistd.h> /* UNIX standard function definitions */ #include <fcntl.h> /* File control definitions */ #include <errno.h> /* Error number definitions */ #include <termios.h> /* POSIX terminal control definitions */ /* * 'open_port()' - Open serial port 1. * * Returns the file descriptor on success or -1 on error. */ int open_port(void) { int fd; /* File descriptor for the port */ fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { /* Could not open the port */ fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n", strerror(errno)); } return (fd); } void main() { int mainfd=0; /* File descriptor */ char cnt, result2; unsigned char result[8],chout[8]; int i; struct termios options; mainfd = open_port(); fcntl(mainfd, F_SETFL, FNDELAY); /* Configure port reading */ /* Get the current options for the port */ tcgetattr(mainfd, &options); cfsetispeed(&options, B19200); /* Set the baud rates to 19200 */ cfsetospeed(&options, B19200); /* Enable the receiver and set local mode */ options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; /* Mask the character size to 8 bits, no parity */ options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; /* Select 8 data bits */ options.c_cflag &= ~CRTSCTS; /* Disable hardware flow control */ /* Enable data to be processed as raw input */ options.c_lflag &= ~(ICANON | ECHO | ISIG); /* Set the new options for the port */ tcsetattr(mainfd, TCSANOW, &options); result2 = tcsetattr(mainfd, TCSANOW, &options); if ( result2 == -1 ) { perror("error: tcsetattr"); } if ( result2 != 0 ) { fprintf(stderr,"error in tcsetattr, result = %d\n", result2); } result2 = write(mainfd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8); if ( result2 == -1 ) { perror("error: write"); } if ( result2 != 8 ) { fprintf(stderr,"error in write, result = %d\n", result2); } usleep(3000000); /* wait for 5 second*/ for(i=0;i<8;i++) { result[i] = read(mainfd, &chout[i], sizeof(chout)); /* Read character from ABU (Auto buffering Unit) */ if ( result[i] == -1 ) { perror("error: read"); } if ( result[i] != 1 ) { fprintf(stderr,"error in read, result = %d\n", result[i]); } if (chout[i] != 0) printf("%02x\n",chout[i]); } /* Close the serial port */ close(mainfd); }
Last edited by Marvin Gorres; 04-14-2013 at 12:14 PM.
Why are you using an array for the return values? A single value is all you need. And it should not be a char or unsigned char. You would be better off with an int.
JimCode:result[i] = read(mainfd, &chout[i], sizeof(chout)); /* Read character from ABU (Auto buffering Unit) */ if ( result[i] == -1 ) { perror("error: read"); }
Hello, i got a problem now, i need to receive data more than 8 byte which is 111 byte and i tried to loop it 111 times the same thing i did to receive the 111 byte data but i did not receive the correct data. I've uploaded a picture of the 111 byte data which i get by using cutecom software.
Really need help on this part.