SerialSCF.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef SERIALSCF_H
  2. #define SERIALSCF_H
  3. #define SERIALSCF_API
  4. #define SERIALSCF_C_API extern "C"
  5. #include "SCF.h"
  6. #include "ResDataObject.h"
  7. #include <string>
  8. #include <termios.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. SERIALSCF_C_API SCF* GetSCF();
  12. SERIALSCF_C_API void ReleaseSCF(SCF* p);
  13. // 串口通信类 (Linux实现)
  14. class SERIALSCF_API SerialSCF : public SCF {
  15. int hCom; // 串口文件描述符
  16. std::string m_PortName;
  17. uint32_t m_BaudRate;
  18. uint32_t m_ByteSize;
  19. uint32_t m_Parity;
  20. uint32_t m_StopBits;
  21. uint32_t m_ReadTimeout;
  22. uint32_t m_WriteTimeout;
  23. // Linux串口配置结构
  24. termios m_OriginalTermios;
  25. termios m_CurrentTermios;
  26. int ConnectCOM(const char* pCom, uint32_t BaudRate, uint32_t ByteSize,
  27. uint32_t Parity, uint32_t StopBits);
  28. public:
  29. SerialSCF(void);
  30. virtual ~SerialSCF();
  31. /**
  32. * 连接到串口设备
  33. * @param Connectprameters 连接参数JSON对象
  34. * @param callback 数据包解析回调函数
  35. * @param TransferType 传输类型 (包传输或原始传输)
  36. * @param CommunicateTimeout 通信超时时间(毫秒)
  37. * @return 连接结果 (SCF_ERR枚举值)
  38. *
  39. * 参数示例:
  40. * {
  41. * "connect":"COM",
  42. * "port":"/dev/ttyS0", // Linux串口设备路径
  43. * "baudrate":"115200", // 波特率
  44. * "bytesize":"8", // 数据位
  45. * "parity":"0", // 校验位 (0=None, 1=Odd, 2=Even)
  46. * "stopbits":"1", // 停止位 (1=1位, 2=2位)
  47. * }
  48. */
  49. int Connect(ResDataObject& Connectprameters, PacketParser callback = NULL,
  50. SCF_TRANSFERTYPE TransferType = SCF_NORMAL_TRANSFER,
  51. uint32_t CommunicateTimeout = 0);
  52. /// 断开串口连接
  53. void Disconnect();
  54. /// 检查是否已连接
  55. bool isConnected();
  56. /// 获取连接类型 ("COM")
  57. const char* GetConnectionType();
  58. /// 发送原始数据包
  59. int SendPacket(const char* pPacket, uint32_t length, uint32_t timeout);
  60. /// 发送SCF数据包对象
  61. int SendPacket(SCFPacket* pPacket, uint32_t timeout);
  62. protected:
  63. /**
  64. * 从串口读取数据
  65. * @param pPacket 数据缓冲区
  66. * @param length 缓冲区长度
  67. * @param timeout 读取超时时间(毫秒)
  68. * @return 实际读取的字节数,或错误代码(SCF_ERR枚举值)
  69. */
  70. int ReadData(char* pPacket, uint32_t length, uint32_t timeout);
  71. };
  72. #endif // SERIALSCF_H