-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialPort.cpp
62 lines (48 loc) · 1.3 KB
/
SerialPort.cpp
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
#include "SerialPort.h"
extern int SerialFileStream = -1;
int SerialOpen()
{
char *DeviceName = "/dev/ttyAMA3";
SerialFileStream = open(DeviceName, O_RDWR | O_NOCTTY | O_NDELAY);
if (fcntl(SerialFileStream, F_SETFL, O_NONBLOCK) < 0)return false;
struct termios options;
tcgetattr(SerialFileStream, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; //<Set baud rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(SerialFileStream, TCIFLUSH);
tcsetattr(SerialFileStream, TCSANOW, &options);
return SerialFileStream;
}
bool SerialWriteLine(char inArray[]){
int i = 0;
while(inArray[i]!=0)
{
WaitFdWriteable(SerialFileStream);
if (write(SerialFileStream, &inArray[i], 1) < 0)return false;
i++;
}
return true;
}
unsigned char* SerialReadLine(){
unsigned char* chr = new unsigned char[30];
int i = 1;
/*while (chr[i-1]!='\n')
{
if(read(SerialFileStream, &chr[i], 1)>=1)i++;
}*/
chr[1]='P';
chr[2]='d';
return chr;
}
void WaitFdWriteable(int Fd)
{
fd_set WriteSetFD;
FD_ZERO(&WriteSetFD);
FD_SET(Fd, &WriteSetFD);
if (select(Fd + 1, NULL, &WriteSetFD, NULL, NULL) < 0) { }
}
void SerialClose(){
close(SerialFileStream);
}