> I tried the code to check error you gave me and it appears that there's an error in writing and reading,but sadly i really don;t know how to solve this.
Well post the error message(s) that do get printed.
In fact, use this
What happens in Cutecom if you set the char delay (bottom right) to zero?Code: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(fd,"\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); }
At 19200 baud, it takes ~500uS to transmit each char, so a 1mS delay is quite significant (something your C code isn't doing).
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.
I changed the ms delay to 0 or any number and it still works. Anyway, how to make the char delay in C?
I also got this when running the code with additional of your code:
error: write: Bad file descriptor
error in write, result = -1
error: read: Resource temporarily unavailable
error in read, result = -1
p/s: When the device respond, it will send back this "\0xf5\0x01\0x00\0x00\0x00\0x00\0x01\0xf5" to me then only i read.
Last edited by Marvin Gorres; 04-14-2013 at 08:22 AM.
Then you should change
toCode:if (fd == -1) { /* Could not open the port */ fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n", strerror(errno)); }
KurtCode:if (fd < 0) { /* Could not open the port */ fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 - %s\n", strerror(errno)); }
Looking at the picture of your Cutecom window it appears you may be running this program as root, looking at the log location. Do you need "root" access to access your USB port?
Jim
Are you running your program as root?
You may want to consider changing you user settings to allow using the ttyUSB port as non-root. Try adding the user to the dialout group.
Jim
"...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
Running your code as root is a dangerous thing to do. You really should change your settings to allow non-root access to the port.
Jim
I found this link when searching Google for "Ubuntu USB access non-root", Since I don't run Ubuntu I don't really know what one of the answers will solve the problem. But on my system (Mint) there is a GUI program (system settings) that allows me to edit user profiles to add the dialup group to the user.
Jim
So what is fd in the following snippet?
The port file descriptor looks to be be mainfd, not fd.Code:mainfd = open_port(); write(fd,"\xF5\x01\x00\x01\x03\x00\x03\xF5",8);
Jim