ElEscalador
Posts: 856
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

I think other stuff is stealing USB serial data

Thu Sep 29, 2016 3:47 pm

How can I tell what is reading my /dev/ttyUSB0 and how do I make it stop? That's my chief suspect anyway - I've got a bit of code that writes a few bytes with pigpio and then reads them. I've tinkered around with varying delays between opening the USB port and writing, and between writing and trying to read. I can run the exact same program over and over and sometimes I get my bytes back and sometimes nothing at all. I have an FTDI chip and the tx and rx jumped for now. I do know that the device works because it is hooked up to my roomba and communicates effectively. (although I suspect the same problem with that code even using wiringPi - I've just made a bastardized function that samples foreeeever until it receives complete data strings. Last time I checked, I have to throw away 60% of the data roomba sends until I receive an entire string to work with. I'm overhauling all the robot software now that I know a little about coding, but would not like to have to live with that) A sample of the current test code is below, in case that's useful.

Code: Select all

    int USBHandle = serOpen("/dev/ttyUSB0",115200,0);

  
    cout << "USBHandle = " << USBHandle<< endl;
    gpioSleep(PI_TIME_RELATIVE,0,350000);

    serWriteByte(USBHandle,128);
    serWriteByte(USBHandle,333);
    serWriteByte(USBHandle,131);
    gpioSleep(PI_TIME_RELATIVE,0,2000);  //required or I read before data available

for(int i=0; i < 8000; i++){
if(serDataAvailable(USBHandle)>0){
    cout <<"Byte read = " << serReadByte(USBHandle)<< endl;
    cout <<"Byte read = " << serReadByte(USBHandle)<< endl;
    cout <<"Byte read = " << serReadByte(USBHandle)<< endl;
    cout << "In i = " << i << endl;  //added to see if I was reading too soon. 
}}

    serClose(USBHandle);


    gpioTerminate(); //Always use at end of program to release memory and DMA channels
    return 0; 
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

PiGraham
Posts: 4199
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: I think other stuff is stealing USB serial data

Thu Sep 29, 2016 4:02 pm

What happens with other serial comms read/write? what about screen?

Code: Select all

screen /dev/ttyUSB0 115200 
What happens at lower baud rates?

You code looks odd. The read loop will exit after 8000 loops. with no sleeps in that loop that could be pretty quick.
You attempt to read three bytes after only checking once if some data is available. Try reading one byte at a time each time serDataAvailable() reports there is data, or use the return value in a for loop to read then number of bytes that are available.

ElEscalador
Posts: 856
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: I think other stuff is stealing USB serial data

Thu Sep 29, 2016 4:27 pm

That's a good question...I should try minicom. And for that matter test this same code on the uart. Lower baud rates doesn't seem to change anything.

I'm sure my code does look goofy - I'm still an uneducated hack learning by doing. The read loop was really just a troubleshooting thing to help me determine how long the wait between write and read should be - if that wait is too short sometimes it reads at i = 170 or maybe i = 850, but never higher than that. With the delay that's in there, i always = 0 when I do recieve my bytes. Which is about half the time. And when I do receive bytes - it's all or none. (Though with the roomba spitting out I think 80ish bytes, it was common to get partial strings. Maybe this test bit is just too short to get chopped)
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

mutley
Posts: 61
Joined: Sat Jan 02, 2016 8:06 pm

Re: I think other stuff is stealing USB serial data

Thu Sep 29, 2016 4:55 pm

ElEscalador wrote: I've tinkered around with varying delays between opening the USB port and writing, and between writing and trying to read. I can run the exact same program over and over and sometimes I get my bytes back and sometimes nothing at all.
ElEscalador wrote: The read loop was really just a troubleshooting thing to help me determine how long the wait between write and read should be
It sounds like you may have the parity / stop bits / databits set incorrectly.
Open the port in blocking mode, then your read will wait indefinitely until it reads something. This is a sure way to make sure you read everything being sent while debugging communication. Probably want to open in in non blocking mode and set the timeout for the read when you've figured out what's going on. Also remember that what ever put the data on the serial port can also take it off (and usually does) when it wants to send more.
Reading and writing to serial ports is trivial, and all the libraries out there that are supposed to make it easier, usually just make it a real pain to debug problems like this.
Here are some examples in C.
http://www.tldp.org/HOWTO/Serial-Progra ... /x115.html

User avatar
FTrevorGowen
Forum Moderator
Forum Moderator
Posts: 5911
Joined: Mon Mar 04, 2013 6:12 pm
Location: Bristol, U.K.
Contact: Website

Re: I think other stuff is stealing USB serial data

Thu Sep 29, 2016 5:15 pm

W.r.t. using other (serial terminal) tools for debugging you may find GtkTerm useful - it was for me when dealing with a "RS232" digital multimeter which used an "odd" protocol and continuously streamed data:
https://www.cpmspectrepi.uk/raspberry_p ... 000zc.html
You will probably have to install GtkTerm manually - there's a link on that page to download and basic installation information (there I was using it to test another device).
Trev.
Still running Raspbian Jessie or Stretch on some older Pi's (an A, B1, 2xB2, B+, P2B, 3xP0, P0W, 2xP3A+, P3B+, P3B, B+, and a A+) but Buster on the P4B's. See: https://www.cpmspectrepi.uk/raspberry_pi/raspiidx.htm

ElEscalador
Posts: 856
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: I think other stuff is stealing USB serial data

Fri Sep 30, 2016 4:23 pm

Thanks. I don't think I could have a parity problem (especially with the test program, since it's just writing out and feeding directly back into itself with PigPio). Also, the Pigpio library function serOpen() only calls for a baud rate and I don't know how to change parity or stop bits - I presume it defaults to 8N1, which seems most common?

Currently stuck in an academic conference...hope to tinker this afternoon.
Robotics tips, hacks, book extras https://youtube.com/practicalrobotics

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 27433
Joined: Sat Jul 30, 2011 7:41 pm

Re: I think other stuff is stealing USB serial data

Mon Oct 03, 2016 11:07 am

Found an interesting trick for testing UART/USB0uart ports.

This line copies input from the device to the screen, running in background so you can still use command line
cat < /dev/ttyUSB0 &

And this send data to the device
echo "Hello" > /dev/ttyUSB0
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed.
I've been saying "Mucho" to my Spanish friend a lot more lately. It means a lot to him.

Return to “General discussion”