serial port access on POSIX

serial port access on POSIX

Post by Bob » Sun, 09 Dec 2001 11:26:33



I have been running linux for 10 months now, decided to finally rid my life
of windows.

But I have a problem, a device which is accessed through the serial port
only had software for windows, so I am stuck without the ability to access
it.

I decided I might try to write something to access it myself, but I fell
flat on my face. I am using g++.

I have specs on how the port needs to be set so that the device can
understand the data being sent to it.

It requires a baud of 9600, data sent as 8 data bits, no parity, 1 stop
bit; abbreviated N81.

The device then needs 3 byte of data, to control devices, which are dasiy
chained off the device, which is connect to ttyS1 of my PC.

The first byte is a sync byte, of size 255, while the other two are bytes
that vary of size depending on what device needs to be controlled.

Here is my lame attempt:
#include <iostream>
#include <asm/io.h>
#define BAUDRATE B9600
#define OUTPORT "/dev/ttyS1"
#include <string>
#include <fstream>
using namespace std;

int main() {
int clear;
clear = char(255);
string data, data2;
ofstream out("OUTPORT");
cin >> data;
cin >> data2;
out << clear << data << data2 << endl;

Quote:}

// EOF//

(When the program executed, i typed in data and data 2 by hand by using
alt + (numpad keys) to get char values)

If anybody good assist me, (I have looked through termios in man, and
almost everything regarding serial ports in man) it would be greatly
apperciated.

Regards,
Bob

 
 
 

serial port access on POSIX

Post by Jason A Kill » Sun, 09 Dec 2001 00:56:47



> I have been running linux for 10 months now, decided to finally rid my life
> of windows.

> But I have a problem, a device which is accessed through the serial port
> only had software for windows, so I am stuck without the ability to access
> it.

> I decided I might try to write something to access it myself, but I fell
> flat on my face. I am using g++.

> I have specs on how the port needs to be set so that the device can
> understand the data being sent to it.

> It requires a baud of 9600, data sent as 8 data bits, no parity, 1 stop
> bit; abbreviated N81.

> The device then needs 3 byte of data, to control devices, which are dasiy
> chained off the device, which is connect to ttyS1 of my PC.

> The first byte is a sync byte, of size 255, while the other two are bytes
> that vary of size depending on what device needs to be controlled.

> Here is my lame attempt:
> #include <iostream>
> #include <asm/io.h>
> #define BAUDRATE B9600
> #define OUTPORT "/dev/ttyS1"
> #include <string>
> #include <fstream>
> using namespace std;

> int main() {
> int clear;
> clear = char(255);
> string data, data2;
> ofstream out("OUTPORT");
> cin >> data;
> cin >> data2;
> out << clear << data << data2 << endl;
> }
> // EOF//

> (When the program executed, i typed in data and data 2 by hand by using
> alt + (numpad keys) to get char values)

> If anybody good assist me, (I have looked through termios in man, and
> almost everything regarding serial ports in man) it would be greatly
> apperciated.

> Regards,
> Bob

You might look at the code for bottlerocket, it's a program to control
an X10 device on the serial port.  You can get it here:
http://mlug.missouri.edu/~tymm/

 
 
 

serial port access on POSIX

Post by Terran Melconi » Sun, 09 Dec 2001 06:46:50




>If anybody good assist me, (I have looked through termios in man, and
>almost everything regarding serial ports in man) it would be greatly
>apperciated.

The real solution is to read this book:

Advanced Programming in the Unix Environment - W.R. Stevens
    http://www.kohala.com/start/apue.html
    http://www.bookpool.com/.x/x/ss/0201563177
    http://www.isbn.nu/0201563177

For now, I'll post and annotate a simple program I wrote awhile ago
which reads from a serial port.  It uses C; IMO this really isn't the
sort of thing where C++ provides any benefit.  Hopefully once you look
through this example the termios manpage will make more sense.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int main (void)
{
    struct termios tp;
    char *portstr="/dev/ttyS0";
    int port, count;
    char buf[512];

    // Note the use of open, not fopen.  We're doing low-level stuff,
    // and stdio (or C++ streams, God help you) would provide much
    // trouble and little benefit
    port=open(portstr,O_NOCTTY|O_SYNC|O_RDWR);

    // Always check for errors
    if (port==-1)
    {
        fprintf(stderr, "Could not open %s.\n", portstr);
        exit(1);
    }

    // We have to set the desired parameters on the port.  We start by
    // reading the current ones
    tcgetattr(port, &tp);

    // cfmakeraw is a convenience shortcut which sets several
    // settings indicating that all control characters should be
    // ignored
    cfmakeraw(&tp);

    // Setting the speed is pretty obvious
    cfsetispeed(&tp, B4800);

    // 8-bit data
    tp.c_cflag|=CS8;

    // no model control lines
    tp.c_cflag|=CLOCAL;

    // turn off rts/cts
    tp.c_cflag&=(~CRTSCTS);

    // make sure it worked
    if (tcsetattr(port, TCSANOW, &tp))
    {
        fprintf(stderr, "Failed to configure port %s.\n", portstr);
        exit(1);
    }

    // and read from the port one byte at a time
    while(1)
    {
        count=read(port,buf,1);
        if (count==-1)
            break;
        printf("%c",buf[0]);
        fflush(stdout);
    }

Quote:}

 
 
 

serial port access on POSIX

Post by Geoff Clar » Wed, 12 Dec 2001 23:14:11



>    // cfmakeraw is a convenience shortcut which sets several
>    // settings indicating that all control characters should be
>    // ignored
>    cfmakeraw(&tp);

cfmakeraw is not portable.  (The OP specified POSIX).

Quote:>    // 8-bit data
>    tp.c_cflag|=CS8;

You must clear the current character size before setting the one
you want:

     tp.c_cflag&=~CSIZE;
     tp.c_cflag|=CS8;

Quote:>    // turn off rts/cts
>    tp.c_cflag&=(~CRTSCTS);

CRTSCTS is not portable.

--
Geoff Clare

 
 
 

1. posix/unix parallel port access

G'day,
    I have one of those new Dallas Semiconductor Devices, three wire
temperature sensors and switches, that are basically run off of the
parallel port.  I need to be able to send/recieve data over the parallel
port and was wondering how to do that under linux?  Thanks alot!
                                                Matt

2. Get on to Internet.

3. serial port on v490 and Serial port on StorEdge 3510

4. delay on boot for pppd (starting in background)

5. Serial port to serial port copying software

6. Game mode indeterminate

7. Send traffic from one serial port out to another serial port

8. backspace in dialog boxes not working right!

9. Serial port to serial port with null modem cable

10. Serial port - to - serial port networking

11. serial console - lilo serial= no serial port input

12. Serial port access in wine

13. Serial Port Access