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
| #ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int srl_handle;
struct termios options;
void mode_raw(int activer)
{
struct termios raw;
static int raw_actif=0;
static struct termios cooked;
if (raw_actif == activer) return;
if (activer)
{
tcgetattr(STDIN_FILENO, &cooked);
raw = cooked;
cfmakeraw(&raw);
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
printf("Raw mode activated\r\n");
}
else
{
tcsetattr(STDIN_FILENO, TCSANOW, &cooked);
printf("Raw mode deactivated\r\n");
}
raw_actif = activer;
}
int main(int argc, char *argv[])
{
char c=0;
char s[2]="\0";
char* buf=0;
mode_raw(1);
/*Open serial port*/
srl_handle=open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(srl_handle<0)
{
perror("serial port open");
exit(-1);
}
fcntl(srl_handle, F_SETFL, FNDELAY);
tcgetattr(srl_handle, &options);
/*Set baud rate*/
cfsetospeed(&options, B19200);
cfsetispeed(&options, B19200);
options.c_cflag |= (CLOCAL | CREAD);
/*Set no parity*/
options.c_cflag |= PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag &= ~PARODD;
options.c_cflag |= CS7;
options.c_cflag &= ~CRTSCTS;
options.c_iflag &= ~(IXON|IXOFF|IXANY);
/*Set raw output*/
tcsetattr(srl_handle, TCSANOW, &options);
/*Write Data*/
while ((c=fgetc(stdin))!='x')
{
printf("%c\t",c);
s[0] = c;
s[1] = '\0';
printf("%s\r\n",s);
if (write(srl_handle, s, 1) < 0)
fputs("write() failed!\r\n", stderr);
}
mode_raw(0);
// fcntl(srl_handle, F_SETFL, 0);
// while (strstr(buf,"OK")==NULL)
// {
if (read(srl_handle, buf, 10) < 0)
fputs("read() failed!\r\n", stderr);
printf("%s\n",buf);
// }
close(srl_handle);
// mode_raw(0);
return EXIT_SUCCESS;
} |