-
Notifications
You must be signed in to change notification settings - Fork 21
/
com.c
244 lines (229 loc) · 7.05 KB
/
com.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifdef __MINGW32__
#include <windows.h>
#include <winbase.h>
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux)
#include <sys/types.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#ifdef __MINGW32__
static HANDLE hSerial;
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux)
static int fd;
#endif
static uint32_t COM_Baudrate = 115200;
/** \brief Open COM port with settings
*
* \param [in] port Port name as string
* \param [in] baudrate Port baudrate
* \param [in] have_parity true if parity should be switched on
* \param [in] two_stopbits true if 2 stop bits used
* \return true if succeed
*
*/
bool COM_Open(char *port, uint32_t baudrate, bool have_parity, bool two_stopbits)
{
printf("Opening %s at %u baud\n", port, baudrate);
COM_Baudrate = baudrate;
#ifdef __MINGW32__
char str[64];
uint8_t multiplier;
sprintf(str, "\\\\.\\%s", port);
hSerial = CreateFile(str, GENERIC_READ | GENERIC_WRITE, 0,
NULL, OPEN_EXISTING, 0, NULL);
if (hSerial == INVALID_HANDLE_VALUE)
return false;
DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
GetCommState(hSerial, &dcbSerialParams);
dcbSerialParams.BaudRate = baudrate; // Setting BaudRate
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
if (two_stopbits == true)
dcbSerialParams.StopBits = TWOSTOPBITS;
else
dcbSerialParams.StopBits = ONESTOPBIT;
if (have_parity == true)
dcbSerialParams.Parity = EVENPARITY; // Setting Parity = None
else
dcbSerialParams.Parity = NOPARITY;
dcbSerialParams.fDtrControl = DTR_CONTROL_DISABLE;
SetCommState(hSerial, &dcbSerialParams);
COMMTIMEOUTS timeouts;
multiplier = (uint8_t)ceil((float)100000 / baudrate);
timeouts.ReadIntervalTimeout = 20 * multiplier;
timeouts.ReadTotalTimeoutMultiplier = 1 * multiplier;
timeouts.ReadTotalTimeoutConstant = 100 * multiplier;
timeouts.WriteTotalTimeoutMultiplier = 1;
timeouts.WriteTotalTimeoutConstant = 1;
SetCommTimeouts(hSerial, &timeouts);
//COM_Bytes = 0;
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux)
fd = open(port, O_RDWR | O_NOCTTY );
if (fd <0)
return false;
struct termios SerialPortSettings;
tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
/* Setting the Baud rate */
switch (baudrate)
{
case 300:
cfsetispeed(&SerialPortSettings, B300);
cfsetospeed(&SerialPortSettings, B300);
break;
case 9600:
cfsetispeed(&SerialPortSettings, B9600);
cfsetospeed(&SerialPortSettings, B9600);
break;
case 38400:
cfsetispeed(&SerialPortSettings, B38400);
cfsetospeed(&SerialPortSettings, B38400);
break;
case 57600:
cfsetispeed(&SerialPortSettings, B57600);
cfsetospeed(&SerialPortSettings, B57600);
break;
default:
case 115200:
cfsetispeed(&SerialPortSettings, B115200);
cfsetospeed(&SerialPortSettings, B115200);
break;
case 230400:
cfsetispeed(&SerialPortSettings, B230400);
cfsetospeed(&SerialPortSettings, B230400);
break;
}
cfmakeraw(&SerialPortSettings); /* Set raw mode (special processing disabled) */
if (have_parity == true)
SerialPortSettings.c_cflag |= PARENB; /* Enables the Parity Enable bit(PARENB) */
else
SerialPortSettings.c_cflag &= ~PARENB; /* Disables the Parity Enable bit(PARENB), so No Parity */
if (two_stopbits == true)
SerialPortSettings.c_cflag |= CSTOPB; /* CSTOPB = 2 Stop bits */
else
SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
SerialPortSettings.c_cflag |= (CREAD | CLOCAL); /* Enable receiver,Ignore Modem Control lines */
SerialPortSettings.c_cc[VMIN] = 0; // read doesn't block
SerialPortSettings.c_cc[VTIME] = 5; // 0.1 seconds read timeout
tcsetattr(fd, TCSANOW, &SerialPortSettings); /* Set the attributes to the termios structure*/
tcflush(fd, TCIFLUSH);
#endif
return true;
}
/** \brief Write data to COM port
*
* \param [in] data Data buffer for writing
* \param [in] len Length of data buffer
* \return 0 if everything Ok
*
*/
int COM_Write(uint8_t *data, uint16_t len)
{
#ifdef __MINGW32__
DWORD dwBytesWritten = 0;
//DWORD signal;
//OVERLAPPED ov = { 0 };
//int res;
//ov.hEvent = CreateEvent(NULL, true, true, NULL);
if (!WriteFile(hSerial, data, len, &dwBytesWritten, NULL))
return -1;
//COM_Bytes += dwBytesWritten;
// WriteFile(hSerial, data, len, &dwBytesWritten, &ov);
// signal = WaitForSingleObject(ov.hEvent, INFINITE);
// if ((signal == WAIT_OBJECT_0) && (GetOverlappedResult(hSerial, &ov, &dwBytesWritten, true)))
// res = 0;
// else
// res = -1;
// CloseHandle(ov.hEvent);
// return res;
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux)
int iOut = write(fd, data, len);
if (iOut < 0)
return -1;
#endif
return 0;
}
/** \brief Read data from COM port
*
* \param [out] data Data buffer to read data in
* \param [in] len Length of data to read
* \return number of received bytes as int
*
*/
int COM_Read(uint8_t *data, uint16_t len)
{
#ifdef __MINGW32__
//OVERLAPPED ov = { 0 };
//COMSTAT status;
//DWORD errors;
//DWORD mask, btr, temp, signal;
DWORD dwBytesRead = 0;
// ClearCommError(hSerial, &errors, &status);
// if (!ReadFile(hSerial, data, len, &dwBytesRead, &ov))
// return -1;
// btr = 0;
// while (btr < len)
// {
// SetCommMask(hSerial, EV_RXCHAR);
// WaitCommEvent(hSerial, &mask, NULL);
// if (mask & EV_ERR)
// break;
// ClearCommError(hSerial, &temp, &status);
// btr = status.cbInQue;
// if (btr >= len)
// {
// ReadFile(hSerial, data, len, &dwBytesRead, NULL);
// }
// }
ReadFile(hSerial, data, len, &dwBytesRead, NULL);
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux)
int dwBytesRead = read(fd, data, len);
if (dwBytesRead < 0)
return -1;
#endif
return dwBytesRead;
}
/** \brief Calculate time for transmission with current baudrate
*
* \param [in] len Length of transmitted data
* \return time in milliseconds as uint16_t
*
*/
uint16_t COM_GetTransTime(uint16_t len)
{
return (uint16_t)(len * 1000 * 11 / COM_Baudrate + 1);
}
#ifdef __MINGW32__
void COM_WaitForTransmit(void)
{
COMSTAT rStat;
DWORD nErr;
do {
ClearCommError(hSerial, &nErr, &rStat);
} while (rStat.cbOutQue > 0);
}
#endif // __MINGW32__
/** \brief Close current COM port
*
* \return Nothing
*
*/
void COM_Close(void)
{
printf("Closing COM port\n");
#ifdef __MINGW32__
CloseHandle(hSerial);
#endif
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__linux)
close(fd);
#endif
}