how to get data from serial_port?

bridged with qdn.public.articles
Post Reply
jgp

how to get data from serial_port?

Post by jgp » Thu Nov 22, 2001 5:49 am

hi,I want to get data from serial_port,where to find the articles
about how to initial the port .thanks!

Xiaodan Tang

Re: how to get data from serial_port?

Post by Xiaodan Tang » Thu Nov 22, 2001 2:52 pm

jgp <gpjin@nairc.ac.cn> wrote:
hi,I want to get data from serial_port,where to find the articles
about how to initial the port .thanks!
The basic is:

fd = open("/dev/ser1", O_RDWR);
read(fd, buf, bufsize);
write(fd, buf, bufsize);
close(fd);

For setting tty related params, tc*, especialy tcgetaddr(), tcsetaddr().
Or if you are asking utils, stty is the one used to change tty paramaters.

-xtang

Bill Caroselli

Re: how to get data from serial_port?

Post by Bill Caroselli » Fri Nov 23, 2001 5:56 pm

Isn't that tcgetattr() and tcsetattr().

--
Bill Caroselli -- 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


"Xiaodan Tang" <xtang@qnx.com> wrote in message
news:9tj3eu$86h$1@nntp.qnx.com...
jgp <gpjin@nairc.ac.cn> wrote:
hi,I want to get data from serial_port,where to find the articles
about how to initial the port .thanks!

The basic is:

fd = open("/dev/ser1", O_RDWR);
read(fd, buf, bufsize);
write(fd, buf, bufsize);
close(fd);

For setting tty related params, tc*, especialy tcgetaddr(), tcsetaddr().
Or if you are asking utils, stty is the one used to change tty paramaters.

-xtang

Xiaodan Tang

Re: how to get data from serial_port?

Post by Xiaodan Tang » Sat Nov 24, 2001 2:29 am

Bill Caroselli <qtps@earthlink.net> wrote:
Isn't that tcgetattr() and tcsetattr().
Yes. Sorry my fingers are more formaliar with "addr" :)

-xtang
--
Bill Caroselli -- 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net
"Xiaodan Tang" <xtang@qnx.com> wrote in message
news:9tj3eu$86h$1@nntp.qnx.com...
jgp <gpjin@nairc.ac.cn> wrote:
hi,I want to get data from serial_port,where to find the articles
about how to initial the port .thanks!

The basic is:

fd = open("/dev/ser1", O_RDWR);
read(fd, buf, bufsize);
write(fd, buf, bufsize);
close(fd);

For setting tty related params, tc*, especialy tcgetaddr(), tcsetaddr().
Or if you are asking utils, stty is the one used to change tty paramaters.

-xtang

diamond

Re: how to get data from serial_port?

Post by diamond » Thu Dec 06, 2001 7:13 am

thanks!
if I want to initial the serial-port to be;
9600bps,8 bit data,1 bit stop-bit,no parity.
who can tell me how to set the stop_bit to be 1?

cfsetispeed(&termios_p, 9600); // input
termios_p.c_cflag &= ~CSIZE;
termios_p.c_cflag |= CS8|CREAD |?




Xiaodan Tang <xtang@qnx.com> wrote in message
news:9tn0lg$lg8$1@nntp.qnx.com...
Bill Caroselli <qtps@earthlink.net> wrote:
Isn't that tcgetattr() and tcsetattr().

Yes. Sorry my fingers are more formaliar with "addr" :)

-xtang

--
Bill Caroselli -- 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


"Xiaodan Tang" <xtang@qnx.com> wrote in message
news:9tj3eu$86h$1@nntp.qnx.com...
jgp <gpjin@nairc.ac.cn> wrote:
hi,I want to get data from serial_port,where to find the articles
about how to initial the port .thanks!

The basic is:

fd = open("/dev/ser1", O_RDWR);
read(fd, buf, bufsize);
write(fd, buf, bufsize);
close(fd);

For setting tty related params, tc*, especialy tcgetaddr(),
tcsetaddr().
Or if you are asking utils, stty is the one used to change tty
paramaters.

