ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I need to communicate a Single Board Computer so called PC104 (AMD GX466, 333 MHz, 512 MB DDR) with a sensor by RS232. The PC104 works with Slackware 13.0, without graphical interface.
Just to test it, I used a DB9 connector with Tx jumped with Rx. So, it should read the same data that were written. It’s all right when it communicates strings.
However, the communication protocol of the sensor is defined in hexadecimal numbers and I don’t know to format the read() and write() function to hexadecimal.
I show a piece of the code with the write() and read():
char buffer[ 3 ];
I tested the code with the “\xDB” and it didn’t have any compilation problem. It seems the writing process was OK! However, the program was waiting the reading process that didn’t happen. I finished the program with CTRL+C.
I’ve been looking for a solution but unfortunately without success.
I also tried the following based on a C code (for Windows) provided by the sensor company. But it also wait the reading process. Thank you very much in advance for any comments!
Well, I tried to flush the I/O before reading and writing process as the piece of code is showing but it doesn’t work. It’s not able to read...
Do you have any idea to fix the problem? Thanks in advance!
What is the value of nRead?
Are you using asynchronous input with a signal handler?
Posting your entire program might provide some better insight on your problems. With just this snippet I can only suggest that you could be trying to read characters before they are actually transmitted from the board.
Dear hda7! You are a genius! Congratulations and thank you very much! You really help me! It read the hex data that was sent!
However, the program is not so much stable...When I repeat the program, it spends some time to read and sometimes it reads 0 instead 0xDB. When it works it reads DB. I’m afraid because the sensor works with a range of acquisition frequency.
Well, I am not sure I did the right thing. I tried to put the command (stty –F /dev/ttyS0 ) in the code but there was an error at the compilation time. So, I just set the command before to execute the program...It could be the reason of the instability? If yes, how can I put it in the code?
I’m sending the code and I’d like to ask your suggestions.
//======OPEN THE SERIAL PORT======
fd = open ( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY );
if ( fd == -1 )
perror( "The serial port is not opened." );
fcntl( fd, F_SETFL, 0 );
printf( "\nThe serial port is open." );
//======SERIAL PORT CONFIGURATION======
tcgetattr( fd, &options );
Hello, Michaelk! Yes, it’s better to put the entire code than a small piece. So, I just did it. As you can see, hda gave me an excelent idea. But it's still so instable. In any case, I'm very glad with the support that all of you are providing and it's very good keep contact with you!