luat_uart_win32.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "luat_base.h"
  2. #include "luat_mem.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_uart.h"
  5. #include "luat_log.h"
  6. #include "windows.h"
  7. #define LUAT_LOG_TAG "luat.uart"
  8. int l_uart_handler(lua_State *L, void* ptr);
  9. //检测串口存不存在
  10. int (*luat_uart_exist_extern)(int id) = NULL;
  11. //打开串口
  12. int (*luat_uart_open_extern)(int id,int br,int db, int sb, int para) = NULL;
  13. //关闭串口
  14. int (*luat_uart_close_extern)(int id) = NULL;
  15. //读取数据
  16. int (*luat_uart_read_extern)(int id,void* buff, size_t len) = NULL;
  17. //发送数据
  18. int (*luat_uart_send_extern)(int id,void* buff, size_t len) = NULL;
  19. //配置接收回调
  20. int (*luat_uart_recv_cb_extern)(int id,void (*)(int id, int len)) = NULL;
  21. //配置发送回调
  22. int (*luat_uart_sent_cb_extern)(int id,void (*)(int id, int len)) = NULL;
  23. //获取可用串口列表
  24. int (*luat_uart_get_list_extern)(uint8_t* list, size_t buff_len) = NULL;
  25. void luat_uart_initial_win32()
  26. {
  27. HMODULE module = LoadLibraryA("luat_uart.dll");
  28. if (module == NULL) {
  29. LLOGE("uart library initial error!");
  30. return;
  31. }
  32. luat_uart_exist_extern = (int (*)(int))GetProcAddress(module, "luat_uart_exist_extern");
  33. luat_uart_open_extern = (int (*)(int,int,int,int,int))GetProcAddress(module, "luat_uart_open_extern");
  34. luat_uart_close_extern = (int (*)(int))GetProcAddress(module, "luat_uart_close_extern");
  35. luat_uart_read_extern = (int (*)(int,void*,size_t))GetProcAddress(module, "luat_uart_read_extern");
  36. luat_uart_send_extern = (int (*)(int,void*,size_t))GetProcAddress(module, "luat_uart_send_extern");
  37. luat_uart_recv_cb_extern = (int (*)(int,void (*)(int, const char *, int)))GetProcAddress(module, "luat_uart_recv_cb_extern");
  38. luat_uart_sent_cb_extern = (int (*)(int,void (*)(int, int)))GetProcAddress(module, "luat_uart_sent_cb_extern");
  39. luat_uart_get_list_extern = (int (*)(uint8_t*,size_t))GetProcAddress(module, "luat_uart_get_list_extern");
  40. //FreeLibrary(module);//感觉用不到
  41. }
  42. int luat_uart_exist(int uartid)
  43. {
  44. return luat_uart_exist_extern(uartid);
  45. }
  46. int luat_uart_list(uint8_t* list, size_t buff_len){
  47. return luat_uart_get_list_extern(list,buff_len);
  48. }
  49. int luat_uart_setup(luat_uart_t* uart)
  50. {
  51. return luat_uart_open_extern(uart->id,uart->baud_rate,uart->data_bits,uart->stop_bits,uart->parity);
  52. }
  53. int luat_uart_write(int uartid, void* data, size_t length)
  54. {
  55. return luat_uart_send_extern(uartid,data,length);
  56. }
  57. int luat_uart_read(int uartid, void* buffer, size_t length)
  58. {
  59. return luat_uart_read_extern(uartid,buffer,length);
  60. }
  61. int luat_uart_close(int uartid)
  62. {
  63. return luat_uart_close_extern(uartid);
  64. }
  65. void luat_uart_recv_cb(int id, int len)
  66. {
  67. rtos_msg_t msg;
  68. msg.handler = l_uart_handler;
  69. msg.ptr = NULL;
  70. msg.arg1 = id;
  71. msg.arg2 = len;
  72. luat_msgbus_put(&msg, 1);
  73. }
  74. int luat_setup_cb(int uartid, int received, int sent) {
  75. if (!luat_uart_exist(uartid)) {
  76. LLOGW("uart id=%d not exist", uartid);
  77. return -1;
  78. }
  79. if (received) {
  80. return luat_uart_recv_cb_extern(uartid,luat_uart_recv_cb);
  81. }
  82. if (sent) {
  83. LLOGW("[waring] win32 uart lib do not support uart sent cb function %d", uartid);
  84. return 1;
  85. }
  86. return 0;
  87. }