Serial Programming: Difference between revisions

From miki
Jump to navigation Jump to search
m (→‎References: fixed wrong address)
Line 1: Line 1:
=== References ===
== References ==
* [http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html Serial Programming HOWTO]
* [http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html Serial Programming HOWTO]
* [http://www.easysw.com/~mike/serial/serial.html Serial Programming Guide for POSIX Operating System]
* [http://www.easysw.com/~mike/serial/serial.html Serial Programming Guide for POSIX Operating System]
* [http://read.pudn.com/downloads130/ebook/556289/Serial%20port%20programming%20for%20Windows%20and%20Linux.pdf Serial Port Programming in Windows and Linux]
* [http://read.pudn.com/downloads130/ebook/556289/Serial%20port%20programming%20for%20Windows%20and%20Linux.pdf Serial Port Programming in Windows and Linux]

== How-To ==

=== Flush Serial Buffers ===
Use constant <code>TCSAFLUSH</code> for function <code>tcsetattr</code> (see [http://www.linuxquestions.org/questions/linux-general-1/flush-the-serial-buffer-403127/] and [http://www.easysw.com/~mike/serial/serial.html]):

{| class="wikitable"
|+|Constants for <tt>tcsetattr</tt>
|-
!Constant!!Description
|-
|TCSANOW||Make changes now without waiting for data to complete
|-
|TCSADRAIN||Wait until everything has been transmitted
|-
|TCSAFLUSH||Flush input and output buffers and make the change
|}
This can be done even on a serial port that is already configured:
<source lang="c">
struct termios options;
tcgetattr(fd, &options); // Get current configuration of this port
cfsetispeed(&options, B115200); // Configure some port options (e.g. baud rate...)
...
tcsetattr(fd, TCSANOW, &options); // Apply the new settings immediately
... // Read / Write to the port...
tcsetattr(fd, TCSAFLUSH, &options); // Flush IO buffer
</source>

Revision as of 21:09, 26 October 2011

References

How-To

Flush Serial Buffers

Use constant TCSAFLUSH for function tcsetattr (see [1] and [2]):

Constants for tcsetattr
Constant Description
TCSANOW Make changes now without waiting for data to complete
TCSADRAIN Wait until everything has been transmitted
TCSAFLUSH Flush input and output buffers and make the change

This can be done even on a serial port that is already configured:

  struct termios  options;
  tcgetattr(fd, &options);                   // Get current configuration of this port
  cfsetispeed(&options, B115200);            // Configure some port options (e.g. baud rate...)
  ...
  tcsetattr(fd, TCSANOW, &options);          // Apply the new settings immediately 
  ...                                        // Read / Write to the port...                       
  tcsetattr(fd, TCSAFLUSH, &options);        // Flush IO buffer