diff --git a/inc/kSerial.h b/inc/kSerial.h index deea153..138ee0b 100644 --- a/inc/kSerial.h +++ b/inc/kSerial.h @@ -27,21 +27,33 @@ extern "C" { /* Define ----------------------------------------------------------------------------------*/ #ifndef KSERIAL_SEND_ENABLE -#define KSERIAL_SEND_ENABLE (1U) +#define KSERIAL_SEND_ENABLE (1U) #ifndef KS_MAX_SEND_BUFFER_SIZE -#define KS_MAX_SEND_BUFFER_SIZE (4096 + 32) +#define KS_MAX_SEND_BUFFER_SIZE (4096 + 32) #endif #endif #ifndef KSERIAL_RECV_ENABLE -#define KSERIAL_RECV_ENABLE (1U) +#define KSERIAL_RECV_ENABLE (1U) #ifndef KS_MAX_RECV_BUFFER_SIZE -#define KS_MAX_RECV_BUFFER_SIZE (4096 + 1024 + 32) +#define KS_MAX_RECV_BUFFER_SIZE (4096 + 1024 + 32) #endif #endif +#ifndef KSERIAL_RECV_TREAD_ENABLE +#define KSERIAL_RECV_TREAD_ENABLE (1U) +#define KSERIAL_MAX_PACKET_LENS (4096) +#define KSERIAL_RECV_PACKET_BUFFER_LENS (64 * 1024) +#endif + #ifndef KSERIAL_CMD_ENABLE -#define KSERIAL_CMD_ENABLE (1U) +#define KSERIAL_CMD_ENABLE (1U) +#endif + +#if KSERIAL_RECV_TREAD_ENABLE +#if !(KSERIAL_RECV_ENABLE) +#error "Need to enable recv" +#endif #endif #if KSERIAL_CMD_ENABLE #if !(KSERIAL_SEND_ENABLE && KSERIAL_RECV_ENABLE) @@ -49,21 +61,23 @@ extern "C" { #endif #endif +#define KSERIAL_TYPE_LENS (16) + /* Macro -----------------------------------------------------------------------------------*/ #if KSERIAL_SEND_ENABLE #ifndef kSerial_Send -#define kSerial_Send(__DATA, __LENS) Serial_SendData(&s, __DATA, __LENS) -#define kSerial_SendByte(__DATA) Serial_SendByte(&s, __DATA) +#define kSerial_Send(__DATA, __LENS) Serial_SendData(&s, __DATA, __LENS) +#define kSerial_SendByte(__DATA) Serial_SendByte(&s, __DATA) #endif #endif #if KSERIAL_RECV_ENABLE -#define kSerial_Recv(__DATA, __LENS) Serial_RecvData(&s, __DATA, __LENS) -#define kSerial_RecvByte() Serial_RecvByte(&s) -#define kSerial_RecvFlush() Serial_Flush(&s) +#define kSerial_Recv(__DATA, __LENS) Serial_RecvData(&s, __DATA, __LENS) +#define kSerial_RecvByte() Serial_RecvByte(&s) +#define kSerial_RecvFlush() Serial_Flush(&s) #endif #if (KSERIAL_SEND_ENABLE || KSERIAL_RECV_ENABLE) -#define kSerial_Delay(__MS) Serial_Delay(__MS) +#define kSerial_Delay(__MS) Serial_Delay(__MS) #endif /* Typedef ---------------------------------------------------------------------------------*/ @@ -102,10 +116,13 @@ typedef enum } kserial_r2_command_t; +typedef void (*pKserialCallback)( kserial_packet_t *pk, uint8_t *data, uint32_t count, uint32_t total ); + /* Extern ----------------------------------------------------------------------------------*/ -extern const uint32_t KS_TYPE_SIZE[16]; -extern const char KS_TYPE_STRING[16][4]; +extern const uint32_t KS_TYPE_SIZE[KSERIAL_TYPE_LENS]; +extern const char KS_TYPE_STRING[KSERIAL_TYPE_LENS][4]; +extern const char KS_TYPE_FORMATE[KSERIAL_TYPE_LENS][8]; /* Functions -------------------------------------------------------------------------------*/ @@ -126,6 +143,8 @@ uint32_t kSerial_RecvPacket( uint8_t input, void *param, void *pdata, uint32_ uint32_t kSerial_Read( kserial_t *ks ); void kSerial_ReadFlush( kserial_t *ks ); void kSerial_GetPacketData( kserial_packet_t *ksp, void *pdata, uint32_t index ); +uint32_t kSerial_ContinuousRead( kserial_packet_t *ksp, uint32_t *index, uint32_t *count, uint32_t *total ); + uint32_t kSerial_SendCommand( uint32_t type, uint32_t p1, uint32_t p2, uint32_t ack[3] ); uint32_t kSerial_DeviceCheck( uint32_t *id ); diff --git a/src/kSerial.c b/src/kSerial.c index e23ce50..564b25d 100644 --- a/src/kSerial.c +++ b/src/kSerial.c @@ -42,7 +42,19 @@ uint8_t ksSendBuf[KS_MAX_SEND_BUFFER_SIZE] = {0}; uint8_t ksRecvBuf[KS_MAX_RECV_BUFFER_SIZE] = {0}; #endif -const uint32_t KS_TYPE_SIZE[16] = +#if KSERIAL_RECV_TREAD_ENABLE +uint8_t ksPacketBuf[KSERIAL_RECV_PACKET_BUFFER_LENS] = {0}; +kserial_packet_t ksPacket[KSERIAL_MAX_PACKET_LENS] = {0}; +kserial_t ks = +{ + .size = KSERIAL_RECV_PACKET_BUFFER_LENS, + .count = 0, + .buffer = ksPacketBuf, + .packet = ksPacket +}; +#endif + +const uint32_t KS_TYPE_SIZE[KSERIAL_TYPE_LENS] = { 1, 2, 4, 8, 1, 2, 4, 8, @@ -50,7 +62,7 @@ const uint32_t KS_TYPE_SIZE[16] = 0, 0, 0, 0 }; -const char KS_TYPE_STRING[16][4] = +const char KS_TYPE_STRING[KSERIAL_TYPE_LENS][4] = { "U8", "U16", "U32", "U64", "I8", "I16", "I32", "I64", @@ -58,6 +70,14 @@ const char KS_TYPE_STRING[16][4] = "R1", "R2", "R3", "R4", }; +const char KS_TYPE_FORMATE[KSERIAL_TYPE_LENS][8] = +{ + "%4d", "%6d", "%11d", "%20d", + "%4d", "%6d", "%11d", "%20d", + "", "%.6f", "%.6f", "%.6f", + "", "", "", "" +}; + /* Prototypes ------------------------------------------------------------------------------*/ /* Functions -------------------------------------------------------------------------------*/ @@ -379,6 +399,23 @@ void kSerial_ReadFlush( kserial_t *ks ) #endif } +/** + * @brief kSerial_GetFrequence + */ +// float kSerial_GetFrequence( uint32_t lens, uint32_t time, uint32_t count ) +// { +// // static uint64_t kslensLast = 0; +// // static uint64_t ksTimeLast = 0; +// static float frequence = 0; +// // if ((time - ksTimeLast) >= count) +// // { +// // frequence = ((float)lens - kslensLast) / (time - ksTimeLast) * 1000.0f; +// // kslensLast = lens; +// // ksTimeLast = time; +// // } +// return frequence; +// } + /** * @brief kSerial_GetPacketData */ @@ -391,6 +428,35 @@ void kSerial_GetPacketData( kserial_packet_t *ksp, void *pdata, uint32_t index ) free(ksp[index].data); } +/** + * @brief kSerial_ContinuousRead + */ +uint32_t kSerial_ContinuousRead( kserial_packet_t *ksp, uint32_t *index, uint32_t *count, uint32_t *total ) +{ +#if KSERIAL_RECV_TREAD_ENABLE + if ((*count == 0) || (*index >= *count)) + { + *count = kSerial_Read(&ks); + if (*count == 0) + { + return KS_ERROR; + } + *index = 0; + } + kSerial_GetPacketData(ks.packet, ksp->data, *index); + ksp->param[0] = ks.packet[*index].param[0]; + ksp->param[1] = ks.packet[*index].param[1]; + ksp->type = ks.packet[*index].type; + ksp->lens = ks.packet[*index].lens; + ksp->nbyte = ks.packet[*index].nbyte; + (*total)++; + (*index)++; + return KS_OK; +#else + return KS_ERROR; +#endif +} + /** * @brief kSerial_SendCommand * Send packet ['K', 'S', type, 0, param1, param2, ck, '\r'] diff --git a/src/serial.c b/src/serial.c index c4c9587..319c49f 100644 --- a/src/serial.c +++ b/src/serial.c @@ -89,7 +89,7 @@ int Serial_OpenComport( serial_t *serial ) // check max comport number if (serial->port >= SERIAL_MAX_PORT_NUM) { - klogd("max port number\n"); + klogd(" max port number\n"); return KS_ERROR; } @@ -107,7 +107,7 @@ int Serial_OpenComport( serial_t *serial ) serial->handle = CreateFileA(portName, desiredAccess, 0, NULL, OPEN_EXISTING, 0, NULL); if (serial->handle == INVALID_HANDLE_VALUE) { - klogd("open comport failed (%s)\n", serial->name); + klogd(" open comport failed (%s)\n", serial->name); return KS_ERROR; } @@ -115,7 +115,7 @@ int Serial_OpenComport( serial_t *serial ) // event setting if (!SetCommMask(serial->handle, EV_RXCHAR | EV_ERR)) { - klogd("set comport event failed\n"); + klogd(" set comport event failed\n"); CloseHandle(serial->handle); return KS_ERROR; } @@ -125,7 +125,7 @@ int Serial_OpenComport( serial_t *serial ) // rx, tx buffer size setting if (!SetupComm(serial->handle, serial->txBufferSize, serial->txBufferSize)) { - klogd("set comport buffer size failed\n"); + klogd(" set comport buffer size failed\n"); CloseHandle(serial->handle); return KS_ERROR; } @@ -139,7 +139,7 @@ int Serial_OpenComport( serial_t *serial ) serial->timeouts.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(serial->handle, &serial->timeouts)) { - klogd("set comport timeout failed\n"); + klogd(" set comport timeout failed\n"); CloseHandle(serial->handle); return KS_ERROR; } @@ -187,7 +187,7 @@ int Serial_GetComportList( comport_list_t *list ) ); if (retVal != ERROR_SUCCESS) { - klogd("open registry key failed\n"); + klogd(" open registry key failed\n"); return KS_ERROR; } @@ -208,7 +208,7 @@ int Serial_GetComportList( comport_list_t *list ) ); if (retVal != ERROR_SUCCESS) { - klogd("get registry key information failed\n"); + klogd(" get registry key information failed\n"); RegCloseKey(hKey); return KS_ERROR; } @@ -318,7 +318,7 @@ static int get_dcb_config( const serial_t *serial, serial_config_t *config, DCB { if (!GetCommState(serial->handle, dcb)) { - klogd("get comport dcb setting failed\n"); + klogd(" get comport dcb setting failed\n"); return KS_ERROR; } @@ -421,7 +421,7 @@ static int set_dcb_config( const serial_t *serial, serial_config_t *config, DCB case SERIAL_PARITY_SPACE: dcb->Parity = SPACEPARITY; break; default: { - klogd("invalid parity setting\n"); + klogd(" invalid parity setting\n"); return KS_ERROR; } } @@ -432,7 +432,7 @@ static int set_dcb_config( const serial_t *serial, serial_config_t *config, DCB case 2: dcb->StopBits = TWOSTOPBITS; break; default: { - klogd("invalid stopbits setting\n"); + klogd(" invalid stopbits setting\n"); return KS_ERROR; } } @@ -444,7 +444,7 @@ static int set_dcb_config( const serial_t *serial, serial_config_t *config, DCB case SERIAL_RTS_FLOW_CONTROL: dcb->fRtsControl = RTS_CONTROL_HANDSHAKE; break; default: { - klogd("invalid rts setting\n"); + klogd(" invalid rts setting\n"); return KS_ERROR; } } @@ -455,7 +455,7 @@ static int set_dcb_config( const serial_t *serial, serial_config_t *config, DCB case SERIAL_CTS_FLOW_CONTROL: dcb->fOutxCtsFlow = TRUE; break; default: { - klogd("invalid cts setting\n"); + klogd(" invalid cts setting\n"); return KS_ERROR; } } @@ -467,7 +467,7 @@ static int set_dcb_config( const serial_t *serial, serial_config_t *config, DCB case SERIAL_DTR_FLOW_CONTROL: dcb->fDtrControl = DTR_CONTROL_HANDSHAKE; break; default: { - klogd("invalid dtr setting\n"); + klogd(" invalid dtr setting\n"); return KS_ERROR; } } @@ -478,7 +478,7 @@ static int set_dcb_config( const serial_t *serial, serial_config_t *config, DCB case SERIAL_DSR_FLOW_CONTROL: dcb->fOutxDsrFlow = TRUE; break; default: { - klogd("invalid dsr setting\n"); + klogd(" invalid dsr setting\n"); return KS_ERROR; } } @@ -491,14 +491,14 @@ static int set_dcb_config( const serial_t *serial, serial_config_t *config, DCB case SERIAL_XONXOFF_INOUT: dcb->fInX = TRUE; dcb->fOutX = TRUE; break; default: { - klogd("invalid xon/xoff setting\n"); + klogd(" invalid xon/xoff setting\n"); return KS_ERROR; } } if (!SetCommState(serial->handle, dcb)) { - klogd("set comport dcb setting failed\n"); + klogd(" set comport dcb setting failed\n"); CloseHandle(serial->handle); return KS_ERROR; }