1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| #include <iostream>
using namespace std;
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#define BAUDRATE 57600
#define MODEMDEVICE "/dev/ttyUSB0"
int main()
{
int fd;
struct termios newtio;
struct sigaction saio; /* definition of signal action */
char buf[4096];
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY |O_NDELAY);
if (fd <0) {
printf("couldn't open \n");
perror(MODEMDEVICE);
}//exit(-1); }
/* install the signal handler before making the device asynchronous */
/* __sigset_t t;
saio.sa_handler = signal_handler_IO;
saio.sa_mask = t;
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);
//bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
//newtio.c_iflag = IGNPAR;
//newtio.c_oflag = 0;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 5 chars received */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
while(1)
{
int res = read(fd,buf,4096);
buf[res] = 0;
for(int i =0; i<res; i++)
{
printf(" %d " , buf[i]);
}
if(res>0)
printf(" res = %d \n",res);
}
return 0;
} |
Partager