-xtang

Ian Cannon

Re: how to get data from serial_port?

Post by Ian Cannon » Thu Dec 06, 2001 7:41 am

Previously, diamond wrote in qdn.public.articles:
thanks!
if I want to initial the serial-port to be;
9600bps,8 bit data,1 bit stop-bit,no parity.
who can tell me how to set the stop_bit to be 1?

cfsetispeed(&termios_p, 9600); // input
termios_p.c_cflag &= ~CSIZE;
termios_p.c_cflag |= CS8|CREAD |?

termios_p.c_cflag &= ~CSTOPB;


Xiaodan Tang <xtang@qnx.com> wrote in message
news:9tn0lg$lg8$1@nntp.qnx.com...
Bill Caroselli <qtps@earthlink.net> wrote:
Isn't that tcgetattr() and tcsetattr().

Yes. Sorry my fingers are more formaliar with "addr" :)

-xtang

--
Bill Caroselli -- 1(530) 510-7292
Q-TPS Consulting
QTPS@EarthLink.net


"Xiaodan Tang" <xtang@qnx.com> wrote in message
news:9tj3eu$86h$1@nntp.qnx.com...
jgp <gpjin@nairc.ac.cn> wrote:
hi,I want to get data from serial_port,where to find the articles
about how to initial the port .thanks!

The basic is:

fd = open("/dev/ser1", O_RDWR);
read(fd, buf, bufsize);
write(fd, buf, bufsize);
close(fd);

For setting tty related params, tc*, especialy tcgetaddr(),
tcsetaddr().
Or if you are asking utils, stty is the one used to change tty
paramaters.

-xtang




diamond

Re: how to get data from serial_port?

Post by diamond » Sun Dec 09, 2001 1:23 pm

hi,here is my code ,when I run ./a.out under QNX4.25.
It just print some information anout the settings of serial_port,
but no data received from serial_port as expected.what's wrong?
Is there anything to do with the configure of hardware?



9600baud,8 bit data,1bit stop.

thank you all the same.


#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(;;)
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


// Control modes:
//
// CS8----> 8 bits-per-byte
// CREAD--> Enable receiver. If this bit is not set,
// no characters are received.
// no parity

termios_p.c_cflag &= ~CSIZE;
termios_p.c_cflag |= CS8|CREAD;
termios_p.c_cflag &=~CSTOPB;

// 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);

}
}

DMitri

Re: how to get data from serial_port?

Post by DMitri » Tue Jan 29, 2002 10:39 am

One problem might be that you use 9600 as a speed value.
Legal speed values are declared in termios.h
So, it must be:

#define BAUD B9600

Also, you don't specify if your line is going to be in canonical
or raw mode. In canonical mode serial driver will do nothing until it
gets newline character. Canonical mode is enabled by ICANON bit .

"diamond" <gpjin@nairc.ac.cn> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ ÓÌÅÄÕÀÝÅÅ:
news:9uvo9c$4rj$1@inn.qnx.com...
hi,here is my code ,when I run ./a.out under QNX4.25.
It just print some information anout the settings of serial_port,
but no data received from serial_port as expected.what's wrong?
Is there anything to do with the configure of hardware?



9600baud,8 bit data,1bit stop.

thank you all the same.


#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(;;)
read_device (file_des);
}

int open_device (char *device)


int file_des;
struct _dev_info_entry info;

if ((file_des = open(device, O_RDWR)) =

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


// Control modes:
//
// CS8----> 8 bits-per-byte
// CREAD--> Enable receiver. If this bit is not set,
// no characters are received.
// no parity

termios_p.c_cflag &= ~CSIZE;
termios_p.c_cflag |= CS8|CREAD;
termios_p.c_cflag &=~CSTOPB;

// 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));

e{
printf("bytes read is %d\n", bytes);
for (i=0; i<bytes; i++)
printf("\tbyte_read = %o\n", readbuf, i);

}
}














Post Reply

Return to “qdn.public.articles”