LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2004, 11:35 AM   #1
SeanatIL
LQ Newbie
 
Registered: Jun 2004
Location: IL
Posts: 24

Rep: Reputation: 15
Serial port Read Write


I need to implement a Serail port I/O.
what's the difference between with O_NONBLOCK and without it? Note that I have set my c_cc[VTIME] = 0 and c_cc[VMIN] = 0, which means read will return right away no matter if it's a non-blocking mode. The only difference I can see is write(). But it seems the STREAM have enough system buffer to allow write() return right away.

So what's the difference between with and without O_NONBLOCK in question line 1? Thanks

bool mstpInitSerialPort()
{
struct termios SerialIOSetting;
//if ( (fd_SerialPort=open (portname, O_RDWR|O_NOCTTY)) < 0)

if((fd_SerialPort=open(MSTPPORT, O_RDWR|O_NOCTTY/*| O_BLOCK*/))<0) //*****question 1, with and without*****
{
perror("Open Serial Port failed\n");
return false;
}

bzero(&SerialIOSetting, sizeof(SerialIOSetting));

SerialIOSetting.c_cflag = B9600| CRTSCTS| CS8 | CLOCAL | CREAD;
SerialIOSetting.c_iflag = 0; // if == IGNPAR; // ignore parity error
SerialIOSetting.c_oflag = 0; //raw output
SerialIOSetting.c_lflag = 0; // non-canonical, no echo
SerialIOSetting.c_cc[VEOF] = 0xFF;
SerialIOSetting.c_cc[VTIME] = 0; //****Note for question 1
SerialIOSetting.c_cc[VMIN] = 0; //****Note for question 1
tcflush(fd_SerialPort, TCIFLUSH);
if(tcsetattr(fd_SerialPort, TCSANOW, &SerialIOSetting) == -1)
{
perror(strerror(errno));
return false;
}
return true;
}


int SerialRx()
{
int Byte;
if ( read(fd_SerialPort, &Byte, 1) == 1)
return Byte;
else
return -1;
}

bool SerialTxBuf(byte far *txb, word txlen)
{
if( write(fd_SerialPort, txb, txlen) == txlen)
return true;
else
return false;
}
 
Old 07-13-2004, 01:36 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Here is a little help:
http://www.faqs.org/faqs/unix-faq/programmer/faq/
 
Old 07-14-2004, 03:42 PM   #3
SeanatIL
LQ Newbie
 
Registered: Jun 2004
Location: IL
Posts: 24

Original Poster
Rep: Reputation: 15
STREAM buffer size?

read() is reading from STREAM, where the input characters are buffered. what will happen if the input on serial line is too fast that my read() can't catch up? Thanks.

For example, in the following code, I read one character and do some computation. If the computer connected to my serial port send data very fast, the system buffer endded up to be full and discard characters coming in. How can I know if that happens? By the way, what's the size of the system buffer for STREAM? Thanks


for(; ; )
{
read(fd_serial,&c, 1);
//state machine transformation code
}
 
  


Reply



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
Problem to write to serial port os2 Programming 8 08-23-2016 02:48 AM
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 08:35 PM
Need help to read a signal from the CTS pin of a serial port juan_de_margo Linux - Hardware 0 02-24-2005 03:36 AM
Problem with Non-blocking serial port read ZX_SA Programming 0 01-18-2005 08:50 AM
Serial Port read in shell script tjt Linux - Newbie 1 08-12-2004 09:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:52 AM.

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