Solved

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

Posted on 1997-06-24
2
978 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

735 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