test_tcp.py 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import asyncio
  2. import time
  3. from uhf.reader import *
  4. from tools_use import ProductInfoTokenManager, ProductImage
  5. async def tcp_client(host: str, port: int):
  6. # 创建 socket 对象
  7. reader, writer = await asyncio.open_connection(host, port)
  8. print(f"已连接到 TCP 服务器 {host}:{port}")
  9. msg = MsgBaseInventoryEpc(antennaEnable=EnumG.AntennaNo_1.value,
  10. inventoryMode=EnumG.InventoryMode_Inventory.value)
  11. msg.pack()
  12. writer.write(msg.toByte(False))
  13. await writer.drain()
  14. try:
  15. buffer = RingBuffer()
  16. while True:
  17. # 接收服务器返回的信息
  18. response = await reader.read(1024)
  19. if response:
  20. buffer.writeData(response)
  21. if buffer.dataCount < 48:
  22. continue
  23. if buffer.indexData(0) != 0x5A:
  24. buffer.cleanData(1 * 8)
  25. continue
  26. if buffer.dataCount >= 56:
  27. temp = DynamicBuffer()
  28. temp.putBytes(buffer.readData(40))
  29. dataLen = buffer.readBit(16)
  30. if dataLen != 0:
  31. temp.putShort(dataLen)
  32. if (dataLen + 2) * 8 > buffer.dataCount:
  33. if dataLen > 1024:
  34. buffer.cleanData(1 * 8)
  35. else:
  36. buffer.subPos(temp.len)
  37. continue
  38. else:
  39. temp.putBytes(buffer.readData(dataLen * 8))
  40. data = buffer.readData(16)
  41. temp.putBytes(data)
  42. msg = Message().new(temp.hex)
  43. print(msg.msgId)
  44. if msg:
  45. if msg.checkCrc():
  46. buffer.cleanAll()
  47. if msg.mt_8_11 == EnumG.Msg_Type_Bit_Base.value:
  48. if msg.msgId == EnumG.BaseLogMid_Epc.value:
  49. info = LogBaseEpcInfo()
  50. info.cData = msg.cData
  51. info.unPack()
  52. print("收到服务器消息:", info.epc)
  53. product_info_instance = ProductInfoTokenManager()
  54. product_image_instance = ProductImage()
  55. barcode = await product_info_instance.get_barcode_from_epc(info.epc)
  56. product_info = await product_info_instance.get_product_info_by_barcode(barcode)
  57. product_image = await product_image_instance.get_product_image_by_barcode(barcode)
  58. response_data = {
  59. "code": 1,
  60. "message": "ok",
  61. "data": {
  62. "barcode": barcode,
  63. "product_info": product_info,
  64. "product_image": product_image
  65. }
  66. }
  67. print(f'response_data*******{response_data["code"]}')
  68. mess = MsgAppGetBaseVersion()
  69. mess.pack()
  70. writer.write(mess.toByte(False))
  71. await writer.drain()
  72. elif msg.mt_8_11 == EnumG.Msg_Type_Bit_App.value:
  73. if msg.msgId == EnumG.AppMid_Heartbeat.value:
  74. writer.write(msg.msgData)
  75. else:
  76. print('ok4 \n')
  77. mess = MsgAppGetBaseVersion()
  78. mess.pack()
  79. writer.write(mess.toByte(False))
  80. await writer.drain()
  81. except Exception as e:
  82. print("连接或通信过程中发生错误:", e)
  83. finally:
  84. print('finally')
  85. writer.close()
  86. await writer.wait_closed()
  87. print("连接已关闭")
  88. if __name__ == "__main__":
  89. asyncio.run(tcp_client('10.41.191.107', 8160)) # 根据实际IP和端口修改
  90. print(MsgAppGetBaseVersion())