欢迎来到天天文库
浏览记录
ID:41123810
大小:92.00 KB
页数:17页
时间:2019-08-17
《VB MODBUS实现源码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、OptionExplicitPublicbln_busyAsBooleanPublicbln_successAsBooleanPublicbln_readwordAsBooleanPublicDeclareFunctiontimeGetTimeLib"winmm.dll"()AsLongPublicSubtran_modbus_order(ByValbyt_slv_idAsByte,ByValbyt_funcAsByte,ByValaddrAsLong,byt_data()AsByte)Dimtrans_byte()
2、AsByteDimiAsIntegerDimjAsIntegerDimkAsIntegerDimIndexAsIntegerDimCRC()AsByteDimtempAsIntegerDimlenthAsIntegerDimlenth1AsIntegerSelectCasebyt_funcCase1ReDimtrans_byte(7)AsBytetrans_byte(0)=byt_slv_idtrans_byte(1)=1trans_byte(2)=(addr-1)256trans_byte(3)=(addr-1)
3、Mod256trans_byte(4)=0trans_byte(5)=byt_data(0)CRC=CRC16(trans_byte)trans_byte(6)=CRC(0)trans_byte(7)=CRC(1)Case3ReDimtrans_byte(7)AsBytetrans_byte(0)=byt_slv_idtrans_byte(1)=3trans_byte(2)=(addr-1)256trans_byte(3)=(addr-1)Mod256trans_byte(4)=0trans_byte(5)=byt
4、_data(0)CRC=CRC16(trans_byte)trans_byte(6)=CRC(0)trans_byte(7)=CRC(1)Case6ReDimtrans_byte(7)AsBytetrans_byte(0)=byt_slv_idtrans_byte(1)=6trans_byte(2)=(addr-1)256trans_byte(3)=(addr-1)Mod256trans_byte(4)=byt_data(0)trans_byte(5)=byt_data(1)CRC=CRC16(trans_byte
5、)trans_byte(6)=CRC(0)trans_byte(7)=CRC(1)Case15lenth=UBound(byt_data)+1lenth1=(lenth-1)8+1ReDimtrans_byte(8+lenth1)trans_byte(0)=byt_slv_idtrans_byte(1)=&HFtrans_byte(2)=(addr-1)256trans_byte(3)=(addr-1)Mod256trans_byte(4)=0trans_byte(5)=lenthtrans_byte(6)=le
6、nth1k=0Index=7temp=0Fori=1Tolenthtemp=temp+byt_data(i-1)*(2^k)If(iMod8=0)Thentrans_byte(Index)=CByte(temp)Index=Index+1temp=0k=0EndIfk=k+1Nextitrans_byte(Index)=CByte(temp)CRC=CRC16(trans_byte)trans_byte(7+lenth1)=CRC(0)trans_byte(8+lenth1)=CRC(1)Case16lenth=UB
7、ound(byt_data)+1ReDimtrans_byte(8+lenth)trans_byte(0)=byt_slv_idtrans_byte(1)=&H10trans_byte(2)=(addr-1)256trans_byte(3)=(addr-1)Mod256trans_byte(4)=0trans_byte(5)=lenth2trans_byte(6)=lenthFori=0Tolenth-1trans_byte(7+i)=byt_data(i)NextiCRC=CRC16(trans_byte)tr
8、ans_byte(7+lenth)=CRC(0)trans_byte(8+lenth)=CRC(1)EndSelectfrm_main.com_modbus.Output=trans_byteDimts_iAsIntegerDimts_strAsStringts_str="send:"Forts_i=0ToUBound(trans_byte)t
此文档下载收益归作者所有