引入mqtt 引入cjson
This commit is contained in:
1 parent
a69c1a58d6
commit
f8591b8b99
86 files changed
+12141
-2157
No files matched your search
@@ -2,6 +2,8 @@
|
||||
|
||||
#include<stdint.h>
|
||||
#include "crc.h"
|
||||
#include "modbus-m.h"
|
||||
#include "rtthread.h"
|
||||
|
||||
// 读取保持寄存器的值
|
||||
// addr 寄存器地址
|
||||
@@ -22,6 +24,25 @@ int modbus_m_read_regist(uint8_t id,uint16_t addr,uint16_t length,uint8_t * data
|
||||
return index;
|
||||
}
|
||||
|
||||
// 读取输入的值
|
||||
// addr 寄存器地址
|
||||
// id 丛机地址
|
||||
// length 读取丛机数据长度
|
||||
int modbus_m_read_input(uint8_t id,uint16_t addr,uint16_t length,uint8_t * data)
|
||||
{
|
||||
uint16_t index = 0,crc;
|
||||
data[index++] = id;
|
||||
data[index++] = 0x04;
|
||||
data[index++] = (addr >> 8) & 0x00FF;
|
||||
data[index++] = addr & 0x00FF;
|
||||
data[index++] = (length >> 8) & 0x00FF;
|
||||
data[index++] = length & 0x00FF;
|
||||
crc = MX_CRC_CALC(data,index);
|
||||
data[index++] = crc & 0x00FF;
|
||||
data[index++] = (crc >> 8) & 0x00FF;
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
// 写入寄存器
|
||||
// addr 寄存器地址
|
||||
@@ -43,3 +64,72 @@ int modbus_m_write_regist(uint8_t id,uint16_t addr,uint16_t key,uint8_t * data)
|
||||
}
|
||||
|
||||
|
||||
//03 10 00 02 00 02 04 41 00 00 00
|
||||
// 写入寄存器
|
||||
// addr 寄存器地址
|
||||
// id 丛机地址
|
||||
// length 读取丛机数据长度
|
||||
int modbus_m_write_mregist(uint8_t id,uint16_t addr,uint8_t *key,uint16_t size ,uint8_t * data)
|
||||
{
|
||||
uint16_t index = 0,crc;
|
||||
data[index++] = id;
|
||||
data[index++] = 0x10;
|
||||
data[index++] = (addr >> 8) & 0x00FF;
|
||||
data[index++] = addr & 0x00FF;
|
||||
data[index++] = (size >> 8) & 0x00FF;
|
||||
data[index++] = size & 0x00FF;
|
||||
data[index++] = size * 2;
|
||||
for (int i =0;i < size * 2;i++)
|
||||
{
|
||||
data[index++] = key[i];
|
||||
}
|
||||
crc = MX_CRC_CALC(data,index);
|
||||
data[index++] = crc & 0x00FF;
|
||||
data[index++] = (crc >> 8) & 0x00FF;
|
||||
return index;
|
||||
}
|
||||
|
||||
// 写入寄存器
|
||||
// addr 寄存器地址
|
||||
// id 丛机地址
|
||||
// length 读取丛机数据长度
|
||||
int modbus_m_write_coil(uint8_t id,uint16_t addr,uint16_t key,uint8_t * data)
|
||||
{
|
||||
uint16_t index = 0,crc;
|
||||
data[index++] = id;
|
||||
data[index++] = 0x05;
|
||||
data[index++] = (addr >> 8) & 0x00FF;
|
||||
data[index++] = addr & 0x00FF;
|
||||
if (key == 1)
|
||||
{
|
||||
data[index++] = (addr >> 8) & 0x00FF;
|
||||
data[index++] = addr & 0x00FF;
|
||||
}
|
||||
else
|
||||
{
|
||||
data[index++] = 0x00;
|
||||
data[index++] = 0x00;
|
||||
}
|
||||
crc = MX_CRC_CALC(data,index);
|
||||
data[index++] = crc & 0x00FF;
|
||||
data[index++] = (crc >> 8) & 0x00FF;
|
||||
return index;
|
||||
}
|
||||
|
||||
|
||||
MOD_T modbus_m_crc_check(uint8_t * data,int size )
|
||||
{
|
||||
uint16_t rcrc = 0,ccrc;
|
||||
if (size < 4)
|
||||
{
|
||||
return MOD_ERR;
|
||||
}
|
||||
|
||||
ccrc = MX_CRC_CALC(data,size-2);
|
||||
rcrc = ((uint16_t)data[size-1] << 8 )+ (uint16_t)data[size-2];
|
||||
if (ccrc == rcrc )
|
||||
return MOD_OK;
|
||||
return MOD_ERR;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user