display output from serial port device
From: Robert Ngo (robertngo_at_perridot.com)
Date: 08/26/04
- Next message: Grant Edwards: "Re: display output from serial port device"
- Previous message: Ulrich Weigand: "Re: Address of instruction causing SIGSEGV"
- Next in thread: Grant Edwards: "Re: display output from serial port device"
- Reply: Grant Edwards: "Re: display output from serial port device"
- Reply: Floyd L. Davidson: "Re: display output from serial port device"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 25 Aug 2004 16:37:58 -0800
i use the following code to connect to HA3035 proximity access controller
card reader through serial port. the problem is that the output from the
devide is in some strange character. how can i format these output into
human readable form.
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int reader(int fd)
{
char buffer[26] /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int tries; /* Number of tries so far */
/* send an command followed by a CR */
write(fd, "EXXA\r", 4);
/* read characters into our string buffer until we get a CR or NL */
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr
- 1)) >0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
write(1,buffer,sizeof(buffer));
if (buffer == 0)
return (-1);
else
return (0);
}
int main(void)
{
struct termios options;
char buffer[26];
int nread;
int fd; /* File descriptor for the port */
int run =1;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_iflag |= (INPCK | ISTRIP);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
tcsetattr(fd, TCSANOW, &options);
if(reader(fd)==-1)
write(2, "error sending command to card reader\n",38);
else
write(1, "connection to reader is good\n",30);
close(fd);
}
- Next message: Grant Edwards: "Re: display output from serial port device"
- Previous message: Ulrich Weigand: "Re: Address of instruction causing SIGSEGV"
- Next in thread: Grant Edwards: "Re: display output from serial port device"
- Reply: Grant Edwards: "Re: display output from serial port device"
- Reply: Floyd L. Davidson: "Re: display output from serial port device"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|