Solved

Read/Write to Linux serial port using C or C++

Posted on 1997-06-24
2
945 Views
Last Modified: 2013-12-26
OS Version: Red Hat Linux release 3.0.3
Language: GNU gcc 2.7.2-2 or g++ 2.7.2-2

I need an example of how to write and read characters to the /dev/cua0 device (COM1) in Linux.  I need to communicate at a baud rate of 1200, 8 data bits, 1 stop bit, and no parity.  (1200,N,8,1).

I have tried the following code fragement:
--------------------
 if ((serial_fd = open("/dev/cua0",O_RDWR))==-1) {
    printf("Error opening /dev/cua0\n");
    return -1;
  }

  buf[0] = (char)1;
  buf[1] = (char)24;
  buf[2] = (char)230;
  i = write(serial_fd,buf,3);
  printf("wrote %d bytes\n", i);

  i = read(serial_fd, buf, 4);
  printf("read %d bytes, errno=%d\n", i,errno);      
-----------------

It writes the characters out.  I put a tester (on the port) with LEDs that flickers when the program sends characters to the port.  However I don't know how to set the port to no parity, 8 data bits, and 1 stop bit.  I was able to use the setserial command to set the baud rate.  

Does anyone have an example on how to do this?
It can be in either C or C++.
0
Comment
Question by:mag062397
  • 2
2 Comments
 
LVL 3

Accepted Solution

by:
dhm earned 100 total points
ID: 1293713
Check the termios(3) functions.  The ones you want are tcgetattr() [get current settings] and tcsetattr() [change settings].  Your code would look like this:
------------------------------------------------------
struct termios old_settings;
struct termios new_settings;

tcgetattr( serial_fd, &old_settings );
new_settings = old_settings;
cfsetispeed( &new_settings, B1200 );
cfsetospeed( &new_settings, B1200 );
new_settings.c_cflag &= ~(PARENB | CSTOPB);
tcsetattr( serial_fd, &new_settings );
-----------------------------------------------
Cfset[io]speed() are convenience functions that set the appropriate bits in the terminal control structure; you could use bitwise ANDs and ORs to accomplish the same thing.  In the next to last line, I clear the Parity-Enable and 2-Stop-Bits flags.  Incidentally, the reason I have two termios structures (old_settings and new_settings) is that it's polite to restore the terminal to the state you found it when your program exits.
0
 
LVL 3

Expert Comment

by:dhm
ID: 1293714
Whoops, forgot to set 8-bit mode...add these lines before the tcsetattr():

new_settings.c_cflag &= ~CSIZE;
new_settings.c_cflag |= CS8;
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now