You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.7 KiB

2 years ago
  1. #include <fcntl.h>
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <termios.h>
  7. #include <unistd.h>
  8. //
  9. #include <arpa/inet.h>
  10. #include <netinet/in.h>
  11. #include <sys/socket.h>
  12. #include <sys/types.h>
  13. //
  14. #include <map>
  15. #include <set>
  16. #include <string>
  17. #include "uart.hpp"
  18. using namespace std;
  19. #define BACK_LOG 10
  20. #define MAX_RECV_SIZE 235
  21. typedef struct {
  22. int fd;
  23. struct sockaddr_in client;
  24. } tcpclient_t;
  25. map<string, uint32_t> g_baundmap = {
  26. {"0", 0000000}, {"50", 0000001}, {"75", 0000002}, {"110", 0000003}, //
  27. {"134", 0000004}, {"150", 0000005}, {"200", 0000006}, {"300", 0000007}, //
  28. {"600", 0000010}, {"1200", 0000011}, {"1800", 0000012}, {"2400", 0000013}, //
  29. {"4800", 0000014}, {"9600", 0000015}, {"19200", 0000016}, {"38400", 0000017}, //
  30. {"57600", 0010001}, {"115200", 0010002}, {"230400", 0010003}, {"460800", 0010004}, //
  31. {"500000", 0010005}, {"576000", 0010006}, {"921600", 0010007}, {"1000000", 0010010}, //
  32. {"1152000", 0010011}, {"1500000", 0010012}, {"2000000", 0010013}, {"2500000", 0010014}, //
  33. {"3000000", 0010015}, {"3500000", 0010016}, {"4000000", 0010017},
  34. };
  35. UartDevice g_uart_device = {0};
  36. void printf_buf(char *rx, ssize_t size) {
  37. for (int i = 0; i < size; i++) {
  38. printf("0x%02x,", rx[i]);
  39. }
  40. printf("\n");
  41. }
  42. void *uart_rx_thread_func(void *arg) {
  43. char buff[4096];
  44. while (true) {
  45. int ret = uartReceive(&g_uart_device, buff, 1024); // send the received text over UART
  46. if (ret < 0) {
  47. printf("uartReceive fail\n");
  48. exit(-1);
  49. }
  50. //
  51. if (ret > 0) {
  52. printf("net<-uart: %d\n", ret);
  53. printf_buf(buff, ret);
  54. }
  55. //
  56. }
  57. return NULL;
  58. }
  59. int openuart(struct UartDevice *device, const char *devname, int rate) {
  60. device->name = (char *)devname;
  61. device->rate = rate;
  62. printf("UART open %s\n", device->name);
  63. return uartStart(device, 0);
  64. }
  65. static int strtohex(const char *str, uint8_t *hex) {
  66. /**
  67. * @brief
  68. * str: 02 34 78 92 65 AF
  69. */
  70. int len = strlen(str);
  71. int i = 0;
  72. int j = 0;
  73. while (i < len) {
  74. if (str[i] == ' ') {
  75. i++;
  76. continue;
  77. }
  78. char tmp[3] = {0};
  79. tmp[0] = str[i];
  80. tmp[1] = str[i + 1];
  81. hex[j] = strtol(tmp, NULL, 16);
  82. i += 2;
  83. j++;
  84. }
  85. if(j == 0){
  86. return 0;
  87. }
  88. return j-1;
  89. }
  90. int main(int argc, char const *argv[]) {
  91. /* code */
  92. //"net_uart /dev/ttyUSB0 115200 port"
  93. if (argc != 4) {
  94. printf("Usage: %s /dev/ttyUSB0 115200 port\n", argv[0]);
  95. return -1;
  96. }
  97. printf("device name:%s\n", argv[1]);
  98. printf("baundrate :%s\n", argv[2]);
  99. printf("port :%s\n", argv[3]);
  100. auto baundrate_find_result = g_baundmap.find(argv[2]);
  101. if (baundrate_find_result == g_baundmap.end()) {
  102. printf("unsupport baundrate\n");
  103. return -1;
  104. // baundrate_find_result.
  105. };
  106. /**
  107. *
  108. */
  109. int rc = openuart(&g_uart_device, argv[1], baundrate_find_result->second);
  110. if (rc) {
  111. perror("open uart fail");
  112. exit(-1);
  113. }
  114. pthread_t uart_rx_thread;
  115. pthread_create(&uart_rx_thread, NULL, uart_rx_thread_func, NULL);
  116. while (true) {
  117. char input[1024] = {0};
  118. uint8_t hex[1024] = {0};
  119. // scanf("%s", input);
  120. fgets(input, 1024, stdin);
  121. // printf("input:%s\n", input);
  122. // 将输入的字符串转换为16进制
  123. int nhex = strtohex(input, hex);
  124. printf("tx:");
  125. printf_buf((char *)hex, nhex);
  126. // int ret = uartSend(&g_uart_device, input, strlen(input)); // send the received text over UART
  127. int ret = uartSend(&g_uart_device, (char *)hex, nhex); // send the received text over UART
  128. if (ret < 0) {
  129. printf("uartSend fail\n");
  130. } else {
  131. printf("uartSend ok\n");
  132. }
  133. }
  134. return 0;
  135. }