微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

计算 RS232 连接到燃油表的校验和

如何解决计算 RS232 连接到燃油表的校验和

我正在构建一个 Python 应用程序来与燃油表进行通信,并且通信协议要求每个数据包都有一个校验和。我不知道 C(是的,我应该学习)并且示例是用 C 提供的,我正在尝试对其进行逆向工程,以便我可以用 Python 重写它。

我不知道在下面的示例中提供了实际输入?

一个示例命令在 ASCII 中看起来像这样,_Q10013 然后是 2 字节校验和,然后是回车。

示例 C 代码如下所示:

//! Adjust a byte of checksum for packet to the dfv(emh)
//! @sa CheckSum_emh
uint8_t CheckSumAdjust_emhrx(uint8_t before) 
{ 
    uint8_t after = before;
    if (before == 0x00) after = 0x01;
    if (before == 0x0D) after = 0x0E;
    if (before == 0x5F) after = 0x60;
    return after;
}

//! Calculate checksum on packet transmitted to the dfv(emh)
//! @param ascii_buff Characters in the message to calculate checksum for
//! @param last_char Buffer index of the last character to perform a checksum on.
//! @param chksum Array of where to put the resulting checksum.
void CheckSum_emh(uint8_t ascii_buff[],uint8_t last_char,uint8_t checksum[2])
{
    char i;
    int16_t check_sum;
    check_sum = 0;
    for (i = 0; i <= last_char; i++)
    { 
        check_sum = check_sum + (int16_t)ascii_buff[i];
    } 
    checksum[0] = CheckSumAdjust_emhrx((check_sum & 0xFF00) >> 8);
    checksum[1] = CheckSumAdjust_emhrx(check_sum & 0x00FF);
}
//! end checksum

实际的字符串或十六进制在哪里提供给函数

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。