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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <error.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int fd, result;
char buffer[255];
struct termios options, storedOptions;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if(!fd)
{
perror("ERROR (device not opened) ");
exit(EXIT_FAILURE);
}
if(fcntl(fd, F_SETFL, FNDELAY) == -1)
{
perror("ERROR (device problem) ");
exit(EXIT_FAILURE);
}
if(tcgetattr(fd, &options) == -1)
{
perror("ERROR (port configuration problem) ");
exit(EXIT_FAILURE);
}
bzero(&options, sizeof(options));
// baudrate
if(cfsetispeed(&options, B4800) == -1 || cfsetospeed(&options, B4800) == -1)
{
perror("ERROR (baudrate setting) ");
exit(EXIT_FAILURE);
}
// line oriented mode
// (notice : be careful to desactivate ECHO before sending something to the devive)
options.c_lflag &= ~ECHONL;
options.c_lflag &= ~NOFLSH;
options.c_lflag &= ~ECHOPRT;
options.c_lflag &= ~TOSTOP;
options.c_lflag &= ~XCASE;
options.c_lflag |= ICANON;
options.c_lflag |= ISIG;
options.c_lflag |= IEXTEN;
options.c_lflag |= ECHO;
options.c_lflag |= ECHOE;
options.c_lflag |= ECHOK;
options.c_lflag |= ECHOKE;
options.c_lflag |= ECHOCTL;
options.c_cflag &= ~PARENB;
options.c_cflag &= ~PARODD;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= CS8;
options.c_cflag |= HUPCL;
options.c_cflag |= CREAD;
options.c_cflag |= CLOCAL;
options.c_iflag &= ~IGNBRK;
options.c_iflag &= ~BRKINT;
options.c_iflag &= ~IGNPAR;
options.c_iflag &= ~PARMRK;
options.c_iflag &= ~INPCK;
options.c_iflag &= ~ISTRIP;
options.c_iflag &= ~INLCR;
options.c_iflag &= ~IGNCR;
options.c_iflag &= ~IXOFF;
options.c_iflag &= ~IUCLC;
options.c_iflag &= ~IXANY;
options.c_iflag &= ~IMAXBEL;
options.c_iflag &= ~IUTF8;
options.c_iflag |= ICRNL;
options.c_iflag |= IXON;
options.c_oflag &= ~OLCUC;
options.c_oflag &= ~OCRNL;
options.c_oflag &= ~ONOCR;
options.c_oflag &= ~ONLRET;
options.c_oflag &= ~OFILL;
options.c_oflag &= ~OFDEL;
options.c_oflag |= OPOST;
options.c_oflag |= ONLCR;
options.c_oflag |= NL0;
options.c_oflag |= CR0;
options.c_oflag |= TAB0;
options.c_oflag |= BS0;
options.c_oflag |= VT0;
options.c_oflag |= FF0;
/* character setting */
options.c_cc[VINTR]= 3; // "^C"
options.c_cc[VQUIT]= 28; // "^\"
options.c_cc[VERASE] = 127; // "^?"
options.c_cc[VKILL]= 21; // ^U
options.c_cc[VEOF]= 4; // ^D
options.c_cc[VSTART]= 17; // ^Q
options.c_cc[VSTOP]= 19; // ^S
options.c_cc[VSUSP]= 26; // ^Z
options.c_cc[VREPRINT]= 18; // ^R
options.c_cc[VWERASE]= 23; // ^W
options.c_cc[VLNEXT]= 22; // ^V
options.c_cc[VMIN]=1;
options.c_cc[VTIME]=0;
if(tcsetattr(fd, TCSANOW, &options) == -1)
{
perror("ERROR (port configuration setting) ");
exit(EXIT_FAILURE);
}
int nodataCnt = 0;
while(nodataCnt < 200)
{
result = read(fd, buffer, sizeof(buffer) - 1);
// read fail
if(result == -1)
{
perror("ERROR (read process) ");
exit(EXIT_FAILURE);
}
// no data
if(!result)
{
printf("no data\n");
nodataCnt++;
}
// data received
if(result > 0)
{
buffer[result] = 0;
printf("string : %s number : %d\n", buffer, result);
}
}
tcsetattr(fd, TCSANOW, &storedOptions);
close(fd);
return EXIT_SUCCESS;
} |
Partager