serial port programming

Hi

I’ve been trying to make a simple program to communicate with a
robot using RS-232, it seems like there’s 2 approches (correct me if I’m
wrong)
on low level is qnx_ioctl(), and the other using the termios strcuture.

I tried to implement the program shown in the Knowledge base, but I
encountered
problems, beacuse I don’t have the <sys/dev.h> header file, Is there a
problem with
my QNX installation? could someone point me into the right direction?

Any ideas/advice will be greatly appreciated.

Program listing:

#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <termios.h>
#include <sys/dev.h>
#include <sys/uio.h>

#define SERDEV1 “/dev/ser1”
#define BAUD 9600

int open_device (char *device);
void serial_device_init(int file_des);
void read_device (int file_des);

main()
{
int file_des;

file_des = open_device (SERDEV1);
serial_device_init (file_des);
for(;:wink:
read_device (file_des);
}

int open_device (char *device)
{
int file_des;
struct _dev_info_entry info;

if ((file_des = open(device, O_RDWR)) = -1{
printf(“error opening %s: %s\n”,
device, strerror(errno));
exit(1);
}
if(dev_info(file_des, &info) != -1)
printf(“NODE %d, TTY %d\n”, info.nid, info.tty);
else
printf(“dev_info failed\n”,strerror(errno));
return(file_des);
}
void serial_device_init(int file_des)
{
int i;
struct termios termios_p;
speed_t speed = BAUD;

// get the control structure for /dev/ser1

tcgetattr(file_des, &termios_p);

/* get control structure for /dev/ser1 */

// Set the baud (will he set with tcsetattr())

cfsetispeed(&termios_p, speed); // input
cfsetospeed(&termios_p, speed); // output

termios_p.c_cflag &= ~CSIZE;
termios_p.c_cflag |= CS8|CREAD|PARENB;
termios_p.c_iflag |=PARMRK | INPCK;
termios_p.c_iflag &= ~IGNPAR;

// disable all control characters in the c_cc array

for (i = 0; i <NCCS; i++)
termios_p.c_cc _= PC_VDISABLE;

// Apply the settings immediately

tcsetattr(file_des, TCSANOW, &termios_p);
tcgetattr(file_des, &termios_p);

// Print out some status information

printf(“Input modes %lu\n”, termios_p.c_iflag);
printf(“Output modes %lu\n”, termios_p.c_oflag);
printf(“Control modes %lu\n”, termios_p.c_cflag);
printf(“Local modes %lu\n”, termios_p.c_lflag);
printf(“Device Status %d\n”, termios_p.c_status);
printf(“QNX spec flags %d\n”, termios_p.c_qflag);
}

void read_device (int file_des)
{
int bytes;
char readbuf[1]={0};
int i;

// Read from the serial port

if((bytes = dev_read(file_des, &readbuf,
1, 1, 0, 0, 0, NULL)) = -1)
printf(“error on dev_read %s\n”, strerror(errno));
else{
printf(“bytes read is %d\n”, bytes);
for (i=0; i<bytes; i++)
printf("\tbyte_read = %o\n", readbuf, i);
}

I did not read the Knowledge base article, but here is some info:

“Hugo Ordoñez” wrote:

Hi

I’ve been trying to make a simple program to communicate with a
robot using RS-232, it seems like there’s 2 approches (correct me if I’m
wrong)
on low level is qnx_ioctl(), and the other using the termios strcuture.

The POSIX approach we’ve used successfully is what you’ve called the termios
approach. BTW, the POSIX way to specify the baud rate is to use ‘B9600’ from
termios.h, but your way should work too.

I tried to implement the program shown in the Knowledge base, but I
encountered
problems, beacuse I don’t have the <sys/dev.h> header file, Is there a
problem with

Looking back thru our code, we do not include the file <sys/dev.h>.

my QNX installation? could someone point me into the right direction?

Any ideas/advice will be greatly appreciated.

Program listing:

#include <string.h
#include <errno.h
#include <fcntl.h
#include <unistd.h
#include <stdlib.h
#include <stdio.h
#include <termios.h
#include <sys/dev.h
#include <sys/uio.h

#define SERDEV1 “/dev/ser1”
#define BAUD 9600

int open_device (char *device);
void serial_device_init(int file_des);
void read_device (int file_des);

main()
{
int file_des;

file_des = open_device (SERDEV1);
serial_device_init (file_des);
for(;:wink:
read_device (file_des);
}

int open_device (char *device)
{
int file_des;
struct _dev_info_entry info;

if ((file_des = open(device, O_RDWR)) = -1{
printf(“error opening %s: %s\n”,
device, strerror(errno));
exit(1);
}
if(dev_info(file_des, &info) != -1)
printf(“NODE %d, TTY %d\n”, info.nid, info.tty);
else
printf(“dev_info failed\n”,strerror(errno));
return(file_des);
}
void serial_device_init(int file_des)
{
int i;
struct termios termios_p;
speed_t speed = BAUD;

// get the control structure for /dev/ser1

tcgetattr(file_des, &termios_p);

/* get control structure for /dev/ser1 */

// Set the baud (will he set with tcsetattr())

cfsetispeed(&termios_p, speed); // input
cfsetospeed(&termios_p, speed); // output

termios_p.c_cflag &= ~CSIZE;
termios_p.c_cflag |= CS8|CREAD|PARENB;
termios_p.c_iflag |=PARMRK | INPCK;
termios_p.c_iflag &= ~IGNPAR;

// disable all control characters in the c_cc array

for (i = 0; i <NCCS; i++)
termios_p.c_cc > _= PC_VDISABLE;

// Apply the settings immediately

tcsetattr(file_des, TCSANOW, &termios_p);
tcgetattr(file_des, &termios_p);

// Print out some status information

printf(“Input modes %lu\n”, termios_p.c_iflag);
printf(“Output modes %lu\n”, termios_p.c_oflag);
printf(“Control modes %lu\n”, termios_p.c_cflag);
printf(“Local modes %lu\n”, termios_p.c_lflag);
printf(“Device Status %d\n”, termios_p.c_status);
printf(“QNX spec flags %d\n”, termios_p.c_qflag);
}

void read_device (int file_des)
{
int bytes;
char readbuf[1]={0};
int i;

// Read from the serial port

if((bytes = dev_read(file_des, &readbuf,
1, 1, 0, 0, 0, NULL)) = -1)
printf(“error on dev_read %s\n”, strerror(errno));
else{
printf(“bytes read is %d\n”, bytes);
for (i=0; i<bytes; i++)
printf("\tbyte_read = %o\n", readbuf> , i);
}

_–

John H. Zouck
The Johns Hopkins University
Applied Physics Laboratory
============================_