




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、.modbus(c语言程序)2009-10-15 09:31:28|分类:默认分类|字号订阅#include #include/_nop_();#define osfreq 11059200char timeinterval;char myaddress;char data di6=1,2,3,4,5,6;char data do6=6,7,8,9,10,11;char data ai6=11,12,13,14,15,16;char data ao6=16,17,18,19,20,21;unsigned char data receivedata14;unsigned char data co
2、untnumber;sbit recenable =p16;/控制端 sbit led =p27;/控制端/*-函数说明:crc 高位字节值表-*/static unsigned char code auchcrchi = 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0,0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41,0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0,0x80, 0x41,
3、 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40,0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1,0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41,0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1,0x81, 0x40, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41,0x00, 0xc1, 0x81
4、, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0,0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40,0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1,0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40,0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0,0x80, 0x41, 0x00, 0xc
5、1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40,0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0,0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40,0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0,精品.0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41,0x00, 0xc1, 0x81, 0x40,
6、 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0,0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41,0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0,0x80, 0x41, 0x00, 0xc1, 0x81, 0x40, 0x00, 0xc1, 0x81, 0x40,0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, 0x80, 0x41, 0x00, 0xc1,0x81, 0x40, 0x00, 0xc1, 0x81
7、, 0x40, 0x01, 0xc0, 0x80, 0x41,0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0,0x80, 0x41, 0x00, 0xc1, 0x81, 0x40 ;/*-函数说明:crc低位字节值表-*/static unsigned char code auchcrclo = 0x00, 0xc0, 0xc1, 0x01, 0xc3, 0x03, 0x02, 0xc2, 0xc6, 0x06,0x07, 0xc7, 0x05, 0xc5, 0xc4, 0x04, 0xcc, 0x0c, 0x0d, 0xc
8、d,0x0f, 0xcf, 0xce, 0x0e, 0x0a, 0xca, 0xcb, 0x0b, 0xc9, 0x09,0x08, 0xc8, 0xd8, 0x18, 0x19, 0xd9, 0x1b, 0xdb, 0xda, 0x1a,0x1e, 0xde, 0xdf, 0x1f, 0xdd, 0x1d, 0x1c, 0xdc, 0x14, 0xd4,0xd5, 0x15, 0xd7, 0x17, 0x16, 0xd6, 0xd2, 0x12, 0x13, 0xd3,0x11, 0xd1, 0xd0, 0x10, 0xf0, 0x30, 0x31, 0xf1, 0x33, 0xf3,0xf
9、2, 0x32, 0x36, 0xf6, 0xf7, 0x37, 0xf5, 0x35, 0x34, 0xf4,0x3c, 0xfc, 0xfd, 0x3d, 0xff, 0x3f, 0x3e, 0xfe, 0xfa, 0x3a,0x3b, 0xfb, 0x39, 0xf9, 0xf8, 0x38, 0x28, 0xe8, 0xe9, 0x29,0xeb, 0x2b, 0x2a, 0xea, 0xee, 0x2e, 0x2f, 0xef, 0x2d, 0xed,0xec, 0x2c, 0xe4, 0x24, 0x25, 0xe5, 0x27, 0xe7, 0xe6, 0x26,0x22, 0x
10、e2, 0xe3, 0x23, 0xe1, 0x21, 0x20, 0xe0, 0xa0, 0x60,0x61, 0xa1, 0x63, 0xa3, 0xa2, 0x62, 0x66, 0xa6, 0xa7, 0x67,0xa5, 0x65, 0x64, 0xa4, 0x6c, 0xac, 0xad, 0x6d, 0xaf, 0x6f,0x6e, 0xae, 0xaa, 0x6a, 0x6b, 0xab, 0x69, 0xa9, 0xa8, 0x68,0x78, 0xb8, 0xb9, 0x79, 0xbb, 0x7b, 0x7a, 0xba, 0xbe, 0x7e,0x7f, 0xbf, 0
11、x7d, 0xbd, 0xbc, 0x7c, 0xb4, 0x74, 0x75, 0xb5,0x77, 0xb7, 0xb6, 0x76, 0x72, 0xb2, 0xb3, 0x73, 0xb1, 0x71,0x70, 0xb0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9c, 0x5c,精品.0x5d, 0x9d, 0x5f, 0x9f, 0x9e, 0x5e, 0x5a, 0x9a, 0x9b, 0x5b,0x99, 0x59, 0x5
12、8, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4b, 0x8b,0x8a, 0x4a, 0x4e, 0x8e, 0x8f, 0x4f, 0x8d, 0x4d, 0x4c, 0x8c,0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,0x43, 0x83, 0x41, 0x81, 0x80, 0x40;/*-调用方式:unsigned int crc16(unsigned char *puchmsg, unsigned int usdatalen)函数说明:crc校验-*/unsigned int crc
13、16(unsigned char *puchmsg, unsigned int usdatalen) unsigned char uchcrchi = 0xff ; / 高crc字节初始化 unsigned char uchcrclo = 0xff ; / 低crc 字节初始化 unsigned uindex ; / crc循环中的索引 while (usdatalen-) / 传输消息缓冲区 uindex = uchcrchi *puchmsg+ ; / 计算crc uchcrchi = uchcrclo auchcrchiuindex ; uchcrclo = auchcrclouinde
14、x ; return (uchcrchi 3); temp=*(myaddress+temp); temp=(offset); if (temp&0x01) return 1; else return 0;/*-精品.调用方式:void senddata(unsigned char *output,unsigned char outlength)函数说明:发送数据至窗口-*/void senddata(unsigned char *output,unsigned char outlength) es=0; while(outlength-) ti=0; acc=*output; tb8=p;
15、sbuf=*(output+); while(!ti); ti=0; es=1;/*-调用方式:void function12(unsigned char address,unsigned char len)函数说明:功能码1,2处理-*/void function12(unsigned char address,unsigned char len) unsigned int data i; unsigned char data j; unsigned char data length; unsigned char data *send; unsigned int data temp; uns
16、igned char data function; length=0; send=receivedata; function=send1; for (i=1;i=len;i+) length+; *(send+2+length)=0; for (j=0;j8;j+) *(send+2+length)=*(send+2+length)|getbit(address,function)len)break; i-; *(send+2)=length; /精品.数据长度 temp=crc16(send,length+3); /di状态数据 *(send+3+length)=(char)(temp8);
17、 /crc校验高 *(send+4+length)=(char)temp; /crc校验低 senddata(send,length+5); /调用a发送程序/*-调用方式:void function3(unsigned char address,unsigned char len)函数说明:功能码3处理-读取寄存器-*/void function3(unsigned char address,unsigned char len) unsigned char data i; unsigned char data *send; unsigned int data temp; send=recei
18、vedata; *(send+2)=2*len; /数据长度高 address=2*address; for(i=0;i8); *(send+4+2*len)=(char)temp; senddata(send,2*len+5); /调用a发送程序/*-调用方式:void function4(unsigned char address,unsigned char len)函数说明:功能码处理4-读取输入寄存器-*/void function4(unsigned char address,unsigned char len) unsigned char data i; unsigned char
19、 data *send; unsigned int data temp; send=receivedata; *(send+2)=2*len; address=2*address; for(i=0;i8); *(send+4+2*len)=(char)temp; senddata(send,2*len+5);/*-调用方式:void function6(unsigned char address)函数说明:写单路寄存器-*/void function6(unsigned char address) int temp; unsigned char data *writedata; temp=2*
20、address; writedata=ao; /将写入的数据进行处理 /your code to add here to deal with the write value *(writedata+temp)=receivedata4; *(writedata+temp+1)=receivedata5; senddata(receivedata,countnumber); /调用发送程序,返回与主机相同的报文/*-调用方式:void senderror(char errorcode)函数说明:-*/void senderror(char errorcode) unsigned char dat
21、a *send; unsigned int data temp; send=receivedata; *(send+1)=receivedata1|0x01;/最高位置1 *(send+2)=errorcode; temp=crc16(send,3); *(send+3)=(char)(temp8); *(send+4)=(char)temp; senddata(send,countnumber); /精品.调用发送程序,返回错误代码/*-调用方式:void deal()函数说明:接收数据处理-*/void deal() unsigned int data temp; unsigned cha
22、r data address; unsigned char data length; unsigned char data counter; unsigned char data *pointer; pointer=receivedata; counter=countnumber; if (counter=3 ) return; temp=pointercounter-28; temp= temp|pointercounter-1; if( temp=crc16(pointer,counter-2) /较验正确 address=pointer3; length=pointer5; if(add
23、ressnumber&0x01=1) return 1; else return 0;/设定某一位的值*unsigned char setbit(unsigned char character,unsigned char num,bit boolen) unsigned char code bit_value=1,2,4,8,16,32,64,128; if(boolen) return character|bit_valuenum; else return character&bit_valuenum;*/*-调用方式:void init_timer2(unsigned int baudra
24、te)函数说明:定时器2的初始化-*/void init_timer2(unsigned int baudrate) unsigned int data timreg2; t2con = 0x00; t2mod = 0x00; timreg2=65536-(osfreq/384/baudrate*11); rcap2l=timreg2&0x00ff; rcap2h=(timreg28); /rcap2h=timreg2/256 rcap2l=0;rcap2h=0; tl2 = rcap2l; / th2 = rcap2h; / tr2=1; / t2con.2 start timer精品. et2=0; / 开定时器2中断/*-调用方式:void init_serialport(unsigned int baudrate)函数说明:串口初始化-*/void init_serialport(unsigned int baudrate) unsigned char timreg2; scon = 0xd0; / 方式3, 8-bit uart, enable rcvr tmod = 0x21; / t0方式1(16位), t1方式2,8位重装
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 社区慢病管理方法
- 2025年德育个人工作方案幼儿园演讲稿
- 护理学休克病人的急救护理
- 合同履行监督与评估指南
- 术后谵妄护理个案
- 保育员培训配合教育活动
- 神达电脑人力资源机构组织
- 滨州职业学院《功能高分子》2023-2024学年第二学期期末试卷
- 内蒙古鸿德文理学院《电视演播室》2023-2024学年第二学期期末试卷
- 安徽卫生健康职业学院《形势与政策Ⅲ》2023-2024学年第一学期期末试卷
- 防爆蓄电池单轨吊机车运行安全技术措施完整版
- 《用户体验测试》课件
- 隔离与防护措施的正确应用
- 高血压问卷设计(知信行模式)
- 2023年北京市丰台区初三英语一模试题及答案
- 2023青海省安全员《C证》考试题库
- 职业病危害告知书
- TRIZ理论――创新方法课件
- CORN术中获得性压力性损伤风险评估量表评定细则解读
- 预毕业证明(共5篇)
- 中国大唐集团公司以热率为核心能耗管理指导意见
评论
0/150
提交评论