计算 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 (将#修改为@)