
Private Sub Combo1_Click()
Dim rec As String
rec = Combo1.Text
Select Case rec
Case "串口1"
MSComm1.CommPort = 1 '使用串行埠1
flag = True
Case "串口2"
MSComm1.CommPort = 2 '使用串行埠2
flag = True
Case "串口3"
MSComm1.CommPort = 3 '使用串行埠3
flag = True
Case "串口4"
MSComm1.CommPort = 4 '使用串行埠4
flag = True
Case "串口5"
MSComm1.CommPort = 5 '使用串行埠5
flag = True
Case Else
MSComm1.PortOpen = False
End Select
End Sub
Private Sub Command1_Click()
Dim str1 As String
Dim str2 As String
str1 = Text6.Text
For i = 1 To Len(str1)
If Mid(str1, i, 1) <> " " Then
str2 = str2 & Mid(str1, i, 1)
End If
Next i
If MSComm1.PortOpen = False Then
MsgBox "串口未打開!"
End If
jiaoya = Val("&H" & Text2.Text) + Val("&H" + Text3.Text) + Val("&H" + Text4.Text) + Val("&H" + Text5.Text)
For i = 1 To Len(str2) / 2
jiaoya = Val("&H" + Mid(str2, i * 2 - 1, 2)) + jiaoya
Next i
jiaoya = jiaoya Mod 256
jiaoya = 256 - jiaoya
jiaoyan = CStr(Hex(jiaoya))
If Len(jiaoyan) > 2 Then jiaoyan = Right(jiaoyan, 2)
If Len(jiaoyan) < 2 Then jiaoyan = "0" & jiaoyan
MSComm1.Output = "3A" + Text2.Text + Text3.Text + Text6.Text + Text5.Text + Text5.Text + " ASC(jiaoyan )" + "CR"
End Sub
Private Sub Command2_Click()
Dim str1 As String
recedata1.Text = MSComm1.Input
str1 = Left(recedata1, Len(recedata1) - 2) '對接收到的資料時行較驗
recedata1 = str1
str1 = Right(recedata1, Len(recedata1) - 1)
For n = 1 To Len(str1) / 2 - 1
k = k + Val("&H" & Mid(str1, 2 * n - 1, 2))
Next n
k = k Mod 256
If k + Val("&H" & Right(str1, 2)) = 256 Then
Text1.Text = Text1.Text & "通信成功" & Chr$(13) & Chr$(10)
recedata1 = str1
Else
Text1.Text = Text1.Text & "較驗碼錯誤" & Chr$(13) & Chr$(10)
recedata1 = ""
End If
End Sub
Private Sub Command3_Click()
If Command3.Caption = "打開串口" Then
Command3.Caption = "關閉串口"
MSComm1.Settings = "9600,N,8,1" '設定波特率等
MSComm1.InputLen = 0 '清空從接收快取器中讀到的字符
MSComm1.PortOpen = True '打開埠
Else
If Command3.Caption = "關閉串口" Then Command3.Caption = "打開串口"
MSComm1.PortOpen = False '關閉埠
End If
End Sub
Private Sub Command4_Click()
If MsgBox("確認想現在退出本程式?", vbQuestion + vbYesNo, "退出 ?") = vbYes Then
End
Unload Me
End If
End Sub
Private Sub Command5_Click()
Combo1.AddItem "串口1"
Combo1.AddItem "串口2"
Combo1.AddItem "串口3"
Combo1.AddItem "串口4"
Combo1.AddItem "串口5"
End Sub
Private Sub Text2_LostFocus()
If Val("&H" & Text2.Text) > 16 Then
Text2.Text = "01"
MsgBox "地址錯誤"
Exit Sub
End If
If Val(Text2.Text) < 16 Then Text2.Text = "0" & Hex(Val("&H" & Text2.Text))
If Val(Text2.Text) = 0 Then Text2.Text = "00"
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text4.SetFocus
End If
End Sub
Private Sub Text3_lostfocus()
Dim n As Integer
If Val(Text3.Text) = 5 Then
Text3.Text = "05"
Text6.MaxLength = 2
Label6.Visible = False
Text5.Visible = False
Label7.Visible = True
Text6.Visible = True
Label7.Visible = True
Text5.Text = ""
MSComm1.RThreshold = 13
ElseIf Val(Text3.Text) = 6 Then
Text3.Text = "06"
Text6.MaxLength = 11
Label6.Visible = False
Text5.Visible = False
Label7.Visible = True
Text6.Visible = True
Text5.Text = ""
MSComm1.RThreshold = 15
ElseIf Val(Text3.Text) = 1 Or Val(Text3.Text) = 2 Or Val(Text3.Text) = 3 Or Val(Text3.Text) = 4 Then
n = Val(Text3.Text)
Text3.Text = "0" & CStr(n)
Label6.Visible = True
Text5.Visible = True
Text6.Visible = False
Label7.Visible = False
Text6.Text = ""
ElseIf Val("&H" & Text3.Text) = 15 Or Val("&H" & Text3.Text) = 16 Then
If Val("&H" & Text3.Text) = 15 Then Text3.Text = "0F"
Label11.Visible = True
Text5.Visible = True
Label6.Visible = True
Text6.Visible = True
Label7.Visible = True
Else
Text3.Text = "01"
MsgBox "請輸入正確的功能代碼", , "提示"
Exit Sub
End If
End Sub
除錯中 從機地址為 05 功能號為 05 起始地址為2E 發送內容為 EF 現在的問題是發送不出去
發送幀的格式為幀頭 地址域 功能代碼 資料域 校驗域 幀尾
1個字符
: 2個字符 2個字符 0~2×256字符 2個字符 2個字符CR,LF
求大神幫忙改改 很著急 跪謝
uj5u.com熱心網友回復:
modbus協議吧,不是發送不出去,而是發送資料的格式或資料錯誤,被下位機拋棄了uj5u.com熱心網友回復:
確實是modbus 那我該怎么改正 你看發送和接收指令有錯誤嗎? 校驗部分也幫我檢查下不 很急uj5u.com熱心網友回復:
1、看懂通信協議!2、參考這個程式:http://download.csdn.net/detail/veron_04/4037248
uj5u.com熱心網友回復:
不是會員啊 。。。。。。。囧uj5u.com熱心網友回復:
ok了 我看看 謝了uj5u.com熱心網友回復:
剛開始,別急于求成,弄的command,一個mscomm,然后設定mscomm相關引數mscomm1.ouput設定一個符合modbus要求的常數串,延時100ms,讀入mscomm的資料,如果能成功讀到資料,那你就成功90%了
uj5u.com熱心網友回復:
就是讀不到資料 著急。。。。。uj5u.com熱心網友回復:
學習了不錯風格大方轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/155985.html
標籤:VB基礎類
