Home > Software engineering >  Rookie for debugging PC to send and receive data frames in trouble
Rookie for debugging PC to send and receive data frames in trouble

Time:11-03


 Private Sub Combo1_Click () 
Dim rec As String
Rec=Combo1. Text
The Select Case rec
Case "a serial port 1
"MSComm1.Com mPort=1 'using serial port 1
Flag=True
Case "a serial port 2
"MSComm1.Com mPort=2 'using serial port 2
Flag=True
Case "serial port 3"
MSComm1.Com mPort=3 'use of serial port 3
Flag=True
Case "a serial port 4
"MSComm1.Com mPort=4 'using serial port 4
Flag=True
Case "serial port 5
"MSComm1.Com mPort using serial port 5=5 '
Flag=True
In Case the Else
MSComm1. PortOpen=False
End the 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) & lt;> "" Then
Str2=str2 & amp; Mid (str1, I, 1)
End the If
Next I

If MSComm1. PortOpen=False Then
MsgBox "serial port is not open!"
End the If
Jiaoya=Val (" & amp; H "& amp; Text2. + Val (Text) "& amp; H "+ Text3. + Val (Text)" & amp; H "+ Text4. + Val (Text)" & amp; H "+ Text5. Text)
For I=1 To Len (str2)/2
Jiaoya=Val (" & amp; H "+ Mid (str2, I * 2-1, 2)) + jiaoya
Next I
Jiaoya=jiaoya Mod 256
Jiaoya=256 - jiaoya
Jiaoyan=CStr (Hex (jiaoya))
If Len (jiaoyan) & gt; 2 Then jiaoyan=Right (jiaoyan, 2)
If Len (jiaoyan) & lt; 2 Then jiaoyan="0" & amp; Jiaoyan
MSComm1. The Output="aaa" + 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) 'received data: collation
Recedata1 str1=
Str1=Right (recedata1, Len (recedata1) - 1)
For n=1 To Len (str1)/2-1
K=k + Val (" & amp; H "& amp; Mid (str1, 2 * n - 1, 2))
The Next n
K=k Mod 256
If k + Val (" & amp; H "& amp; Right (str1, 2))=256 Then
Text1. Text=Text1. Text & amp; "Communication" success & amp; CRH $(13) & amp; CRH $(10)
Recedata1 str1=
The Else
Text1. Text=Text1. Text & amp; "The check code error" & amp; CRH $(13) & amp; CRH $(10)
Recedata1=""
End the If
End Sub

Private Sub Command3_Click ()
If Command3. Caption="open the serial port" Then
Command3. Caption="close serial port"
MSComm1. Settings="9600, N, 8, 1" 'set the baud rate, etc.
MSComm1. InputLen=0 'empty from the receive buffer read characters
MSComm1. PortOpen=True 'open port
The Else
If Command3. Caption="close serial port" Then Command3. The Caption="open the serial port
"MSComm1. PortOpen=False 'close port
End the If
End Sub

Private Sub Command4_Click ()
If MsgBox (" confirm want to exit the program now?" , vbQuestion + vbYesNo, "quit?" )=vbYes Then
End
Unload Me
End the If
End Sub

Private Sub Command5_Click ()
Combo1. AddItem "serial port 1
"Combo1. AddItem "serial port 2"
Combo1. AddItem "serial port 3"
Combo1. AddItem "serial port 4
"Combo1. AddItem "serial port 5
"End Sub



Private Sub Text2_LostFocus ()
If Val (" & amp; H "& amp; Text2. Text) & gt; 16 Then
Text2. Text="01"
MsgBox "address error"
The Exit Sub
End the If
If Val (Text2. Text) & lt; 16 Then Text2. Text="0" & amp; Hex (Val (" & amp; H "& amp; 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 the 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="6"
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/Val (Text3. Text)=2 Or Val (Text3. Text)=3 Or Val (Text3. Text)=4 Then
N=Val (Text3. Text)
Text3. Text="0" & amp; CStr (n)
Label6. Visible=True
Text5. Visible=True
Text6. Visible=False
Label7. Visible=False
Text6. Text=""
ElseIf Val (" & amp; H "& amp; Text3. Text)=15 Or Val (" & amp; H "& amp; Text3. Text)=16 Then
If Val (" & amp; H "& amp; Text3. Text)=15 Then Text3. Text="0 f
"Label11. Visible=True
Text5. Visible=True
Label6. Visible=True
Text6. Visible=True
Label7. Visible=True
The Else
Text3. Text="01"
MsgBox "please input the correct function code,", "prompt"
The Exit Sub
End the If
End Sub

Debugging in number from the machine address for 05 function for 05 starting address for send 2 e content for EF the question now is to send out
Send the frame format of frame head address domain function code data domain check domain frame tail
1 characters
: two character character 0 ~ 2 x 256 character two characters of the two characters, CR, LF
Pray god help change very worry appreciate

CodePudding user response:

Modbus protocol, not send not go out, but send the format of the data or data errors, abandoned by the hypogyny machine

CodePudding user response:

Is indeed a modbus so what should I correct you see there is an error in sending and receiving instructions? Check part also help me to check is not very urgentnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related