Solved

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

Posted on 1997-06-24
2
993 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Title # Comments Views Activity
How to create frequencies of a variable from SAS dataset? 10 146
Perl Awk Need Help 3 150
format the code in java 6 117
JQuery serialize and unserialize 8 270
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

738 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