📄 serial.c
字号:
logERROR(LOGSRC,"***** EOL wasn't stripped"); } /* place char in buffer */ BUFFER_LOCK { if (!bufferPutString(serBuffer, buf)) { // This means that we will be losing packets // The buffer is large enough that we should never get backed up. // This would only occur if the main thread somehow stopped pulling // data off this queue. logCRITICAL(LOGSRC,"Command buffer overflow!"); } //double load = (double)bufferGetLength(serBuffer) * 100.0 / (double)bufferGetSize(serBuffer); //if (load > maxBufferLoad) { maxBufferLoad = load; } //logINFO(LOGSRC,"Buffer load: %.1f [max %.1f]", load, maxBufferLoad); BUFFER_NOTIFY; } BUFFER_UNLOCK } return -1; // end of thread }/* thread main */static void _serdevThreadRunnable(void *arg){ ComPort_t *com = &serComPort; serCloseTime = 0L; while (serRunThread) { /* open port to Serial device */ if (!_serdevOpenCom(com, utFalse)) { threadSleepMS(3000L); // short sleep continue; } comPortSetDTR(com, utTrue); comPortSetRTS(com, utTrue); /* read data loop */ int err = _serdevReadData(); // Return codes: // 0 : Serial disconnect // -1 : a comport error occurred, or end-of-thread /* ComPort failed, or Serial disconnect */ logINFO(LOGSRC,"Closing Serial comport\n"); _serdevCloseCom(err? utTrue : utFalse); } // once this thread stops, it isn't starting again logERROR(LOGSRC,"Stopping thread"); threadExit();}/* call-back to stop thread */static void _serdevStopThread(void *arg){ serRunThread = utFalse; _serdevCloseCom(utTrue);}/* start serial communication thread */static utBool serdevStartThread(){ /* init */ comPortInitStruct(&serComPort); /* create thread */ serRunThread = utTrue; if (threadCreate(&serThread,&_serdevThreadRunnable,0,"Serial") == 0) { // thread started successfully threadAddThreadStopFtn(&_serdevStopThread,0); return utTrue; } else { serRunThread = utFalse; return utFalse; } }// ----------------------------------------------------------------------------/* read a line of data fom the serial port */static int serdevReadLine(char *data, int dataLen, UInt32 timeoutMS){ int len; UInt32 nowTimeSec = utcGetTimeSec(); UInt32 timeoutSec = nowTimeSec + ((timeoutMS + 500L) / 1000L); for (;;) { /* get string */ BUFFER_LOCK { len = bufferGetString(serBuffer, data, dataLen); } BUFFER_UNLOCK /* error? (unlikely) */ if (len < 0) { return len; } /* no data? (very likely) */ if (len == 0) { nowTimeSec = utcGetTimeSec(); if (nowTimeSec < timeoutSec) { // wait for data //threadSleepMS(100L); BUFFER_LOCK { BUFFER_WAIT(1000L); } BUFFER_UNLOCK continue; } else { // timeout - exit now return 0; } } /* append EOL and return */ //if ((len < dataLen) && (data[len - 1] != PACKET_ASCII_ENCODING_EOL)) { // data[len++] = PACKET_ASCII_ENCODING_EOL; //} return len; } /* timeout */ return 0; }// ----------------------------------------------------------------------------/* write data to the serial port */static int serdevWrite(const UInt8 *data, int dataLen){ ComPort_t *com = &serComPort; /* last openned? */ UInt32 lastOpenned = serdevGetOpenTime(); if (lastOpenned <= 0L) { // outbound port is not open logINFO(LOGSRC,"Serial port not open"); return -1; } /* write to comport */ return comPortWrite(com, data, dataLen);}// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------// ----------------------------------------------------------------------------/* flush input buffer */static void serial_transportReadFlush(){ BUFFER_LOCK { bufferClear(serBuffer); } BUFFER_UNLOCK}/* return true if transport is open */static utBool serial_transportIsOpen(){ // this may be called by threads other than the protocol thread. UInt32 openTime = serdevGetOpenTime(); return (openTime > 0L)? utTrue : utFalse;}/* cloe transport */static utBool serial_transportClose(utBool sendUDP){ // This transport is never explicitly closed, but is automatically // closed when the Serial port device 'disappears' (ie Serial disconnects) return utTrue; // however, always return successful}/* open transport */static utBool serial_transportOpen(TransportType_t type){ if (type == TRANSPORT_DUPLEX) { serialXport.type = TRANSPORT_DUPLEX; // the client is just now starting a communication session // clear all old server data from the buffer serial_transportReadFlush(); return serial_transportIsOpen(); } else { logERROR(LOGSRC,"Transport type not supported: %d", type); return utFalse; }}/* read packet from transport */static int serial_transportReadPacket(UInt8 *buf, int bufLen){ //logINFO(LOGSRC,"transport read packet ..."); /* transport not open? */ if (!serial_transportIsOpen()) { logERROR(LOGSRC,"Transport is not open"); return -1; } /* no data was requested */ if (!buf || (bufLen == 0)) { return 0; } *buf = 0; /* read line (packet) */ int readLen = serdevReadLine((char*)buf, bufLen, 2000L); //logINFO(LOGSRC,"ReadLine: [%d] %s", readLen, buf); /* return length */ return readLen; }// ----------------------------------------------------------------------------/* write packet to transport */static int serial_transportWritePacket(const UInt8 *buf, int bufLen){ /* transport not open? */ if (!serial_transportIsOpen()) { logERROR(LOGSRC,"Transport is not open"); return -1; } /* nothing specified to write */ if (bufLen == 0) { return 0; } /* write data per transport type */ int len = serdevWrite(buf, bufLen); return len;}// ----------------------------------------------------------------------------/* initialize transport */static utBool _transportDidInit = utFalse;const TransportFtns_t *serial_transportInitialize(){ /* already initialized? */ if (_transportDidInit) { return &serXportFtns; } _transportDidInit = utTrue; /* init transport structure */ memset(&serialXport, 0, sizeof(SerialTransport_t)); serialXport.type = TRANSPORT_DUPLEX; /* init Serial communication */ serdevInitialize(); serdevStartThread(); /* init function structure */ serXportFtns.xportIsOpen = &serial_transportIsOpen; serXportFtns.xportOpen = &serial_transportOpen; serXportFtns.xportClose = &serial_transportClose; serXportFtns.xportReadFlush = &serial_transportReadFlush; serXportFtns.xportReadPacket = &serial_transportReadPacket; serXportFtns.xportWritePacket = &serial_transportWritePacket; return &serXportFtns;}// ----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -