Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-11-2006, 12:12 PM   #1
LQ Newbie
Registered: Jul 2006
Posts: 2

Rep: Reputation: 0
Question RS232 serial port errors when performing mutliple writes

I am sending data through a serial port and am having a couple of problems. I have read the serial and serial programming HOWTOs.

My application basically sends data in Buffer (byte*) using the following code:

BytesLeft = BufLen;
    TxCount = write(handle, Buffer, BytesLeft);
    if(TxCount >= 0)
        BytesLeft -= TxCount;
        printf("TXStr error: %d\r\n", TxCount);
}while (BytesLeft > 0);
I use the while loop to ensure that all characters are sent; if write() returns less than BufLen, it sends the rest. This may be unnecessary.

This code works, but I'm having a problem if I call my send function too frequently. If I call the function twice in succession, write returns -1. Adding a sleep(1) before the while prevents the error, but I'd appreciate a more viable fix.

I have noticed from serial libraries on places such as that the while loop is not used, so my first question is "is the while loop necessary". if not, is there any need to monitor the return value from write() other than to catch the error condition?

My second question is regarding the sleep I need to add. It seems that there is some buffer still being written to after the write() has returned, and if I try to write again too soon, I get an error. is there anything I can check to see whether the port is ready to be written to?

Many thanks,


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
console login through RS232/serial port ? Yalla-One Linux - Software 3 02-24-2005 04:12 PM
USB - Serial RS232 Adapter blox Linux - Hardware 3 01-28-2005 10:58 AM
Serial RS232 problem Onyx^ Linux - Hardware 6 09-09-2004 11:35 PM
RS232 serial port programing blackzone Programming 2 08-11-2004 12:54 AM
serial port: RS232 RS574? blackzone Linux - Hardware 1 08-10-2004 04:15 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration