Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2006
    Posts
    14

    Cool 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.

    Code:
    //open link to board.
    int fd;
    fd = open(sComPort, O_RDWR | O_NOCTTY | O_NONBLOCK);
    I then check if that worked.....

    Code:
    if (fd>0){
    .....my code here
    }
    I then send the board my binary string packet (7 bytes)...

    Code:
    write(fd, "\x04\x14\x80\x00\x00\x68\x0f",7)
    The on-board components react as expected... All good so far

    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....

    Code:
    char buff[7];
    int rd;
    rd=read(fd, buff, 7);
    printf(" Bytes received are %d \n",rd);
    printf("%s\n",buff);
    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("%x%x%x%x%x%x%x\n",buff[0],buff[1],buff[2],buff[3],buff[4],buff[5],buff[6]);
    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.

    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.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,530
    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!

  3. #3
    Just Joined!
    Join Date
    May 2006
    Posts
    14
    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;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •