Python 用C解释Modbus ASCII中的数据
我收到MODBUS ASCII协议设备的回复 我将应答的“数据”部分存储到C中的数组中:Python 用C解释Modbus ASCII中的数据,python,c,ascii,modbus,Python,C,Ascii,Modbus,我收到MODBUS ASCII协议设备的回复 我将应答的“数据”部分存储到C中的数组中: char test_reply[10]; char buffer[255]; //contains all the reply from modbus device following ascii protocol //存储回复测试的“数据部分”\u回复固定端性 test_reply[0] = buffer[9]; test_reply[1] = buffer[10]; test_reply[2] = b
char test_reply[10];
char buffer[255]; //contains all the reply from modbus device following ascii protocol
//存储回复测试的“数据部分”\u回复固定端性
test_reply[0] = buffer[9];
test_reply[1] = buffer[10];
test_reply[2] = buffer[7];
test_reply[3] = buffer[8];
test_reply[4] = buffer[13];
test_reply[5] = buffer[14];
test_reply[6] = buffer[11];
test_reply[7] = buffer[12];
我打印了这个,我得到:
printf("this is the data reply: %s\n", test_reply)
"this is the data reply: 7B346543"
如果我得到字符串的长度,我得到8(我认为这是正确的)
我相信每一个角色都可以这样理解:
>> x = chr(0x7B) + chr(0x34) + chr(0x65) +chr(0x43)
>> y = "".join(x)
>> z = struct.unpack("<f",y)
>> print z
(229.2050,)
字符(0x37)=7
字符(0x42)=B
字符(0x33)=3
等等
现在,我必须从回复中得到一个值“229.20”
我所做的是手动将它们输入一个变量,并使用python中的命令进行计算
像这样:
>> x = chr(0x7B) + chr(0x34) + chr(0x65) +chr(0x43)
>> y = "".join(x)
>> z = struct.unpack("<f",y)
>> print z
(229.2050,)
>x=chr(0x7B)+chr(0x34)+chr(0x65)+chr(0x43)
>>y=“”.加入(x)
>>z=struct.unpack(“您正在从ModBus接收浮点229,20十六进制表示的ASCII值。您必须手动转换它们,如以下示例所示
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
uint8_t getDigit ( uint8_t digit)
{
uint8_t retVal = 0;
if ((digit >= '0') && (digit <= '9'))
{
retVal = digit - '0';
}
else if ((digit >= 'A') && (digit <= 'F'))
{
retVal = digit - 'A' + 10;
}
else if ((digit >= 'a') && (digit <= 'f'))
{
retVal = digit - 'a' + 10;
}
else
{
printf("Wrong char\n");
}
return retVal;
}
uint8_t ascii2bin (uint8_t hi, uint8_t lo)
{
uint8_t retVal = (getDigit(hi) << 4) | getDigit(lo);
return retVal;
}
int main()
{
char *ModBus_value = "33336543";
float yourFloatData;
char *special_ptr = (char *)(&yourFloatData);
int i;
int len = strlen(ModBus_value);
if ( len == (sizeof(float)*2) )
{
for (i=0; i<len; i+=2)
{
*special_ptr = ascii2bin(ModBus_value[i], ModBus_value[i+1]);
special_ptr++;
}
printf("yourFloatData_1: %f\n", yourFloatData );
}
else
{
printf("Wrong string\n");
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
uint8\u t获取数字(uint8\u t数字)
{
uint8_t retVal=0;
如果((digit>='0')&&(digit='A')&(digit='A')&(digit),那么您需要的是一种将{0x37,0x42}
的2字节序列“7B”
转换为单字节0x7b
的方法
使用x
格式的sscanf
是您所需要的。假设您知道在test\u reply中只有8个有用字符,并且您已经在平台上处理了endianness,并且希望将它们存储在32位浮点中,您可以执行以下操作:
int i;
float z;
unsigned char *ix = (unsigned char *) &z; // you can always convert any address to a char *
for(i=0; i<4; i++) {
sscanf(test_reply + 2 * i, "%2hhx", &ix[i]); // 2 to process only 2 chars, hh to store only one byte
}
// control:
printf("%f\n", z); // should output 229.205002
inti;
浮动z;
unsigned char*ix=(unsigned char*)&z;//您始终可以将任何地址转换为字符*
for(i=0;iI已经做了strtod,但是我得到了0000个值。在调用strtod之前,可能您以错误的方式存储了数据。发布您的代码。char*ptr;double val;val=strtod(test_reply,&ptr);printf(“值:%f\n”,val)
我指的是将数据存储到test\u reply
数组中的代码。不要在注释中发布代码,编辑您的问题。在printf
中,在strtod
之后使用%lf
因为您创建的是双精度,而不是浮点型(它们在Python中相同,但在C中不同)如果我错了,请纠正我,您的*ModBus_值
我的测试_回复
?@EnzoVidal是的,它是.NULL终止的。
uint8_t getDigit ( uint8_t digit)
{
uint8_t retVal = 0;
if ((digit >= '0') && (digit <= '9'))
{
retVal = digit - '0';
}
else if ((digit >= 'A') && (digit <= 'F'))
{
retVal = digit - 55;
}
else if ((digit >= 'a') && (digit <= 'f'))
{
retVal = digit - 87;
}
else
{
printf("Wrong char\n");
}
return retVal;
}
int i;
float z;
unsigned char *ix = (unsigned char *) &z; // you can always convert any address to a char *
for(i=0; i<4; i++) {
sscanf(test_reply + 2 * i, "%2hhx", &ix[i]); // 2 to process only 2 chars, hh to store only one byte
}
// control:
printf("%f\n", z); // should output 229.205002