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 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():
int nWrite;
int nRead;
char buffer[ 3 ];
char *bufptr;
The standard read and write functions should use any character values in the buffer. Most languages have a means to embed hex values in strings. I think the way to do it in C is like "\xdb" or "\xDB".
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!
May I suggest reading up on serial programing. FYI read/write length is based on bytes. So 0xDB is actually two characters and you need to transmit six characters not 3.
I based the code I've been developing on several references but the most valuable I've found was that one you suggested me.
I guess I don't understand very well your comment about the 0xDB and I'm sorry about that. Because individual hex number is composed by 4 bits so 0xDB is composed by 8 bits (1101 1011).
Concerning the number of the bytes that should be read, I guess it can be less than the serial port buffer contains. Is it right?
Never mind... Your correct... Where I was going no one knows. I guess I'm a bit tired... Yes, the value returned (nRead) is the actual number of characters read.
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!
MPRT
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." );
}
else
{
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!
I put a comment in the flush command (tcflush( fd, TCIOCLUSH ) before the read function. After that, the program works fine! There is no delay at the reading process!
To be more independent of external commands, it would be very interesting the “stty –F /dev/ttyS0 raw” be in the code. Now, I’ll do the code with more hex numbers.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.