12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- #ifndef SERIALSCF_H
- #define SERIALSCF_H
- #define SERIALSCF_API
- #define SERIALSCF_C_API extern "C"
- #include "SCF.h"
- #include "ResDataObject.h"
- #include <string>
- #include <termios.h>
- #include <fcntl.h>
- #include <unistd.h>
- SERIALSCF_C_API SCF* GetSCF();
- SERIALSCF_C_API void ReleaseSCF(SCF* p);
- // 串口通信类 (Linux实现)
- class SERIALSCF_API SerialSCF : public SCF {
- int hCom; // 串口文件描述符
- std::string m_PortName;
- uint32_t m_BaudRate;
- uint32_t m_ByteSize;
- uint32_t m_Parity;
- uint32_t m_StopBits;
- uint32_t m_ReadTimeout;
- uint32_t m_WriteTimeout;
- // Linux串口配置结构
- termios m_OriginalTermios;
- termios m_CurrentTermios;
- int ConnectCOM(const char* pCom, uint32_t BaudRate, uint32_t ByteSize,
- uint32_t Parity, uint32_t StopBits);
- public:
- SerialSCF(void);
- virtual ~SerialSCF();
- /**
- * 连接到串口设备
- * @param Connectprameters 连接参数JSON对象
- * @param callback 数据包解析回调函数
- * @param TransferType 传输类型 (包传输或原始传输)
- * @param CommunicateTimeout 通信超时时间(毫秒)
- * @return 连接结果 (SCF_ERR枚举值)
- *
- * 参数示例:
- * {
- * "connect":"COM",
- * "port":"/dev/ttyS0", // Linux串口设备路径
- * "baudrate":"115200", // 波特率
- * "bytesize":"8", // 数据位
- * "parity":"0", // 校验位 (0=None, 1=Odd, 2=Even)
- * "stopbits":"1", // 停止位 (1=1位, 2=2位)
- * }
- */
- int Connect(ResDataObject& Connectprameters, PacketParser callback = NULL,
- SCF_TRANSFERTYPE TransferType = SCF_NORMAL_TRANSFER,
- uint32_t CommunicateTimeout = 0);
- /// 断开串口连接
- void Disconnect();
- /// 检查是否已连接
- bool isConnected();
- /// 获取连接类型 ("COM")
- const char* GetConnectionType();
- /// 发送原始数据包
- int SendPacket(const char* pPacket, uint32_t length, uint32_t timeout);
- /// 发送SCF数据包对象
- int SendPacket(SCFPacket* pPacket, uint32_t timeout);
- protected:
- /**
- * 从串口读取数据
- * @param pPacket 数据缓冲区
- * @param length 缓冲区长度
- * @param timeout 读取超时时间(毫秒)
- * @return 实际读取的字节数,或错误代码(SCF_ERR枚举值)
- */
- int ReadData(char* pPacket, uint32_t length, uint32_t timeout);
- };
- #endif // SERIALSCF_H
|