Advertisement

校验modbus返回数据合法性

阅读量:

在处理modbus设备数据读取过程中(尤其是modbus-rtu协议),常常需要验证返回信息的准确性与合规性。自行编写校验函数通常难以达到理想效果,而modbus开源库中已提供了成熟的解决方案,现将其提取出来与大家分享。

复制代码
 int check_confirmation(uint8_t *req, uint8_t *rsp, int rsp_length)

    
 {
    
     int rc;
    
     int rsp_length_computed;
    
     const int offset = _MODBUS_RTU_HEADER_LENGTH;
    
     const int function = rsp[offset];
    
  
    
     rsp_length_computed = compute_response_length_from_request(req);
    
  
    
     /* Exception code */
    
     if (function >= 0x80) {
    
     if (rsp_length == (offset + 2 + (int)_MODBUS_RTU_CHECKSUM_LENGTH) &&
    
         req[o

全部评论 (0)

还没有任何评论哟~