Modbus通讯协议编程本人最近为了实现电脑与DeltaVFD-M变频器通讯,特意用VB6.0编了一个Modbus协议通讯软件,不过这只是一个测试版,但Modbus的ASCii协议和RTU协议都已经实现。现在将源程序上贴,希望可以帮助到有需要的朋友,谢谢!(我发现图片贴不上去)另外,假如你觉得有更好的想法,欢迎E-mail指教。附:VB6源程序OptionExplicitPrivateText1textAsStringPrivateRTUCRCAsString'串口选择PrivateSubCombo1_Click()MSComm1.CommPort=Combo1.ListIndex+1EndSub'数据位改变PrivateSubCombo2_Click()CallsettingEndSub'波特率改变PrivateSubCombo3_Click()CallsettingEndSub'奇偶校验改变PrivateSubCombo4_Click()CallsettingEndSub'停止位改变PrivateSubCombo5_Click()CallsettingEndSubPrivateSubsetting()MSComm1.Settings=CStr(Combo3.Text)&,&CStr(Combo4.Text)&,&CStr(Combo2.Text)_&,&CStr(Combo5.Text)EndSub'打开关闭串口PrivateSubCommand1_Click()OnErrorResumeNextIfMSComm1.PortOpen=FalseThenMSComm1.PortOpen=TrueElseMSComm1.PortOpen=FalseEndIfIfMSComm1.PortOpenThen'打开关闭按钮显示文字及combo1使能Command1.Caption=关闭串口Combo1.Enabled=FalseElseCommand1.Caption=打开串口Combo1.Enabled=TrueEndIfIfErrThen'打开串口失败,则显示出错信息MsgBoxError$,48,错误信息ExitSubEndIfEndSub'10转16进制PrivateSubCommand2_Click(IndexAsInteger)OnErrorResumeNextText4.Text=Hex(Text3.Text)IfErrThen''则显示出错信息MsgBoxError$,48,错误信息ExitSubEndIfEndSub'16转10进制PrivateSubCommand3_Click()DimaAsLonga=Val(&H&CStr(Text4.Text))Text3.Text=aEndSub'手动串口发送PrivateSubCommand4_Click()IfMSComm1.PortOpen=FalseThenMsgBox请先打开串口,,错误信息ExitSubEndIfCallsentsubEndSub'清除接收窗PrivateSubCommand5_Click()Text2.Text=EndSubPrivateSubCommand6_Click()UnloadMeEndSubPrivateSubCommand7_Click()OnErrorResumeNextDimSTPAsStringSTP=CStr(Chr(2))&010001&CStr(Chr(3))&25MSComm1.Settings=9600,N,7,2MSComm1.PortOpen=TrueMSComm1.Output=STPMSComm1.PortOpen=FalseIfErrThen'打开串口失败,则显示出错信息MsgBoxError$,48,错误信息ExitSubEndIfEndSubPrivateSubCommand8_Click()OnErrorResumeNextDimFWDAsStringFWD=CStr(Chr(2))&010101&CStr(Chr(3))&26MSComm1.Settings=9600,N,7,2MSComm1.PortOpen=TrueMSComm1.Output=FWDMSComm1.PortOpen=FalseIfErrThen'打开串口失败,则显示出错信息MsgBoxError$,48,错误信息ExitSubEndIfEndSubPrivateSubCommand9_Click()OnErrorResumeNextDimREVAsStringREV=CStr(Chr(2))&010201&CStr(Chr(3))&27MSComm1.Settings=9600,N,7,2MSComm1.PortOpen=TrueMSComm1.Output=REVMSComm1.PortOpen=FalseIfErrThen'打开串口失败,则显示出错信息MsgBoxError$,48,错误信息ExitSubEndIfEndSub'窗口加载PrivateSubForm_Load()Dimd%Ford=1To16Combo1.AddItem(COM&CStr(d))NextCombo1.ListIndex=0Combo2.AddItem6Combo2.AddItem7Combo2.AddItem8Combo2.ListIndex=2Combo3.AddItem110Combo3.AddItem330Combo3.AddItem1200Combo3.AddItem2400Combo3.AddItem4800Combo3.AddItem9600Combo3.AddItem19200Combo3.AddItem38400Combo3.AddItem56000Combo3.AddItem57600Combo3.AddItem115200Combo3.ListIndex=5Combo4.AddItemnCombo4.AddItemoCombo4.AddItemeCombo4.ListIndex=0Combo5.AddItem1Combo5.AddItem2Combo5.ListIndex=0Ford=0To254Combo6.AddItemdNextCombo6.ListIndex=1Text1.Text=010601001770Text2.Text=Text3.Text=Text4.Text=Text5.Text=1000Text6.Text=06Text7.Text=0Text8.Text=1Option1.value=TrueOption3.value=TrueOption7.value=TrueOption9.value=TrueIfMSComm1.PortOpen=FalseThenCommand1.Caption=打开串口ElseCommand1.Caption=关闭串口EndIfEndSub'串口接收程序PrivateSubMSComm1_OnComm()DimHexchrAsString,hexstringAsString,iAsInteger,jAsInteger,hexdispAsStringIfOption8.valueThenhexstring=MSComm1.Input'十六进制显示i=Len(hexstring)Forj=1ToiHexchr=Mid(hexstring,j,1)IfHex(Asc(Hexchr))16ThenText2.Text=Text2.Text&0&Hex(Asc(Hexchr))&ElseText2.Text=Text2.Text&Hex(Asc(Hexchr))&EndIfNextjText2.Text=Text2.Text&CStr(Chr(13))&CStr(Chr(10))ElseText2.Text=Text2.Text&MSComm1.Input&CStr(Chr(13))&CStr(Chr(10))'ASCII码显示EndIfEndSub'手动发送选择PrivateSubOption1_Click()IfOption1.value=TrueThenTimer1.Enabled=FalseCommand4.Enabled=TrueElseTimer1.Enabled=TrueCommand4.Enabled=FalseEndIfEndSub'DeltaASCII发送协议PrivateSubOption10_Click()Combo6.Enabled=TrueText6.Enabled=TrueText7.Enabled=TrueText8.Enabled=TrueLabel10.Enabled=TrueLabel11.Enabled=TrueLabel12.Enabled=TrueLabel13.Enabled=TrueOption6.Enabled=FalseOption7.Enabled=FalseOption11.value=TrueCombo2.ListIndex=1Combo5.ListIndex=1Text1.Enabled=FalseLabel14.Enabled=FalseFrame7.Visible=TrueEndSub'自动发送选择PrivateSubOption2_Click()IfOption2.value=TrueThenTimer1.Enabled=TrueCommand4.Enabled=FalseElseTimer1.Enabled=FalseCommand4.Enabled=TrueEndIfEndSubPrivateSubOption3_Click()'Non选项Combo6.Enabled=FalseText6.Enabled=FalseText7.Enabled=FalseText8.Enabled=FalseLabel10.Enabled=FalseLabel11.Enabled=FalseLabel12.Enabled=FalseLabel13.Enabled=FalseOption6.Enabled=TrueOption7.Enabled=TrueCombo2.ListIndex=2Combo5.ListIndex=0Text1.Enabled=TrueLabel14.Enabled=TrueFrame7.Visible=FalseEndSubPrivateSubOption4_Click()'ASCII选项Combo6.Enabled=TrueText6.Enabled=TrueText7.Enabled=TrueText8.Enabled=TrueLabel10.Enabled=TrueLabel11.Enabled=TrueLabel12.Enabled=TrueLabel13.Enabled=TrueOption6.Enabled=FalseOption7.Enabled=FalseCombo2.ListIndex=1Combo5.ListIndex=1Text1.Enabled=FalseLabel14.Enabled=FalseFrame7.Visible=FalseEndSubPrivateSubOption5_Click()'RTU选项Combo6.Enabled=TrueText6.Enabled=TrueText7.Enabled=TrueText8.Enabled=TrueLabel10.Enabled=TrueLabel11.Enabled=TrueLabel12.Enabled=TrueLabel13.Enabled=TrueOption6.Enabled=FalseOption7.Enabled=FalseCombo2.ListIndex=2Combo5.ListIndex=1Text1.Enabled=FalseLabel14.Enabled=FalseFrame7.Visib