请问一下各位关于modbus/tcp的问题

m0_37580103 2018-07-05 08:59:05
1.我写了一个tcp服务器的程序,程序应该是没有问题的,但是老提示网口打开失败。。是不是因为我用的是校园网,没有固定的物理IP,不知道测程序的时候怎么配置自己的电脑,,最好有附图说明。。
2.另外问一下各位有没有CRC-CCITT的c语言代码,最好是计算法不是查表法的,谢谢
...全文
210 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-07-06
  • 打赏
  • 举报
回复
/***** crc16.c *****/
#include <stdio.h>

#define CRC16_DNP 0x3D65u // DNP, IEC 870, M-BUS, wM-BUS, ...
#define CRC16_CCITT 0x1021u // X.25, V.41, HDLC FCS, Bluetooth, ...

//Other polynoms not tested
#define CRC16_IBM 0x8005u // ModBus, USB, Bisync, CRC-16, CRC-16-ANSI, ...
#define CRC16_T10_DIF 0x8BB7u // SCSI DIF
#define CRC16_DECT 0x0589u // Cordeless Telephones
#define CRC16_ARINC 0xA02Bu // ACARS Aplications

#define POLYNOM CRC16_DNP // Define the used polynom from one of the aboves

// Calculates the new crc16 with the newByte. Variable crcValue is the actual or initial value (0).
unsigned short crc16(unsigned short crcValue, unsigned char newByte) {
int i;

for (i = 0; i < 8; i++) {
if (((crcValue & 0x8000u) >> 8) ^ (newByte & 0x80u)){
crcValue = (crcValue << 1) ^ POLYNOM;
} else {
crcValue = (crcValue << 1);
}
newByte <<= 1;
}
return crcValue;
}

unsigned short exampleOfUseCRC16(unsigned char *Data, int len){

unsigned short crc;
int aux = 0;

crc = 0x0000u; //Initialization of crc to 0x0000 for DNP
//crc = 0xFFFFu; //Initialization of crc to 0xFFFF for CCITT

while (aux < len){
crc = crc16(crc,Data[aux]);
aux++;
}

return (~crc); //The crc value for DNP it is obtained by NOT operation

//return crc; //The crc value for CCITT
}

int main() {
unsigned char d[10]={0,1,2,3,4,5,6,7,8,9};

printf("0x%04hX\n",exampleOfUseCRC16(d,10));//0x1078
return 0;
}
zgl7903 2018-07-05
  • 打赏
  • 举报
回复
在同一台电脑上同时运行服务器端和客户端程序, 就可以测试 (IP地址127.0.0.1 )
查表法速度是最快的
http://djb69.blog.163.com/blog/static/6776323420100158391490/

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