ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to write a very simple test program that communicates with a serial device. What I want to accomplish is very simple:
1. Wait for a keypress
2. Send a 4 byte command to the device
3. Wait for it to send a byte back
4. Go to step 1
The program successfully sends the command to the device (the device carries out the action), but it never receives the reply byte back, instead blocking on the read forever. I have verified that the device does indeed send the response byte by sending it commands using gtkterm, and the response character is displayed every time.
Frankly, I am quite stumped, as I have looked at many examples and tried several different things. My current code is as follows:
/dev/ttyUSBx devices are valid and assigned to USB serial port adapters. You should use non-canonical input processing i.e newtio.c_lflag = 0 or asynchronous with a signal handler. Canonical input waits until a line terminating character is received.
Your correct I misread that line.... I do see a mistake.
No need for the &.
That code looks fine to me. The response buffer is a char scalar, 1 byte deep. read() requires a pointer to it. Ordinarily, one would define the buffer as an array, but here, that is not the case.
With respect to the original problem, I think the cooked mode being used is the problem. Non-canonical processing is probably the solution.
--- rod.
That code looks fine to me. The response buffer is a char scalar, 1 byte deep. read() requires a pointer to it. Ordinarily, one would define the buffer as an array, but here, that is not the case.
Oops.. Your correct... I stand corrected again.... I shall clean my glasses and look again.
Not sure what the problem is anymore other then you might not be sending the correct characters and the getchar() requires the enter key to be pressed i.e if you are not actually using the enter key as your keypress.
Not sure what the problem is anymore other then you might not be sending the correct characters and the getchar() requires the enter key to be pressed i.e if you are not actually using the enter key as your keypress.
Well, I am indeed using the enter key as my keypress, and I have verified that it is indeed hanging on the read.
How do you know that the response is not received? Your code does nothing with any received characters.
Sorry for having misread the code with respect to the use of non-canonical processing.
--- rod.
How do you know that the response is not received? Your code does nothing with any received characters.
Sorry for having misread to code with respect to the use of non-canonical processing.
--- rod.
Well, the main purpose is to just wait until the byte is received, and I don't really care what it is (it's actually always 'R'). But I have indeed tried putting a printf after the read statement, and it never got executed. The key issue is that my program hangs on the read command forever, even though I know that a byte has been sent back.
Have you tested the return codes from open(), tcsetattr(), write(), and read()? Permissions on the device? If you set VTIME to a non-zero value, does the read() call get any data?
--- rod.
I've tested your code using two laptops with the device being simulated via hyperterminal. Obviously the laptop does not care what characters are being sent to it and in fact I changed them to regular ASCII text. I also added some additional debug print statements but basically the program works for me.
Okay, so some more weirdness. Just for grins I tried rewriting the program in python, and it works. I am at a loss as to why it doesn't work in C (since it does work for you).
Code:
import sys
import serial
ser = serial.Serial('/dev/ttyUSB0', 9600)
while 1:
char = sys.stdin.read(1)
if char == 'q':
break;
ser.write("\xA5\x5A\x0A\xF5")
ser.read(1)
ser.close()
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.