Find the answer to your Linux question:
Results 1 to 3 of 3
I am trying to read data over the serial port to display in a graph but I am having a hard time getting the data read correctly. Here is what ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    14

    C++ LibSerial reads the same thing

    I am trying to read data over the serial port to display in a graph but I am having a hard time getting the data read correctly.

    Here is what I have so far.
    Code:
            SerialStream usart;
    	usart.Open("/dev/ttyUSB0");
    	usart.SetBaudRate(SerialStreamBuf::BAUD_38400);
    	usart.SetCharSize(SerialStreamBuf::CHAR_SIZE_8);
    	usart.SetNumOfStopBits(1);
    	usart.SetParity(SerialStreamBuf::PARITY_NONE);
    	usart.SetFlowControl(SerialStreamBuf::FLOW_CONTROL_NONE);
    
    	char out_buff[1] = {START_DATA};
    	usart.write(out_buff, 1);
    
    	uint16_t buffer[3];
    	char data;
    	cout << "Reading" << endl;
    	while (1)
    	{
    		usart.read(&data, 1);
    		cout << "Waiting for packet..." << endl;
    		while ((uint8_t)data != PACKET_START)
    		{
    			usart.read(&data, 1);
    		}
    		usart.read((char *)buffer, 6);
    
    		cout << buffer[0] << " " << buffer[1] << " " << buffer[2] << endl;
    	}
    Sending START_DATA starts the data flow. Each packet has 7 bytes, the first being PACKET_START and the other three being uint16_t.

    When I run the program it works for a few reads but then just prints the same thing over and over. If I turn off the device sending the data it still reads the same thing.

    It looks something like this
    Code:
    Waiting for packet...
    35 79 7868
    Waiting for packet...
    35 80 7843
    Waiting for packet...
    35 76 7218
    Waiting for packet...
    32 79 8042
    Waiting for packet...
    35 75 8043
    Waiting for packet...
    35 76 7805
    Waiting for packet...
    35 79 7273
    Waiting for packet...
    35 79 7877
    Waiting for packet...
    32 75 8042
    Waiting for packet...
    33 79 7219
    Waiting for packet...
    35 79 7219
    Waiting for packet...
    35 79 7219
    Waiting for packet...
    35 79 7219
    35 79 7219 is repeated forever.

    Any ideas how to get this working?

  2. #2
    Linux Guru Rubberman's Avatar
    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
    If there is no more data received, your usart.read() function will still return, therefore it is printing out the data in your buffer from the last successful packet read. You probably need to use the select() function on the port in order to get notified when it actually has data to read, unless the usart class has some sort of wait-for-data() function that you can use which does basically the same thing as select().
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    14
    That's what I thought but if I stop the serial stream then it seems to wait for data. Maybe I am wrong.

    Anyways I redid it with termios and wrote my own wait for data function.

    Thanks anyways

Posting Permissions

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