How to read serial with interrupt serial? - c
I'm trying to read NMEA message in Linux. But I can't get a completely message:
54.441,V,,,,,0.00,0.00,010720,,,N*42
$GPVTG,0.00,T,,M,0.00,N,0.00,K,N*32
$GPGGA,020954.441,,,,,0,0,,,M,,M,,*43
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSA,A,1,,,,,,,,,,,,,,,*1E
$GPGSV,1,1,00*79
$GLGSV,1,1,00*65
$GPGLL,,,,,020954.441,V,N*71
$GP
The first line and last line is the one message but it have been splited. I thing, It's cause by sleep 1 second. And It's not right at all. I think I should use interrupt serial.
My idea is when data in come, interrupt serial will run a function which read serial and handle it. After that, system will sleep until next message. I searched some material but It's doesn't help.
This is my new code and it's not working:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
void signal_handler_IO ();
int fd;
int connected;
struct termios termAttr;
struct sigaction saio;
int main(int argc, char *argv[])
{
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open port\n");
exit(1);
}
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, O_ASYNC );
tcgetattr(fd,&termAttr);
cfsetispeed(&termAttr,B9600);
cfsetospeed(&termAttr,B9600);
termAttr.c_cflag &= ~PARENB;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
tcsetattr(fd,TCSANOW,&termAttr);
printf("UART1 configured....\n");
while(1){
sleep(1);
}
close(fd);
exit(0);
}
void signal_handler_IO ()
{
FILE *csv;
char buff [1024];
int n = read(fd, &buff, sizeof(buff));
char * token = strtok(buff, ",");
csv=fopen("csvfile.csv","w");
while( token != NULL ) {
fprintf(csv,"%s\n",token);
token = strtok(NULL, ",");
}
fclose(csv);
}
What should I do now ?
NMEA message are lines, ending with a '\n'.
Change read() to fgets() (open using fopen()) and read as a line into a string for later strtok() processing.
See also #Craig Estey ideas.
This is prefaced by my top comment.
thank you for your positive response. Did you mean I should use read() function like my old code ? And actually, I have never working with select before. But I very interesting with your idea. And I hope you can show me more the way which apply on my case.
Okay, here's a simple [and untested] version that does not use a signal handler. And, I'm using poll instead of select [they are similar] because it's easier to use.
Note that you opened the TTY device file with O_NDELAY, so the read call is non-blocking.
Also note that the open device will not produce an EOF condition either the way you did it or the way I'm doing it.
So, you'll have to have code that looks for a line that signifies the last line (e.g. $GP).
Or, after an initial wait the first time in the loop, a subsequent timeout should indicate no more input
Anyway, here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
#if 1
#include <poll.h>
#endif
void signal_handler_IO(); /* definition of signal handler */
int fd;
struct termios termAttr;
struct sigaction saio;
struct pollfd fdpoll;
int
main(int argc, char *argv[])
{
int timout;
FILE *fout = NULL;
int buf_has_GP = 0;
int lastchr = -1;
int curchr;
int err;
int rlen;
int idx;
char buf[1000];
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open port\n");
exit(1);
}
#if 0
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO, &saio, NULL);
#endif
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, O_ASYNC);
tcgetattr(fd, &termAttr);
cfsetispeed(&termAttr, B9600);
cfsetospeed(&termAttr, B9600);
termAttr.c_cflag &= ~PARENB;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
tcsetattr(fd, TCSANOW, &termAttr);
printf("UART1 configured....\n");
fout = fopen("csvfile.csv","w");
fdpoll.fd = fd;
fdpoll.events = POLLIN;
timout = 10000;
while (1) {
err = poll(&fdpoll,1,timout);
// timeout
if (err == 0)
break;
// error
if (err < 0) {
fprintf(stderr,"error -- %s\n",strerror(errno));
break;
}
// err will always be _one_ because poll's second arg is 1
while (1) {
rlen = read(fd,buf,sizeof(buf));
if (rlen <= 0)
break;
fwrite(buf,1,rlen,fout);
// need to check buf looking for last line (e.g. $GP)
// to know when to stop
// since read is _not_ line oriented we have to check for G followed
// by P [and they may or may not occur in the same read call]
// FIXME -- this is quite crude -- just to illustrate
for (idx = 0; idx < rlen; ++idx) {
curchr = buf[idx];
buf_has_GP = ((lastchr == 'G') && (curchr == 'P'));
if (buf_has_GP)
break;
lastchr = curchr;
}
if (buf_has_GP)
break;
}
if (buf_has_GP)
break;
timout = 1000;
#if 0
sleep(1);
#endif
}
close(fd);
fclose(fout);
exit(0);
}
void
signal_handler_IO()
{
FILE *csv;
FILE *ff;
char buff[1024];
ff = fopen("/dev/ttyUSB0", "r");
// int n = read(fd, &buff, sizeof(buff));
fgets(buff, sizeof(buff), ff);
char *token = strtok(buff, ",");
csv = fopen("csvfile.csv", "w");
while (token != NULL) {
fprintf(csv, "%s\n", token);
token = strtok(NULL, ",");
}
sleep(0.2);
fclose(csv);
}
UPDATE:
Thank you so much for spend your time for me. I compiled it without error. Unfortunately, I get nothing from output and empty file.
This may have been because of the simple/crude EOF string detection code. I think it could have stopped prematurely. I've added more robust checking.
I've also added debug printing (if -d is given).
Because I don't have access to a real ttyUSB device, I've added test code using a PTY [pseudo TTY]. To use this, put the sample NMEA data into a file (e.g. input.txt) and add -pinput.txt to the arguments.
This was how I was able to debug the general program flow.
I've turned off any unnecessary fcntl options.
If, after trying this, you still have issues, you may wish to test your device interface with a terminal program (e.g. minicom) to verify that the remote device is, indeed, sending data.
If minicom produces output, but your program doesn't, you may have to modify some of the termios options.
Some usbtty/uart devices need RTS/CTS [I've actually used such a device for work]. minicom has a config option to deal with this.
In the program [although I suspect it's off by default], you may need to disable RTS/CTS hardware so that the port doesn't get hung up. And/or ensure that XON/XOFF flow control is disabled.
Or, the remote device needs RTS/CTS support [you have to somehow force the remote device to see CTS high]. Although unlikely, this might have to be done in the cable itself.
Anyway, here's the updated code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
#if 1
#include <poll.h>
#include <pty.h>
#include <sys/wait.h>
#include <time.h>
#endif
#ifndef RAWOUT
#define RAWOUT 1
#endif
void signal_handler_IO(); /* definition of signal handler */
const char *ttydev = "/dev/ttyUSB0";
int fd;
int opt_d; // 1=debug print
char *opt_pty; // PTY input file
int ptypid;
#define PTYSLP 1
FILE *fout = NULL;
struct termios termAttr;
struct sigaction saio;
struct pollfd fdpoll;
int linelen;
char linebuf[1000];
#define SHOWPOLL(_msk) \
if (events & _msk) \
bp += sprintf(bp,"/" #_msk)
typedef long long tsc_t;
tsc_t
tscget(void)
{
struct timespec ts;
tsc_t tsc;
static tsc_t tsczero = 0;
clock_gettime(CLOCK_REALTIME,&ts);
tsc = ts.tv_sec;
tsc *= 1000000000;
tsc += ts.tv_nsec;
if (tsczero == 0)
tsczero = tsc;
tsc -= tsczero;
return tsc;
}
double
tscsec(tsc_t tsc)
{
double sec;
sec = tsc;
sec /= 1e9;
return sec;
}
void
tscprt(void)
{
tsc_t tsc;
tsc = tscget();
printf("%.9f ",tscsec(tsc));
}
#define dbgprt(_fmt...) \
do { \
if (! opt_d) \
break; \
int sverr = errno; \
tscprt(); \
printf(_fmt); \
errno = sverr; \
} while (0)
// dopty -- generate pseudo TTY test device
void
dopty(void)
{
int fdm;
int fdtxt;
int rlen;
int wlen;
int off;
char buf[1000];
#if 0
fdm = open("/dev/pts/ptmx",O_RDWR | O_NDELAY);
#else
fdm = getpt();
#endif
if (fdm < 0) {
perror("dopty/open");
exit(1);
}
dbgprt("dopty: GETPT fdm=%d\n",fdm);
ttydev = ptsname(fdm);
dbgprt("dopty: PTSNAME ttydev=%s\n",ttydev);
grantpt(fdm);
unlockpt(fdm);
dbgprt("dopty: INPUT opt_pty=%s\n",opt_pty);
do {
ptypid = fork();
if (ptypid != 0) {
close(fdm);
break;
}
// open sample test data file
fdtxt = open(opt_pty,O_RDONLY);
if (fdtxt < 0) {
perror("dopty/open");
exit(1);
}
sleep(PTYSLP);
while (1) {
rlen = read(fdtxt,buf,sizeof(buf));
if (rlen <= 0)
break;
dbgprt("dopty: READ rlen=%d\n",rlen);
for (off = 0; off < rlen; off += wlen) {
wlen = rlen - off;
wlen = write(fdm,&buf[off],wlen);
dbgprt("dopty: WRITE wlen=%d\n",wlen);
}
}
sleep(PTYSLP);
dbgprt("dopty: CLOSE\n");
close(fdtxt);
close(fdm);
sleep(PTYSLP);
dbgprt("dopty: EXIT\n");
exit(0);
} while (0);
}
char *
showpoll(short events)
{
char *bp;
static char buf[1000];
bp = buf;
bp += sprintf(bp,"%4.4X",events);
SHOWPOLL(POLLIN);
SHOWPOLL(POLLPRI);
SHOWPOLL(POLLOUT);
SHOWPOLL(POLLRDHUP);
SHOWPOLL(POLLERR);
SHOWPOLL(POLLHUP);
return buf;
}
// lineadd -- add character to line buffer
void
lineadd(int chr)
{
char *bp;
char buf[10];
if (opt_d) {
bp = buf;
*bp = 0;
if ((chr >= 0x20) && (chr <= 0x7E))
bp += sprintf(bp," '%c'",chr);
dbgprt("showchr: CHR chr=%2.2X%s\n",chr,buf);
}
linebuf[linelen++] = chr;
linebuf[linelen] = 0;
}
// eoftst -- decide if current line is the last line
int
eoftst(void)
{
static char *eofstr = "$GP\n";
static int eoflen = 0;
int stopflg = 0;
if (eoflen == 0)
eoflen = strlen(eofstr);
stopflg = ((linelen == eoflen) && (memcmp(linebuf,eofstr,eoflen) == 0));
dbgprt("eoftst: %s\n",stopflg ? "STOP" : "CONT");
return stopflg;
}
int
main(int argc, char **argv)
{
int timout;
int buf_has_eof = 0;
int curchr;
int err;
int rlen;
int idx;
char buf[1000];
--argc;
++argv;
setlinebuf(stdout);
setlinebuf(stderr);
for (; argc > 0; --argc, ++argv) {
char *cp = *argv;
if (*cp != '-')
break;
cp += 2;
switch (cp[-1]) {
case 'd':
opt_d = ! opt_d;
break;
case 'p':
opt_pty = (*cp != 0) ? cp : "input.txt";
break;
}
}
do {
// create test device
if (opt_pty != NULL) {
dopty();
break;
}
if (argc > 0) {
ttydev = *argv;
--argc;
++argv;
}
} while (0);
dbgprt("main: TTYDEV ttydev=%s\n",ttydev);
fd = open(ttydev, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open port\n");
exit(1);
}
#if 0
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO, &saio, NULL);
#endif
// not needed unless doing signal handler
#if 0
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, O_ASYNC);
#endif
#if 1
tcgetattr(fd, &termAttr);
#endif
#if 1
cfsetispeed(&termAttr, B9600);
cfsetospeed(&termAttr, B9600);
#endif
// force immediate return from device read if no chars available
#if 1
dbgprt("main: CC VMIN=%d VTIME=%d\n",
termAttr.c_cc[VMIN],termAttr.c_cc[VTIME]);
termAttr.c_cc[VMIN] = 0;
termAttr.c_cc[VTIME] = 0;
#endif
termAttr.c_cflag &= ~PARENB;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
// FIXME -- you may need to handle this
#if 1
termAttr.c_cflag &= ~CRTSCTS;
#else
termAttr.c_cflag |= CRTSCTS;
#endif
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
#if 1
tcsetattr(fd, TCSANOW, &termAttr);
#endif
printf("UART1 configured....\n");
// open output file
fout = fopen("csvfile.csv","w");
if (fout == NULL) {
perror("main/fopen");
exit(1);
}
fdpoll.fd = fd;
fdpoll.events = POLLIN;
fdpoll.revents = 0;
// set initial timeout of 10 seconds
timout = 10000;
// NOTE: iter is just for testing to prevent infinite looping if failure to
// read or match the EOF string
for (int iter = 1; iter < 10; ++iter) {
dbgprt("main: POLL iter=%d events=%s timout=%d\n",
iter,showpoll(fdpoll.events),timout);
err = poll(&fdpoll,1,timout);
dbgprt("main: POLL revents=%s err=%d\n",showpoll(fdpoll.revents),err);
// timeout
if (err == 0)
break;
// error
if (err < 0) {
fprintf(stderr,"error -- %s\n",strerror(errno));
break;
}
// err will always be _one_ because poll's second arg is 1
// process all data in current chunk
while (1) {
rlen = read(fd,buf,sizeof(buf));
dbgprt("main: READ iter=%d rlen=%d\n",iter,rlen);
if (rlen <= 0)
break;
// send data to output file
#if RAWOUT
fwrite(buf,1,rlen,fout);
#endif
// need to check buf looking for last line (e.g. $GP)
// to know when to stop
// since read is _not_ line oriented we have to check for G followed
// by P [and they may or may not occur in the same read call]
// FIXME -- this is quite crude -- just to illustrate
for (idx = 0; idx < rlen; ++idx) {
curchr = buf[idx];
// add to line buffer
lineadd(curchr);
// wait for newline
if (curchr != '\n')
continue;
// decide if this is the last line of the current NMEA message
buf_has_eof = eoftst();
#if (! RAWOUT)
// do processing on line buffer ...
#endif
// reset line buffer index/length for next line
linelen = 0;
if (buf_has_eof)
break;
}
if (buf_has_eof)
break;
}
if (buf_has_eof)
break;
// set 1 second timeout for subsequent reads
timout = 1000;
#if 0
sleep(1);
#endif
}
close(fd);
fclose(fout);
// reap any child processes [only if doing PTY mode]
while (opt_pty != NULL) {
pid_t pid = wait(NULL);
dbgprt("main: WAIT pid=%d\n",pid);
if (pid <= 0)
break;
}
exit(0);
}
void
signal_handler_IO()
{
FILE *csv;
FILE *ff;
char buff[1024];
ff = fopen("/dev/ttyUSB0", "r");
// int n = read(fd, &buff, sizeof(buff));
fgets(buff, sizeof(buff), ff);
char *token = strtok(buff, ",");
csv = fopen("csvfile.csv", "w");
while (token != NULL) {
fprintf(csv, "%s\n", token);
token = strtok(NULL, ",");
}
sleep(0.2);
fclose(csv);
}
Related
I got a big trouble with BeagleboneBlack Uart
My beagleboneblack(let's call it "BBB") run 3.8.13-bone47, and I have been haunted by a bug for 2 weeks. My problem is :when my BBB gets a string,my BBB will send the same string to another terminal. For example:My laptop with a Uart2USB bridge sent a string "asd",then BBB will got "asd".But meanwhile, BBB would send "asd" to my laptop.And all of Uart module on BBB(Uart1,Uart2,Uart4) did the same thing. By the way,I tried to avoid this bug by using two Uart(one for TX,another one for RX).Fortunately,I did it,but I still want to know the reason and the solution. Here is my Uart.c,Uart.h and test.c: /*====================Uart.c================*/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <errno.h> #include <sys/time.h> #include <string.h> #include "Uart.h" #define TRUE 1 #define FALSE -1 int fd_uart; void set_speed(void) { int i; int status; int speed_arr = B38400; struct termios Opt; tcgetattr(fd_uart,&Opt); tcflush(fd_uart, TCIOFLUSH); cfsetispeed(&Opt, speed_arr); cfsetospeed(&Opt, speed_arr); status = tcsetattr(fd_uart, TCSANOW, &Opt); if(status != 0) perror("tcsetattr fd1"); } int set_Parity(void) { struct termios options; if( tcgetattr( fd_uart,&options)!= 0) { perror("SetupSerial 1"); return(FALSE); } options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_iflag &= ~INPCK; /* Enable parity checking */ options.c_iflag &= ~(ICRNL|IGNCR); options.c_lflag &= ~(ICANON ); options.c_cflag &= ~CSTOPB; options.c_iflag |= INPCK; options.c_cc[VTIME] = 150; // 15 seconds options.c_cc[VMIN] = 0; tcflush(fd_uart,TCIFLUSH); /* Update the options and do it NOW */ if(tcsetattr(fd_uart,TCSANOW,&options) != 0) { perror("SetupSerial 3"); return (FALSE); } return (TRUE); } void initUart(int argc, char **argv) { char devname_head[10] = "/dev/"; char dev_name[20]; if(argc < 2) { printf("Please input './test_uart ttyOx'\n"); exit(1); } else { strcpy(dev_name, devname_head); strcat(dev_name, argv[1]); fd_uart = open(dev_name, O_RDWR); if(fd_uart < 0) { perror("error to open /dev/ttyOx\n"); exit(1); } else if (fd_uart == 0) { printf("Can't Open Serial Port!\n"); exit(0); } else { //Setup set_speed(); //baud:38400 if (set_Parity() == FALSE) //8,1,n { printf("Set Parity Error\n"); exit(1); } } } } void writeUartString(char *buf) { if ( write(fd_uart,buf,strlen(buf)) < 0) { printf("write error\n"); } } void writeUartMsg(char *buf,int num) { if ( write(fd_uart,buf,num) < 0) { printf("write error\n"); } } int readUart(char *buf) { int num = 0, i = 0; num = read(fd_uart,buf,sizeof(buf)); if(num < 0){ printf("read error\n"); } return num; } void closeUart(void) { close(fd_uart); } /*======================================================*/ /*==================Uart.h===================*/ #ifndef UART_H #define UART_H void set_speed(void); int set_Parity(void); void initUart(int argc, char **argv); void writeUartString(char *buf); void writeUartMsg(char *buf,int num); void closeUart(void); extern int fd_uart; #endif /*=======================================*/ /*================test.c=================*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "Uart.h" int main(int argc, char **argv) { int i = 0,temp = 0; char buf[64] = {0}; initUart(argc,argv); while(1){ writeUartString("waiting\n"); printf("waiting\n"); memset(buf,0,64); temp = readUart(buf); printf("readUart's return value = %d\n",temp); if (temp > 0){ printf("get something\n"); printf("%s\n",buf); } sleep(1); } return 0; } /*====================================*/ Did somebody get this situation too?
You left echoing enableed with options.c_lflag &= ~(ICANON ); Change it, at least, to options.c_lflag &= ~(ICANON | ECHO ); With you are left at their original state all flags, rather then except that ICANON.
Serial port programming in Linux
I want to use RS232 port on the board to communicate with a PC. I understand that I can use "dev/ttyS0" for that purpose. I can open and write data to PC by using write() function. But the problem is I can't read from "dev/ttyS0". Every time I use read function, I get "Resource temporarily unavailable". Can u guys tell me how to solve this problem? Here is my code: #include <stdio.h> #include <string.h> #include <fcntl.h> #include <termios.h> #include <unistd.h> int main() { int n = 0, fd = 0, bytes = 0; char ch = 0; char buffer[10], *bufPtr; int nBytes = 0, tries = 0, x = 0; struct termios term; fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror("open"); return; } else { fcntl(fd, F_SETFL, 0); perror("Port"); } if (n = tcgetattr(fd, &term) == -1) { perror("tcgetattr"); return; } if (n = cfsetispeed(&term, B115200) == -1) { perror("cfsetispeed"); return; } if (n = cfsetospeed(&term, B115200) == -1) { perror("cfsetospeed"); return; } term.c_cflag |= (CLOCAL | CREAD); term.c_cflag &= ~PARENB; term.c_cflag &= ~CSTOPB; term.c_cflag &= ~CSIZE; term.c_cflag |= CS8; term.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); term.c_iflag &= ~(IXON | IXOFF | IXANY); term.c_cflag &= ~CRTSCTS; term.c_oflag &= ~OPOST; if (n = tcsetattr(fd, TCSANOW, &term) == -1) { perror("tcsetattr"); return; } write(fd,"LINUX",5); perror("write"); fcntl(fd, F_SETFL, FNDELAY); perror("fcntl"); bytes = read(fd, buffer, sizeof(buffer)); perror("read"); printf("Bytes : %d and data: %s\n", bytes, buffer); }
What is your test? Are you shortcutting pin 2 and 3 of your RS232 DB9 connector? Otherwise read will always return -1 if there are no data to be read. This is what your code is supposed to do using O_NDELAY flag opening the serial line. To avoid to read if there are no data to be read, you could use select In your case: struct timeval tv; fd_set rset; int retV; int timeout = 5000; // 5 seconds tv.tv_usec = (timeout * 1000) % 1000000; tv.tv_sec = timeout / 1000; FD_ZERO ( &rset ); FD_SET ( fd, &rset ); retV = select ( fd+1, &rset, NULL, NULL, &tv ); if( retV == 0 ) { // timeout stuff } else if( retV < 0 ) { // Error stuff. Read errno to see what happened } else { // read data } EDIT Remove fcntl(fd, F_SETFL, FNDELAY); If you want a normal blocking read, unset that flag. In your code you are also assuming that read will return all sent chars but is not true. read will return chars that are available to be read. This means that if you send "LINUX" a 5 times read could be requested to read all 5 chars sent. Last thing printf("Bytes : %d and data: %s\n", bytes, buffer); is Undefined Behavior because of buffer is not a NULL terminated string. You could solve it looping on received chars and print it with %c format, or NULL terminating your buffer with: if (bytes > 0) buffer[bytes] = '\0'; or char stringToSend[] = "LINUX"; size_t len = strlen(stringToSend) +1 ; write(fd,"LINUX", len); perror("write"); size_t receivedBytes = 0; bytes = 0; while (receivedBytes<len) { bytes = read(fd, &buffer[receivedBytes], sizeof(buffer)-1); perror("read"); if (bytes > 0) receivedBytes += bytes; } printf("Bytes : %d and data: %s\n", receivedBytes, buffer);
C program to read data from USB device connected to the system
I am trying to fetch data from the USB device (say pendrive) connected to the USB port of a system. Here, I am able to open the device file and read some random raw data. But I want to fetch data like minicom/teraterm. Please let me know what methods and libraries I can use to do it successfully and how can it be done. #include <stdio.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> #include <signal.h> #include <sys/time.h> int main() { short portfd=-1; int n,f,len; char buf[256],*s; alarm(2); #if defined(O_NDELAY) && defined(F_SETFL) portfd = open("/dev/ttyUSB0", O_RDWR|O_NDELAY); if (portfd >= 0){ /* Cancel the O_NDELAY flag. */ printf("port openend\n"); n = fcntl(portfd, F_GETFL, 0); (void) fcntl(portfd, F_SETFL, n & ~O_NDELAY); } #else portfd = open("/dev/ttyUSB0", O_RDWR); #endif if (portfd >= 0) { if (len == 0) len = strlen(s); for(f = 0; f < len && f <100; f++) buf[f] = *s++ | 0x80; write(portfd, buf, f); printf("Do write\n"); while(portfd>=0){ printf("%s\n",buf); } } alarm(0); signal(SIGALRM, SIG_IGN); if (portfd < 0) { printf("cannot open %s. Sorry.\n", "/dev/ttyUSB0"); } } Log of the output: ���������鉀�������������������鍀���������������������������������������������������������������2 ����������鉀�������������������鍀���������������������������������������������������������������2
you will need to set the correct port configuration... struct termios oldtio,newtio; // open port... // save existing attributes tcgetattr(fd,&oldtio); // set attributes - these flags may change for your device #define BAUDRATE B9600 memset(&newtio, 0x00, sizeof(newtio)); newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; newtio.c_iflag = IGNPAR | ICRNL; newtio.c_oflag = 0; tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); //reset attributes tcsetattr(fd,TCSANOW,&oldtio); I have a rough working example here... http://file-hub.com/cmd:thread/142300
Writing AT commands embedded linux
I am having issues writing AT commands to a GSM module. It works flawless when i use minicom -b 115200 -D /dev/ttySP0 --term=vt100 But i cant figure out how to do the same thing in C code. I do not receive any errors, but the module does no react to the commands. Anyone know what could be wrong? #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> #define BAUDRATE B115200 #define COM1 "/dev/ttySP0" static int fd; static struct termios oldtio,newtio; //============================================================== int tty_read(char *buf,int nbytes) { int temp; temp = read(fd,buf,nbytes); printf("Read string: %s\n", buf); return temp; } //============================================================== int tty_end() { tcsetattr(fd,TCSANOW,&oldtio); close(fd); } //============================================================== int tty_writecmd(char *buf,int nbytes) { int i; for(i=0; i<nbytes; i++) { write(fd,&buf[i],1); usleep(100); } write(fd,"\n",1); //Tried \0 \r aswell usleep(300000); return tcdrain(fd); } //============================================================== int baud = B115200; int tty_init() { fd = open(COM1, O_RDWR ); if (fd <0) { perror(COM1); exit(1); } tcgetattr(fd,&oldtio); bzero(&newtio, sizeof(newtio)); newtio.c_cflag = baud | CRTSCTS | CS8 | CLOCAL | CREAD ; newtio.c_iflag = IGNPAR | ICRNL; newtio.c_oflag = 0; newtio.c_lflag = ICANON; newtio.c_cc[VINTR] = 0; newtio.c_cc[VQUIT] = 0; newtio.c_cc[VERASE] = 0; newtio.c_cc[VKILL] = 0; newtio.c_cc[VEOF] = 4; newtio.c_cc[VTIME] = 0; newtio.c_cc[VMIN] = 1; newtio.c_cc[VSWTC] = 0; newtio.c_cc[VSTART] = 0; newtio.c_cc[VSTOP] = 0; newtio.c_cc[VSUSP] = 0; newtio.c_cc[VEOL] = 0; newtio.c_cc[VREPRINT] = 0; newtio.c_cc[VDISCARD] = 0; newtio.c_cc[VWERASE] = 0; newtio.c_cc[VLNEXT] = 0; newtio.c_cc[VEOL2] = 0; tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); return 0; } int main(int argc, char *argv[]) { char recv[10]; char command[] = "AT+CSQ"; tty_init(); printf("Write: %d\n", tty_writecmd(command, sizeof(command))); usleep(40000); printf("Read: %d\n", tty_read(recv ,sizeof(recv))); tty_end(); }
The first thing you should do is to run stty -F /dev/ttySP0 Do this while minicom is running and while your program is running. Check everything and compare. There are lots of things that can cause you issues. Once you have those matching, you want to make sure the data you send is going out. cat /proc/tty/driver/serial Compare the tx value before and after you send data to make sure it is going out. If it is, then you can check the rx value. If you get no response, you are probably going to need an oscilloscope to inspect the data on the lines. If you can't do this, then triple check the baud rate and flow control.
Simple, small C program for testing serial bandwidth
I have two laptops with a serial port. How do I test the actual bandwidth of the serial port between the two machines using a simple, small C program? In reality, I need to do this on an embedded Linux system which is why the utility must be a small, simple C program (because the embedded environment only has limited library support meaning it doesn't have python, perl, or any other fancy libraries).
I started this as a new question because I didn't want to take this question off topic: Serial port loopback test That question was regarding testing the bandwidth of a serial port in loopback mode, so that you don't have to plug in an actual serial cable. The author (sdaau) put a lot of time into creating a multi-threaded serial bandwidth test program to answer his own question. I then used his simple C program and extended it to be used between two different physical machines connected with a serial cable. It is necessary to start the "remote" side which will wait for the "initiator" side (the local side) to send a go byte, in which both will proceed to transfer data asynchronously. The program (which sdaau calls writeread.c) spawns 2 threads: one which writes data and the other which reads data. In this way, you are fully utilizing the serial port. You can pass in a datafile as a command-line argument. As an example, here is the "remote" side: ./writeread /dev/ttyUSB0 115200 ./datafile.dat 3> output As an example, here is the "local" (or initiator) side: ./writeread /dev/ttyUSB0 115200 ./datafile.dat -I 3> output Note that I had some trouble redirecting the output on the remote side, meaning that 3> output didn't really work. I'm not sure whats going on with that, but my local side worked fine. Also, note the remote side's output timing is skewed because it has a timer running while it is waiting for the initiator. This means you should only trust the bandwidth printout from the local initiator side (see the original question for output results details). Since both sides are sending the same datafile in this example, you should be able to compare the "output" file with the datafile: diff output datafile.dat Complie the code with: gcc -c -Wall writeread.c gcc writeread.o -lpthread -o writeread Here is the modified writeread.c code: /* writeread.c - based on writeread.cpp [SOLVED] Serial Programming, Write-Read Issue - http://www.linuxquestions.org/questions/programming-9/serial-programming-write-read-issue-822980/ build with: gcc -o writeread -lpthread -Wall -g writeread.c */ #include <stdio.h> #include <string.h> #include <stddef.h> #include <stdlib.h> #include <sys/time.h> #include <pthread.h> #include "writeread.h" int serport_fd; //POSIX Threads Programming - https://computing.llnl.gov/tutorials/pthreads/#PassingArguments struct write_thread_data{ int fd; char* comm; //string to send int bytesToSend; int writtenBytes; int iator; // Initiator. 0 = False, 1 = True }; void usage(char **argv) { fprintf(stdout, "Usage:\n"); fprintf(stdout, "%s port baudrate file/string [-I]\n", argv[0]); fprintf(stdout, " The -I is for initiator. Run on the remote side which " "will wait, then start locally with -I which will initiate " "the test.\n"); fprintf(stdout, "Examples:\n"); fprintf(stdout, "%s /dev/ttyUSB0 115200 /path/to/somefile.txt\n", argv[0]); fprintf(stdout, "%s /dev/ttyUSB0 115200 \"some text test\"\n", argv[0]); } // POSIX threads explained - http://www.ibm.com/developerworks/library/l-posix1.html // instead of writeport void *write_thread_function(void *arg) { int go = 0; int lastBytesWritten; struct write_thread_data *my_data; my_data = (struct write_thread_data *) arg; fprintf(stdout, "write_thread_function spawned\n"); // Are we the initiator? if (my_data->iator == 1) { // We are the initiator, send the start command go = 0xde; write(my_data->fd, &go, 1); } else { // We wait for the initiator to send us the start command fprintf(stdout, "Waiting for initiator (start other end with -I)...\n"); read(my_data->fd, &go, 1); if (go == 0xde) { fprintf(stdout, "Go!\n"); } else { fprintf(stdout, "Error: Did not receive start command [0x%x]\n", go); return NULL; } } my_data->writtenBytes = 0; while(my_data->writtenBytes < my_data->bytesToSend) { lastBytesWritten = write( my_data->fd, my_data->comm + my_data->writtenBytes, my_data->bytesToSend - my_data->writtenBytes ); my_data->writtenBytes += lastBytesWritten; if ( lastBytesWritten < 0 ) { fprintf(stdout, "write failed!\n"); return 0; } fprintf(stderr, " write: %d - %d\n", lastBytesWritten, my_data->writtenBytes); } return NULL; //pthread_exit(NULL) } int main( int argc, char **argv ) { if( argc < 4 ) { usage(argv); return 1; } char *serport; char *serspeed; speed_t serspeed_t; char *serfstr; int serf_fd; // if < 0, then serfstr is a string int sentBytes; int readChars; int recdBytes, totlBytes; char* sResp; char* sRespTotal; struct timeval timeStart, timeEnd, timeDelta; float deltasec, expectBps, measReadBps, measWriteBps; struct write_thread_data wrdata; pthread_t myWriteThread; /* Re: connecting alternative output stream to terminal - * http://coding.derkeiler.com/Archive/C_CPP/comp.lang.c/2009-01/msg01616.html * send read output to file descriptor 3 if open, * else just send to stdout */ FILE *stdalt; if(dup2(3, 3) == -1) { fprintf(stdout, "stdalt not opened; "); stdalt = fopen("/dev/tty", "w"); } else { fprintf(stdout, "stdalt opened; "); stdalt = fdopen(3, "w"); } fprintf(stdout, "Alternative file descriptor: %d\n", fileno(stdalt)); // Get the PORT name serport = argv[1]; fprintf(stdout, "Opening port %s;\n", serport); // Get the baudrate serspeed = argv[2]; serspeed_t = string_to_baud(serspeed); fprintf(stdout, "Got speed %s (%d/0x%x);\n", serspeed, serspeed_t, serspeed_t); //Get file or command; serfstr = argv[3]; // Are we the initiator? if (argc == 5 && strncmp(argv[4], "-I", 3) == 0 ) { wrdata.iator = 1; // Initiator. 0 = False, 1 = True } else { wrdata.iator = 0; // Initiator. 0 = False, 1 = True } serf_fd = open( serfstr, O_RDONLY ); fprintf(stdout, "Got file/string '%s'; ", serfstr); if (serf_fd < 0) { wrdata.bytesToSend = strlen(serfstr); wrdata.comm = serfstr; //pointer already defined fprintf(stdout, "interpreting as string (%d).\n", wrdata.bytesToSend); } else { struct stat st; stat(serfstr, &st); wrdata.bytesToSend = st.st_size; wrdata.comm = (char *)calloc(wrdata.bytesToSend, sizeof(char)); read(serf_fd, wrdata.comm, wrdata.bytesToSend); fprintf(stdout, "opened as file (%d).\n", wrdata.bytesToSend); } sResp = (char *)calloc(wrdata.bytesToSend, sizeof(char)); sRespTotal = (char *)calloc(wrdata.bytesToSend, sizeof(char)); // Open and Initialise port serport_fd = open( serport, O_RDWR | O_NOCTTY | O_NONBLOCK ); if ( serport_fd < 0 ) { perror(serport); return 1; } initport( serport_fd, serspeed_t ); wrdata.fd = serport_fd; sentBytes = 0; recdBytes = 0; gettimeofday( &timeStart, NULL ); // start the thread for writing.. if ( pthread_create( &myWriteThread, NULL, write_thread_function, (void *) &wrdata) ) { printf("error creating thread."); abort(); } // run read loop while ( recdBytes < wrdata.bytesToSend ) { while ( wait_flag == TRUE ); if ( (readChars = read( serport_fd, sResp, wrdata.bytesToSend)) >= 0 ) { //~ fprintf(stdout, "InVAL: (%d) %s\n", readChars, sResp); // binary safe - add sResp chunk to sRespTotal memmove(sRespTotal+recdBytes, sResp+0, readChars*sizeof(char)); /* // text safe, but not binary: sResp[readChars] = '\0'; fprintf(stdalt, "%s", sResp); */ recdBytes += readChars; } else { if ( errno == EAGAIN ) { fprintf(stdout, "SERIAL EAGAIN ERROR\n"); return 0; } else { fprintf(stdout, "SERIAL read error: %d = %s\n", errno , strerror(errno)); return 0; } } fprintf(stderr, " read: %d\n", recdBytes); wait_flag = TRUE; // was == //~ usleep(50000); } if ( pthread_join ( myWriteThread, NULL ) ) { printf("error joining thread."); abort(); } gettimeofday( &timeEnd, NULL ); // binary safe - dump sRespTotal to stdalt fwrite(sRespTotal, sizeof(char), recdBytes, stdalt); // Close the open port close( serport_fd ); if (!(serf_fd < 0)) { close( serf_fd ); free(wrdata.comm); } free(sResp); free(sRespTotal); fprintf(stdout, "\n+++DONE+++\n"); sentBytes = wrdata.writtenBytes; totlBytes = sentBytes + recdBytes; timeval_subtract(&timeDelta, &timeEnd, &timeStart); deltasec = timeDelta.tv_sec+timeDelta.tv_usec*1e-6; expectBps = atoi(serspeed)/10.0f; measWriteBps = sentBytes/deltasec; measReadBps = recdBytes/deltasec; fprintf(stdout, "Wrote: %d bytes; Read: %d bytes; Total: %d bytes. \n", sentBytes, recdBytes, totlBytes); fprintf(stdout, "Start: %ld s %ld us; End: %ld s %ld us; Delta: %ld s %ld us. \n", timeStart.tv_sec, timeStart.tv_usec, timeEnd.tv_sec, timeEnd.tv_usec, timeDelta.tv_sec, timeDelta.tv_usec); fprintf(stdout, "%s baud for 8N1 is %d Bps (bytes/sec).\n", serspeed, (int)expectBps); fprintf(stdout, "Measured: write %.02f Bps (%.02f%%), read %.02f Bps (%.02f%%), total %.02f Bps.\n", measWriteBps, (measWriteBps/expectBps)*100, measReadBps, (measReadBps/expectBps)*100, totlBytes/deltasec); return 0; } And here is the .h file which I have renamed from the original question to writeread.h: /* writeread.h (C) 2004-5 Captain http://www.captain.at Helper functions for "ser" Used for testing the PIC-MMC test-board http://www.captain.at/electronic-index.php */ #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 */ #include <sys/signal.h> #include <sys/stat.h> #include <sys/types.h> #define TRUE 1 #define FALSE 0 int wait_flag = TRUE; // TRUE while no signal received // Definition of Signal Handler void DAQ_signal_handler_IO ( int status ) { //~ fprintf(stdout, "received SIGIO signal %d.\n", status); wait_flag = FALSE; } int writeport( int fd, char *comm ) { int len = strlen( comm ); int n = write( fd, comm, len ); if ( n < 0 ) { fprintf(stdout, "write failed!\n"); return 0; } return n; } int readport( int fd, char *resp, size_t nbyte ) { int iIn = read( fd, resp, nbyte ); if ( iIn < 0 ) { if ( errno == EAGAIN ) { fprintf(stdout, "SERIAL EAGAIN ERROR\n"); return 0; } else { fprintf(stdout, "SERIAL read error: %d = %s\n", errno , strerror(errno)); return 0; } } if ( resp[iIn-1] == '\r' ) resp[iIn-1] = '\0'; else resp[iIn] = '\0'; return iIn; } int getbaud( int fd ) { struct termios termAttr; int inputSpeed = -1; speed_t baudRate; tcgetattr( fd, &termAttr ); // Get the input speed baudRate = cfgetispeed( &termAttr ); switch ( baudRate ) { case B0: inputSpeed = 0; break; case B50: inputSpeed = 50; break; case B110: inputSpeed = 110; break; case B134: inputSpeed = 134; break; case B150: inputSpeed = 150; break; case B200: inputSpeed = 200; break; case B300: inputSpeed = 300; break; case B600: inputSpeed = 600; break; case B1200: inputSpeed = 1200; break; case B1800: inputSpeed = 1800; break; case B2400: inputSpeed = 2400; break; case B4800: inputSpeed = 4800; break; case B9600: inputSpeed = 9600; break; case B19200: inputSpeed = 19200; break; case B38400: inputSpeed = 38400; break; case B115200: inputSpeed = 115200; break; case B2000000: inputSpeed = 2000000; break; //added } return inputSpeed; } /* ser.c (C) 2004-5 Captain http://www.captain.at Sends 3 characters (ABC) via the serial port (/dev/ttyS0) and reads them back if they are returned from the PIC. Used for testing the PIC-MMC test-board http://www.captain.at/electronic-index.php */ int initport( int fd, speed_t baudRate ) { struct termios options; struct sigaction saio; // Definition of Signal action // Install the signal handler before making the device asynchronous saio.sa_handler = DAQ_signal_handler_IO; saio.sa_flags = 0; saio.sa_restorer = NULL; sigaction( SIGIO, &saio, NULL ); // Allow the process to receive SIGIO fcntl( fd, F_SETOWN, getpid() ); // Make the file descriptor asynchronous (the manual page says only // O_APPEND and O_NONBLOCK, will work with F_SETFL...) fcntl( fd, F_SETFL, FASYNC ); //~ fcntl( fd, F_SETFL, FNDELAY); //doesn't work; //fcntl(file, F_SETFL, 0); // Get the current options for the port... tcgetattr( fd, &options ); /* // Set port settings for canonical input processing options.c_cflag = BAUDRATE | CRTSCTS | CLOCAL | CREAD; options.c_iflag = IGNPAR | ICRNL; //options.c_iflag = IGNPAR; options.c_oflag = 0; options.c_lflag = ICANON; //options.c_lflag = 0; options.c_cc[VMIN] = 0; options.c_cc[VTIME] = 0; */ /* ADDED - else 'read' will not return, unless it sees LF '\n' !!!! * From: Unix Programming Frequently Asked Questions - 3. Terminal I/O - * http://www.steve.org.uk/Reference/Unix/faq_4.html */ /* Disable canonical mode, and set buffer size to 1 byte */ options.c_lflag &= (~ICANON); options.c_cc[VTIME] = 0; options.c_cc[VMIN] = 1; // Set the baud rates to... cfsetispeed( &options, baudRate ); cfsetospeed( &options, baudRate ); // Enable the receiver and set local mode... options.c_cflag |= ( CLOCAL | CREAD ); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; // Flush the input & output... tcflush( fd, TCIOFLUSH ); // Set the new options for the port... tcsetattr( fd, TCSANOW, &options ); return 1; } /* ripped from http://git.savannah.gnu.org/cgit/coreutils.git/tree/src/stty.c */ #define STREQ(a, b) (strcmp((a), (b)) == 0) struct speed_map { const char *string; /* ASCII representation. */ speed_t speed; /* Internal form. */ unsigned long int value; /* Numeric value. */ }; static struct speed_map const speeds[] = { {"0", B0, 0}, {"50", B50, 50}, {"75", B75, 75}, {"110", B110, 110}, {"134", B134, 134}, {"134.5", B134, 134}, {"150", B150, 150}, {"200", B200, 200}, {"300", B300, 300}, {"600", B600, 600}, {"1200", B1200, 1200}, {"1800", B1800, 1800}, {"2400", B2400, 2400}, {"4800", B4800, 4800}, {"9600", B9600, 9600}, {"19200", B19200, 19200}, {"38400", B38400, 38400}, {"exta", B19200, 19200}, {"extb", B38400, 38400}, #ifdef B57600 {"57600", B57600, 57600}, #endif #ifdef B115200 {"115200", B115200, 115200}, #endif #ifdef B230400 {"230400", B230400, 230400}, #endif #ifdef B460800 {"460800", B460800, 460800}, #endif #ifdef B500000 {"500000", B500000, 500000}, #endif #ifdef B576000 {"576000", B576000, 576000}, #endif #ifdef B921600 {"921600", B921600, 921600}, #endif #ifdef B1000000 {"1000000", B1000000, 1000000}, #endif #ifdef B1152000 {"1152000", B1152000, 1152000}, #endif #ifdef B1500000 {"1500000", B1500000, 1500000}, #endif #ifdef B2000000 {"2000000", B2000000, 2000000}, #endif #ifdef B2500000 {"2500000", B2500000, 2500000}, #endif #ifdef B3000000 {"3000000", B3000000, 3000000}, #endif #ifdef B3500000 {"3500000", B3500000, 3500000}, #endif #ifdef B4000000 {"4000000", B4000000, 4000000}, #endif {NULL, 0, 0} }; static speed_t string_to_baud (const char *arg) { int i; for (i = 0; speeds[i].string != NULL; ++i) if (STREQ (arg, speeds[i].string)) return speeds[i].speed; return (speed_t) -1; } /* http://www.gnu.org/software/libtool/manual/libc/Elapsed-Time.html Subtract the `struct timeval' values X and Y, storing the result in RESULT. Return 1 if the difference is negative, otherwise 0. */ int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) { /* Perform the carry for the later subtraction by updating y. */ if (x->tv_usec < y->tv_usec) { int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1; y->tv_usec -= 1000000 * nsec; y->tv_sec += nsec; } if (x->tv_usec - y->tv_usec > 1000000) { int nsec = (x->tv_usec - y->tv_usec) / 1000000; y->tv_usec += 1000000 * nsec; y->tv_sec -= nsec; } /* Compute the time remaining to wait. tv_usec is certainly positive. */ result->tv_sec = x->tv_sec - y->tv_sec; result->tv_usec = x->tv_usec - y->tv_usec; /* Return 1 if result is negative. */ return x->tv_sec < y->tv_sec; }