29 #if defined(_USE_WIN_API) 31 #elif defined(_USE_LINUX_API) 34 #include <sys/ioctl.h> 37 #define _BAUD_MAX (15) 38 #define _BAUD_PAIR(val) {val, B ## val} 39 static const unsigned int BAUD_RATE[_BAUD_MAX][2] =
40 { _BAUD_PAIR(50), _BAUD_PAIR(75), _BAUD_PAIR(110), _BAUD_PAIR(134), _BAUD_PAIR(150),
41 _BAUD_PAIR(200), _BAUD_PAIR(300), _BAUD_PAIR(600), _BAUD_PAIR(1200), _BAUD_PAIR(1800),
42 _BAUD_PAIR(2400), _BAUD_PAIR(4800), _BAUD_PAIR(9600), _BAUD_PAIR(19200), _BAUD_PAIR(38400)};
51 #define _COM_PORT_MAX (256) 53 #define _FLOW_XINOUT (1) 54 #define _FLOW_HARDWARE (2) 56 #if defined(_USE_WIN_API) 63 sprintf(szName,
"//./COM%d", com_param->
port);
64 *sock = (int)CreateFileA(szName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
79 state.Parity = com_param->
parity;
91 state.fOutxCtsFlow = 1;
92 state.fRtsControl = RTS_CONTROL_HANDSHAKE;
94 state.fOutxCtsFlow = 0;
95 state.fRtsControl = RTS_CONTROL_ENABLE;
97 state.fOutxDsrFlow = 0;
98 state.fDtrControl = DTR_CONTROL_ENABLE;
99 state.fDsrSensitivity = 0;
109 return CloseHandle((HANDLE)sock);
114 return WriteFile((HANDLE)sock, buf, len_send, (LPDWORD)len_sended, NULL);
122 ret = ReadFile((HANDLE)sock, buf, len_recv, (LPDWORD)len_recved, NULL);
130 COMMTIMEOUTS stTimeOut;
133 stTimeOut.ReadIntervalTimeout = MAXDWORD;
134 stTimeOut.ReadTotalTimeoutMultiplier = MAXDWORD;
135 stTimeOut.ReadTotalTimeoutConstant = timeout;
136 stTimeOut.WriteTotalTimeoutMultiplier = 1;
137 stTimeOut.WriteTotalTimeoutConstant = timeout;
138 ret = SetCommTimeouts((HANDLE)sock, &stTimeOut);
153 ret = ClearCommError((HANDLE)sock, &err, NULL);
155 ret = PurgeComm((HANDLE)sock, (PURGE_TXABORT | PURGE_RXABORT| PURGE_TXCLEAR | PURGE_RXCLEAR));
167 return GetCommState((HANDLE)sock, state);
172 return SetCommState((HANDLE)sock, state);
177 return GetCommModemStatus((HANDLE)sock, (DWORD *)state);
179 #elif defined(_USE_LINUX_API) 187 sprintf(szName,
"/dev/ttyS%d", com_param->
port);
188 *sock = open(szName, O_RDWR | O_NOCTTY |
O_NONBLOCK);
201 state.c_cflag = (CLOCAL | CREAD);
203 for(i = 0; i < _BAUD_MAX; i++) {
204 if(com_param->
baud_rate == BAUD_RATE[i][0]) {
205 state.c_cflag |= BAUD_RATE[i][1];
212 state.c_cflag |= CS5;
215 state.c_cflag |= CS6;
218 state.c_cflag |= CS7;
221 state.c_cflag |= CS8;
227 switch(com_param->
parity) {
229 state.c_cflag &= ~PARENB;
232 state.c_cflag |= PARENB;
233 state.c_cflag |= PARODD;
236 state.c_cflag |= PARENB;
237 state.c_cflag &= ~PARODD;
245 state.c_cflag &= ~CSTOPB;
248 state.c_cflag |= CSTOPB;
255 state.c_iflag |= IXON;
256 state.c_iflag |= IXOFF;
258 state.c_iflag &= ~IXON;
259 state.c_iflag &= ~IXOFF;
263 state.c_cflag |= CRTSCTS;
265 state.c_cflag &= ~CRTSCTS;
281 ret = write(sock, buf, len_send);
294 ret = read(sock, buf, len_recv);
308 state.c_cc[VMIN] = 0;
309 state.c_cc[VTIME] = timeout * 10 / 1000;
322 ret = tcflush(sock, TCIFLUSH);
328 ret = tcflush(sock, TCOFLUSH);
339 return tcgetattr(sock, state);
344 return tcsetattr(sock, TCSAFLUSH, state);
349 return ioctl(sock, TIOCMGET, (
int *)state);
366 if (param == NULL || sock == NULL)
370 port = com_param->
port;
389 if (sock == NULL || *sock <= 0)
419 if (buf == NULL || strlen(buf) == 0)
422 len_send = (len_buf != 0) ? len_buf : strlen(buf);
423 ret =
_com_send(sock, buf, len_send, &len_sended, arg);
427 #if defined(_USE_WIN_API) 430 ClearCommError((HANDLE)sock, &err_flag, NULL);
436 if (len_send > len_sended) {
460 if (buf == NULL || len_recved == NULL)
463 ret =
_com_recv(sock, buf, len_buf, len_recved, timeout, arg);
467 #if defined(_USE_WIN_API) 470 ClearCommError((HANDLE)sock, &err_flag, NULL);
476 if (*len_recved == 0)
static int _com_set_state(int sock, COM_STATE *state)
static int32_t _com_clear(int sock, uint32_t timeout)
#define ODDPARITY
A definition for serial communication setting.
static int _com_get_state(int sock, COM_STATE *state)
HRESULT com_clear(int sock, uint32_t timeout)
Clears the received buffer.
HRESULT com_set_timeout(int sock, uint32_t timeout)
Sets timeout value to the serial socket.
static int _com_send(int sock, const char *buf, uint32_t len_send, uint32_t *len_sended, void *arg)
static int32_t _com_set_timeout(int sock, uint32_t timeout)
#define EVENPARITY
A definition for serial communication setting.
#define E_HANDLE
Failed because the handle is invalid.
static int _com_recv(int sock, char *buf, uint32_t len_recv, uint32_t *len_recved, uint32_t timeout, void *arg)
HRESULT com_open(void *param, int *sock)
Opens serial port.
#define E_INVALIDARG
Failed because some arguments are invalid.
#define DNGetLastError()
A macro that gets last OS error.
HRESULT com_get_state(int sock, COM_STATE *state)
Gets the serial socket parameters.
HRESULT com_set_state(int sock, COM_STATE *state)
Puts the serial socket parameters.
#define NOPARITY
A definition for serial communication setting.
#define SUCCEEDED(hr)
A macro that returns TRUE/FALSE. If hr is zero or more, then returns TRUE.
static int32_t _com_open(const void *com_param, int *sock)
#define ONESTOPBIT
A definition for serial communication setting.
HRESULT com_send(int sock, const char *buf, uint32_t len_buf, void *arg)
Sends serial packet.
#define OSFAILED(ret)
A macro that returns TRUE/FALSE. If ret is OS failed, then returns TRUE.
static int _com_close(int sock)
HRESULT com_recv(int sock, char *buf, uint32_t len_buf, uint32_t *len_recved, uint32_t timeout, void *arg)
Receives serial packet.
static int _com_get_modem_state(int sock, uint32_t *state)
HRESULT com_close(int *sock)
Closes the socket.
A type definition for serial connection parameters.
_DN_EXP_DEVICE HRESULT check_timeout(int sock, uint32_t timeout)
Checks the communication timeout.
#define OSSUCCEEDED(ret)
A macro that returns TRUE/FALSE. If ret is OS succeeded, then returns TRUE.
HRESULT com_get_modem_state(int sock, uint32_t *state)
Gets the serial port pin status.
#define E_TIMEOUT
Failed because the communication timed out.
#define OSERR2HRESULT(err)
A macro that returns HREUSLT(0x8091) which means OS error.
#define TWOSTOPBITS
A definition for serial communication setting.