-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
77 lines (72 loc) · 2.48 KB
/
Copy pathmain.cpp
File metadata and controls
77 lines (72 loc) · 2.48 KB
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
/* after start send data to USB serial port device, the watchdog is enabled, then you need remain send data each second
in case of USB disconnect/reconnect, will try reopen the port, if not success in time, the watchdog will press the ATX header reset button */
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int set_interface_attribs(int fd, int speed, int parity)
{
struct termios tty;
if (tcgetattr (fd, &tty) != 0)
{
printf("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf("error %d from tcsetattr", errno);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
if(argc!=2)
{
std::cerr << "arg is serial port /dev/ttyUSB0" << std::endl;
return -1;
}
int fd=-1;
do
{
if(fd==-1)
{
fd=open(argv[1],O_RDWR|O_NOCTTY|O_SYNC);
if(fd<0)
std::cerr << "error " << errno << " opening " << argv[1] << std::endl;
else
set_interface_attribs(fd,B115200,0);
}
if(fd!=-1)
{
char var='1';
const int r=write(fd,(char *)&var,1);
if(r!=1)
{
std::cerr << "error writing, errno: " << errno << std::endl;
::close(fd);
fd=-1;
}
}
sleep(1);
} while(true);
return 0;
}