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)
34 else if (serialPort->
pfnOpen != 0)
36 if (!serialPort->
pfnOpen(serialPort, port, baudRate, blocking))
48 if (serialPort == 0 || serialPort->
handle == 0)
57 if (serialPort != 0 && serialPort->
pfnClose != 0)
59 return serialPort->
pfnClose(serialPort);
66 if (serialPort == 0 || serialPort->
handle == 0 || serialPort->
pfnFlush == 0)
70 return serialPort->
pfnFlush(serialPort);
80 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnRead == 0)
85 int count = serialPort->
pfnRead(serialPort, buffer, readCount, timeoutMilliseconds);
97 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || readCount < 1 || serialPort->pfnAsyncRead == 0 || completion == 0)
102 return serialPort->
pfnAsyncRead(serialPort, buffer, readCount, completion);
112 if (serialPort == 0 || serialPort->
handle == 0 || result == 0 || serialPort->
pfnRead == 0)
118 unsigned int memIndex = 0;
119 unsigned int memCapacity = 512;
121 unsigned char* mem = (
unsigned char*)
malloc(memCapacity);
125 if (memIndex == memCapacity)
127 memCapacity = (
unsigned int)(memCapacity * 1.5);
128 mem = (
unsigned char*)
realloc(mem, memCapacity);
131 if (b ==
'\n' && prevCR)
133 mem[memIndex -= 2] =
'\0';
137 prevCR = (b ==
'\r');
156 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || writeCount < 1 || serialPort->pfnWrite == 0)
161 int count = serialPort->
pfnWrite(serialPort, buffer, writeCount);
173 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || writeCount < 1)
179 count +=
serialPortWrite(serialPort, (
unsigned char[2]) {
'\r',
'\n' }, 2);
185 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || bufferLength < 2)
191 const unsigned char* ptr = (
const unsigned char*)buffer;
193 unsigned char buf[8];
205 count +=
serialPortWrite(serialPort, (
const unsigned char*)buffer, bufferLength);
207 while (ptr != ptrEnd)
214 #pragma warning(push) 215 #pragma warning(disable: 4996) 219 sprintf((
char*)buf,
"*%.2x\r\n", checkSum);
239 if (serialPort == 0 || serialPort->
handle == 0 || buffer == 0 || writeCount < 1 || waitFor == 0 || waitForLength < 1)
244 int actuallyWrittenCount =
serialPortWrite(serialPort, buffer, writeCount);
246 if (actuallyWrittenCount != writeCount)
261 if (serialPort == 0 || serialPort->
handle == 0 || waitFor == 0 || waitForLength < 1)
265 else if (waitForLength > 128)
270 unsigned char buf[128] = { 0 };
273 if (count == waitForLength && memcmp(buf, waitFor, waitForLength) == 0)
302 if (serialPort == 0 || serialPort->
handle == 0 || serialPort->
pfnSleep == 0)
307 return serialPort->
pfnSleep(serialPort, sleepMilliseconds);
int serialPortReadTimeoutAsync(serial_port_t *serialPort, unsigned char *buffer, int readCount, pfnSerialPortAsyncReadCompletion completion)
int serialPortOpen(serial_port_t *serialPort, const char *port, int baudRate, int blocking)
pfnSerialPortIsOpen pfnIsOpen
char port[MAX_SERIAL_PORT_NAME_LENGTH+1]
int serialPortSleep(serial_port_t *serialPort, int sleepMilliseconds)
pfnSerialPortAsyncRead pfnAsyncRead
int serialPortWrite(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
#define MAX_SERIAL_PORT_NAME_LENGTH
size_t count(InputIterator first, InputIterator last, T const &item)
pfnSerialPortGetByteCountAvailableToWrite pfnGetByteCountAvailableToWrite
int serialPortWriteAndWaitForTimeout(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength, const int timeoutMilliseconds)
int serialPortReadTimeout(serial_port_t *serialPort, unsigned char *buffer, int readCount, int timeoutMilliseconds)
pfnSerialPortOpen pfnOpen
int serialPortRead(serial_port_t *serialPort, unsigned char *buffer, int readCount)
int serialPortFlush(serial_port_t *serialPort)
void(* pfnSerialPortAsyncReadCompletion)(serial_port_t *serialPort, unsigned char *buf, int len, int errorCode)
pfnSerialPortRead pfnRead
int serialPortReadLineTimeout(serial_port_t *serialPort, unsigned char **result, int timeoutMilliseconds)
int serialPortWriteAndWaitFor(serial_port_t *serialPort, const unsigned char *buffer, int writeCount, const unsigned char *waitFor, int waitForLength)
pfnSerialPortFlush pfnFlush
void * realloc(void *ptr, size_t size)
int serialPortWriteLine(serial_port_t *serialPort, const unsigned char *buffer, int writeCount)
void serialPortSetPort(serial_port_t *serialPort, const char *port)
int serialPortReadCharTimeout(serial_port_t *serialPort, unsigned char *c, int timeoutMilliseconds)
int serialPortReadLine(serial_port_t *serialPort, unsigned char **result)
int SERIAL_PORT_DEFAULT_TIMEOUT
int serialPortIsOpen(serial_port_t *serialPort)
pfnSerialPortSleep pfnSleep
int serialPortClose(serial_port_t *serialPort)
void * malloc(size_t size)
int serialPortGetByteCountAvailableToWrite(serial_port_t *serialPort)
int serialPortWaitFor(serial_port_t *serialPort, const unsigned char *waitFor, int waitForLength)
pfnSerialPortClose pfnClose
int serialPortWriteAscii(serial_port_t *serialPort, const char *buffer, int bufferLength)
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 serialPortGetByteCountAvailableToRead(serial_port_t *serialPort)