#include #include #include #include #include #include #include // #include #include #include #include // #include #include #include #include "uart.hpp" using namespace std; #define BACK_LOG 10 #define MAX_RECV_SIZE 235 typedef struct { int fd; struct sockaddr_in client; } tcpclient_t; map g_baundmap = { {"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, // {"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, // {"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, // {"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, // {"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, // {"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, // {"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, // {"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017}, }; UartDevice g_uart_device = {0}; void printf_buf(char *rx, ssize_t size) { for (int i = 0; i < size; i++) { printf("0x%02x,", rx[i]); } printf("\n"); } void *uart_rx_thread_func(void *arg) { char buff[4096]; while (true) { int ret = uartReceive(&g_uart_device, buff, 1024); // send the received text over UART if (ret < 0) { printf("uartReceive fail\n"); exit(-1); } // if (ret > 0) { printf("net<-uart: %d\n", ret); printf_buf(buff, ret); } // } return NULL; } int openuart(struct UartDevice *device, const char *devname, int rate) { device->name = (char *)devname; device->rate = rate; printf("UART open %s\n", device->name); return uartStart(device, 0); } static int strtohex(const char *str, uint8_t *hex) { /** * @brief * str: 02 34 78 92 65 AF */ int len = strlen(str); int i = 0; int j = 0; while (i < len) { if (str[i] == ' ') { i++; continue; } char tmp[3] = {0}; tmp[0] = str[i]; tmp[1] = str[i + 1]; hex[j] = strtol(tmp, NULL, 16); i += 2; j++; } if(j == 0){ return 0; } return j-1; } int main(int argc, char const *argv[]) { /* code */ //"net_uart /dev/ttyUSB0 115200 port" if (argc != 3) { printf("Usage: %s /dev/ttyUSB0 115200 \n", argv[0]); return -1; } printf("device name:%s\n", argv[1]); printf("baundrate :%s\n", argv[2]); // printf("port :%s\n", argv[3]); auto baundrate_find_result = g_baundmap.find(argv[2]); if (baundrate_find_result == g_baundmap.end()) { printf("unsupport baundrate\n"); return -1; // baundrate_find_result. }; /** * 打开串口 */ int rc = openuart(&g_uart_device, argv[1], baundrate_find_result->second); if (rc) { perror("open uart fail"); exit(-1); } pthread_t uart_rx_thread; pthread_create(&uart_rx_thread, NULL, uart_rx_thread_func, NULL); while (true) { char input[1024] = {0}; uint8_t hex[1024] = {0}; // scanf("%s", input); fgets(input, 1024, stdin); // printf("input:%s\n", input); // 将输入的字符串转换为16进制 int nhex = strtohex(input, hex); printf("tx:"); printf_buf((char *)hex, nhex); // int ret = uartSend(&g_uart_device, input, strlen(input)); // send the received text over UART int ret = uartSend(&g_uart_device, (char *)hex, nhex); // send the received text over UART if (ret < 0) { printf("uartSend fail\n"); } else { printf("uartSend ok\n"); } } return 0; }