Results 1 to 3 of 3
I am trying to put some c code together to "talk" to a USB-CDC tty device. When the device is connect to my pc it appears as /dev/ttyACM0.
I then ...
- 10-10-2011 #1Just Joined!
- Join Date
- May 2006
- Posts
- 13
C program tty communication USB-CDC linux
I am trying to put some c code together to "talk" to a USB-CDC tty device. When the device is connect to my pc it appears as /dev/ttyACM0.
I then connect to the device as follows.
I then check if that worked.....Code://open link to board. int fd; fd = open(sComPort, O_RDWR | O_NOCTTY | O_NONBLOCK);
I then send the board my binary string packet (7 bytes)...Code:if (fd>0){ .....my code here }
The on-board components react as expected... All good so farCode:write(fd, "\x04\x14\x80\x00\x00\x68\x0f",7)

The part I am having problems with is reading back the response from the board. According to the supplied docs whenever a command as above is sent it is supposed to respond with a string of 7 bytes starting with 51h.
Immediately after I send the above seven bytes i do....
But rd is always -1 and the printed buff appears empty! So changed it to...Code:char buff[7]; int rd; rd=read(fd, buff, 7); printf(" Bytes received are %d \n",rd); printf("%s\n",buff);
A bit better as there are values but they vary from run to run (I would expect the same response to the sent commands each time) and the values are not what I am expecting.Code:char buff[7]; int rd; rd=read(fd, buff, 7); printf(" Bytes received are %d \n",rd); printf("%x%x%x%x%x%x%x\n",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6]);
I appreciate it may be difficult to diagnose specifics for external components but any guidance to a standard way to "send" and "receive" packets to and from a ttyACM0 using USB-CDC library would be great.
Thanks,
Pete.
- 10-11-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
It may be a timing issue. Have you tried placing a wait-state into the code before you read back from the device? Also, in your first example of reading/printing the results, you will overrun your buffer since the printf("%s\n, buff) function will read until it finds a nul byte... Your second example is better (but still not good). Always set your buffer size larger than the size of data you are reading, just as a precaution.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 10-15-2011 #3Just Joined!
- Join Date
- May 2006
- Posts
- 13
So here is my solution. It appears to work as expected.
SEND_pack //send pack to initiate response.
sleep(1); //wait a second so the board has time to respond
int j=0;
while ( j <= 6 ) { //only read 7 bytes (as per protocol for this device)
printf(" 0x%02x\n", my_read(fd) ); // the 0x02x means write out as hex
j++;
}
//function to do the read. Key thing here was the &c rather then in the read.
unsigned char my_read(int fd) {
unsigned char c='D';
read( fd, &c, sizeof(c) );
return c;
}


Reply With Quote