20 if (serialPort != 0 && port != 0 && port != serialPort->
port)
23 memcpy(serialPort->
port, port, portLength);
24 serialPort->
port[portLength] =
'\0';
30 if (serialPort == 0 || port == 0 || serialPort->
pfnOpen == 0)
34 return serialPort->
pfnOpen(serialPort, port, baudRate, blocking);
39 if (serialPort == 0 || port == 0 || serialPort->
pfnOpen == 0)
45 for (
int retry = 0; retry < 30; retry++)
59 if (serialPort == 0 || serialPort->
handle == 0)
68 if (serialPort != 0 && serialPort->
pfnClose != 0)
70 return serialPort->
pfnClose(serialPort);
77 if (serialPort == 0 || serialPort->
handle == 0 || serialPort->
pfnFlush == 0)
81 return serialPort->
pfnFlush(serialPort);
91 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnRead == 0)
96 int count = serialPort->
pfnRead(serialPort, buffer, readCount, timeoutMilliseconds);
108 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnAsyncRead == 0 || completion == 0)
113 return serialPort->
pfnAsyncRead(serialPort, buffer, readCount, completion);
123 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || bufferLength < 8 || serialPort->pfnRead == 0)
133 buffer[bufferIndex++] = c;
134 if (c ==
'\n' && prevCR)
137 buffer[bufferIndex -= 2] =
'\0';
140 prevCR = (c ==
'\r');
153 unsigned char* ptr = buffer;
154 unsigned char* ptrEnd = buffer +
count;
155 while (*ptr !=
'$' && ptr < ptrEnd)
161 if (ptrEnd - ptr > 7)
168 int existingChecksum;
171 unsigned char* ptrEndNoChecksum = ptrEnd - 3;
172 while (++ptr < ptrEndNoChecksum)
180 existingChecksum = strtol((
void*)++ptr,
NULL, 16);
181 if (existingChecksum == checksum)
203 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || writeCount < 1 || serialPort->pfnWrite == 0)
208 int count = serialPort->
pfnWrite(serialPort, buffer, writeCount);
220 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || writeCount < 1)
232 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || bufferLength < 2)
238 const unsigned char* ptr = (
const unsigned char*)buffer;
252 unsigned char buf[16];
254 count +=
serialPortWrite(serialPort, (
const unsigned char*)buffer, bufferLength);
256 while (ptr != ptrEnd)
263 #pragma warning(push) 264 #pragma warning(disable: 4996) 268 snprintf((
char*)buf,
sizeof(buf),
"*%.2x\r\n", checkSum);
288 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || writeCount < 1 || waitFor == 0 || waitForLength < 1)
293 int actuallyWrittenCount =
serialPortWrite(serialPort, buffer, writeCount);
295 if (actuallyWrittenCount != writeCount)
310 if (serialPort == 0 || serialPort->
handle == 0 || waitFor == 0 || waitForLength < 1)
314 else if (waitForLength > 128)
319 unsigned char buf[128] = { 0 };
322 if (count == waitForLength && memcmp(buf, waitFor, waitForLength) == 0)
351 if (serialPort == 0 || serialPort->
pfnSleep == 0)
356 return serialPort->
pfnSleep(sleepMilliseconds);
int serialPortGetByteCountAvailableToRead(serial_port_t *serialPort)
int serialPortReadAscii(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, unsigned char **asciiData)
int serialPortReadLine(serial_port_t *serialPort, unsigned char *buffer, int bufferLength)
pfnSerialPortIsOpen pfnIsOpen
char port[MAX_SERIAL_PORT_NAME_LENGTH+1]
int serialPortReadCharTimeout(serial_port_t *serialPort, unsigned char *c, int timeoutMilliseconds)
pfnSerialPortAsyncRead pfnAsyncRead
int serialPortFlush(serial_port_t *serialPort)
int serialPortReadTimeout(serial_port_t *serialPort, unsigned char *buffer, int readCount, int timeoutMilliseconds)
int serialPortOpen(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
#define MAX_SERIAL_PORT_NAME_LENGTH
int serialPortWriteAscii(serial_port_t *serialPort, const char *buffer, int bufferLength)
size_t count(InputIterator first, InputIterator last, T const &item)
pfnSerialPortGetByteCountAvailableToWrite pfnGetByteCountAvailableToWrite
void serialPortSetPort(serial_port_t *serialPort, const char *port)
int serialPortOpenRetry(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
pfnSerialPortOpen pfnOpen
int serialPortGetByteCountAvailableToWrite(serial_port_t *serialPort)
void(* pfnSerialPortAsyncReadCompletion)(serial_port_t *serialPort, unsigned char *buf, int len, int errorCode)
int serialPortSleep(serial_port_t *serialPort, int sleepMilliseconds)
int serialPortWaitFor(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength)
int serialPortReadAsciiTimeout(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, int timeoutMilliseconds, unsigned char **asciiData)
pfnSerialPortRead pfnRead
int serialPortRead(serial_port_t *serialPort, unsigned char *buffer, int readCount)
pfnSerialPortFlush pfnFlush
int serialPortReadLineTimeout(serial_port_t *serialPort, unsigned char *buffer, int bufferLength, int timeoutMilliseconds)
int SERIAL_PORT_DEFAULT_TIMEOUT
int serialPortWrite(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
int serialPortWriteAndWaitFor(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength)
int serialPortClose(serial_port_t *serialPort)
pfnSerialPortSleep pfnSleep
int serialPortWriteAndWaitForTimeout(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength, const int timeoutMilliseconds)
int serialPortIsOpen(serial_port_t *serialPort)
pfnSerialPortClose pfnClose
int serialPortWriteLine(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
pfnSerialPortGetByteCountAvailableToRead pfnGetByteCountAvailableToRead
pfnSerialPortWrite pfnWrite
int serialPortWaitForTimeout(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength, int timeoutMilliseconds)
int serialPortReadChar(serial_port_t *serialPort, unsigned char *c)
int serialPortReadTimeoutAsync(serial_port_t *serialPort, unsigned char *buffer, int readCount, pfnSerialPortAsyncReadCompletion completion)