VB用Modbus RTU协议和仪表通讯的问题 VB6.0 PC与智能仪表COM通讯modbus协议485

\u57fa\u4e8emodbus rtu\u901a\u8baf\u534f\u8bae\u7684\u591a\u667a\u80fd\u4eea\u8868\u901a\u8baf

\u60a8\u597d\uff0cCRC\u7f16\u7801\u6821\u9a8c\u8bef\u7801\u7387\u6781\u4f4e\uff0c\u662f\u4e00\u79cd\u6bd4\u8f83\u53ef\u9760\u7684\u68c0\u9a8c\u65b9\u5f0f\u3002\u8be5\u6821\u9a8c\u7801\u662f\u901a\u8fc7\u6570\u636e\u5e8f\u5217\u5bf9\u751f\u6210\u591a\u9879\u5f0f\u8fdb\u884c\u4e8c\u8fdb\u5236\u9664\u6cd5\u53d6\u4f59\u6570\u5f97\u5230\u7684\u3002\u91c7\u7528CRC-16\u6821\u9a8c\uff0c\u751f\u6210\u7684\u591a\u9879\u5f0f\u4e3aX16\u2191+ X15\u2191+ X2\u2191+ X1\u2191\u3002
CRC\u57df\u662f\u4e24\u4e2a\u5b57\u8282\uff0c\u5305\u542b\u4e0016\u4f4d\u7684\u4e8c\u8fdb\u5236\u503c\u3002\u5b83\u7531\u4f20\u8f93\u8bbe\u5907\u8ba1\u7b97\u540e\u52a0\u5165\u5230\u6d88\u606f\u4e2d\u3002\u63a5\u6536\u8bbe\u5907\u91cd\u65b0\u8ba1\u7b97\u6536\u5230\u6d88\u606f\u7684CRC\uff0c\u5e76\u4e0e\u63a5\u6536\u5230\u7684CRC\u57df\u4e2d\u7684\u503c\u6bd4\u8f83\uff0c\u5982\u679c\u4e24\u503c\u4e0d\u540c\uff0c\u5219\u6709\u8bef\u3002 CRC\u662f\u5148\u8c03\u5165\u4e00\u503c\u662f\u5168\u201c1\u201d\u768416\u4f4d\u5bc4\u5b58\u5668\uff0c\u7136\u540e\u8c03\u7528\u4e00\u8fc7\u7a0b\u5c06\u6d88\u606f\u4e2d\u8fde\u7eed\u76848\u4f4d\u5b57\u8282\u5404\u5f53\u524d\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\u8fdb\u884c\u5904\u7406\u3002
CRC\u4ea7\u751f\u8fc7\u7a0b\u4e2d\uff0c\u6bcf\u4e2a8\u4f4d\u5b57\u7b26\u90fd\u5355\u72ec\u548c\u5bc4\u5b58\u5668\u5185\u5bb9\u76f8\u6216\uff08OR\uff09\uff0c\u7ed3\u679c\u5411\u6700\u4f4e\u6709\u6548\u4f4d\u65b9\u5411\u79fb\u52a8\uff0c\u6700\u9ad8\u6709\u6548\u4f4d\u4ee50\u586b\u5145\u3002LSB\u88ab\u63d0\u53d6\u51fa\u6765\u68c0\u6d4b\uff0c\u5982\u679cLSB\u4e3a1\uff0c\u5bc4\u5b58\u5668\u5355\u72ec\u548c\u9884\u7f6e\u7684\u503c\u6216\u4e00\u4e0b\uff0c\u5982\u679cLSB\u4e3a0\uff0c\u5219\u4e0d\u8fdb\u884c\u3002\u6574\u4e2a\u8fc7\u7a0b\u8981\u91cd\u590d8\u6b21\u3002\u5728\u6700\u540e\u4e00\u4f4d\uff08\u7b2c8\u4f4d\uff09\u5b8c\u6210\u540e\uff0c\u4e0b\u4e00\u4e2a8\u4f4d\u5b57\u8282\u53c8\u5355\u72ec\u548c\u5bc4\u5b58\u5668\u7684\u5f53\u524d\u503c\u76f8\u6216\u3002\u6700\u7ec8\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\uff0c\u662f\u6d88\u606f\u4e2d\u6240\u6709\u7684\u5b57\u8282\u90fd\u6267\u884c\u4e4b\u540e\u7684CRC\u503c\u3002\u4e0b\u9762\u662f\u5b83\u7684VC\u4ee3\u7801\uff1a
WORD GetCheckCode(const char * pSendBuf, int nEnd)//\u83b7\u5f97\u6821\u9a8c\u7801 {
WORD wCrc = WORD(0xFFFF); for(int i=0; i {
wCrc ^= WORD(BYTE(pSendBuf[i])); for(int j=0; j {
if(wCrc & 1) {
wCrc >>= 1;
wCrc ^= 0xA001; } else {
wCrc >>= 1; } } }
return wCrc; }

\u4f60\u5728VB\u4e2d\u901a\u8fc7\u4e32\u53e3\u7c7b\u4e2d\u7684\u65b9\u6cd5\u5411\u4eea\u8868\u53d1\u9001MODBUS\u6307\u4ee4\uff0c\u7136\u540e\u89e3\u6790\u8fd4\u56de\u6570\u636e\u5373\u53ef\u3002MODBUS\u8bfb\u5bc4\u5b58\u5668\u6307\u4ee4\uff0c\u4f60\u95ee\u9898\u4e2d\u7684\u8868\u53f7\uff0c\u4e00\u822c\u53eb\u505a\u8bbe\u5907ID\uff0c\u8bbe\u5907ID\u662f\u4e00\u4e2a\u5b57\u8282\uff0c\u4ece0\u5230255\uff0c0\u4e00\u822c\u4e0d\u7528\uff0c\u6bd4\u5982\u8bbe\u5907ID\u662f1\uff0c\u8bfb\u53d64XXX\u5bc4\u5b58\u5668\u7684\u6307\u4ee4\u662f \u5341\u516d\u8fdb\u523603\uff0c\u8d77\u59cb\u5730\u5740\u662f1037\uff08\u524d\u9762\u76844\u53bb\u6389\uff09\uff0c\u8fde\u7eed\u8bfb\u53d6\u4e24\u4e2a\u5bc4\u5b58\u5668\uff0c\u4e5f\u5c31\u662f\u5bc4\u5b58\u5668\u957f\u5ea6\u4e3a2\uff0c\u6bcf\u4e2a\u5bc4\u5b58\u5668\u4e24\u4e2a\u5b57\u8282\uff0c\u8fd4\u56de\u6570\u636e\u5c31\u5e94\u8be5\u662f\u56db\u4e2a\u5b57\u8282\uff0c\u6700\u540e\u662f\u4e24\u4e2a\u5b57\u8282\u7684CRC16\u6821\u9a8c\u7801\u3002\u6307\u4ee4\u662f\u8fd9\u6837\u7684
01\uff08\u8bbe\u5907ID\uff09 03\uff08\u8bfb\u5bc4\u5b58\u5668\uff09 10 37\uff08\u8d77\u59cb\u5730\u5740\uff09 00 02\uff08\u5bc4\u5b58\u5668\u4e2a\u6570\uff09 CL CH\uff08CRC16\u6821\u9a8c\u7801\uff09
CRC16\u6821\u9a8c\u7801\u6211\u6ca1\u6cd5\u7ed9\u4f60\u7b97\uff0c\u4f60\u9700\u8981\u5728\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u6307\u4ee4\u7684\u91cd\u7ec4\u548cCRC16\u6821\u9a8c\u7801\u7684\u751f\u6210\uff0c\u89e3\u6790\u65f6\uff0c\u5728\u8fd4\u56de\u6570\u636e\u7684\u6307\u5b9a\u4f4d\u7f6e\u622a\u53d6\u4e0b\u8fde\u7eed\u7684\u56db\u4e2a\u5b57\u8282\u6570\u636e\uff0c\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u5373\u53ef\u3002VB\u4e2d\u90fd\u6709\u76f8\u5e94\u7684\u7c7b\u548c\u65b9\u6cd5\uff0c\u4e0d\u4fbf\u8be6\u8ff0\u3002

哦,这个问题比较简单。
原因是在你的VB6代码中,你使用 Integer 类型的变量设置这个参数了。
在 VB6 中,Integer是有符号的,其取值范围是 -32767 到 32767。
所以,用 Long 来替换 Integer 吧。

扩展阅读:modbus rtu slave ... modbus rtu轮询方式 ... modbus rtu over tcp ... modbus-rtu通讯协议 ... modbus rtu接线方式 ... modbus rtu转以太网 ... modbus rtu奇偶校验 ... modbus rtu接线图 ... 专门生产modbus rtu ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网