I have been seeing some friends asking about the communication problems of the ModBus master and slave stations. I took the time to send you a few articles today.
I declare in advance that I am calling the master slave library. The library itself is not written by me. I am not as capable.
In addition, the host computer is written in VB, VB itself is not very familiar, if there is any error, please forgive me.
In the end, this is just a test procedure. If you want to use the business side, you need to improve it yourself.
Description: The plc slave station, the PC as the master station, communicates with the ModBus protocol. The PC reads the data of the PLCVW area; the PC also reads the status of the internal I or O address of the PLC.
////////////////////////////////////////////
The following is a PLC program implementation, easy to post, has been converted to STL
TITLE=Program Notes
Network 1 // Network title
// Initialize the Modbus slave protocol in the first cycle
LD SM0.1
CALL SBR3, 1, 12, 9600, 0, 0, 128, 32, 1000, &VB0, M10.1, MB11
Network 2
// Execute the Modbus Slave Protocol in each cycle
LD SM0.0
CALL SBR1, M10.2, MB12
Network 3
LD SM0.0
MOVW AIW0, VW100
/I +54, VW100
MOVW AIW2, VW102
/I +54, VW102
MOVW AIW4, VW104
/I +54, VW104
MOVW AIW6, VW106
/I +54, VW106
Network 4
LD SM0.0
= Q0.0
Network 5
LD I0.0
= Q0.1
Network 6
LD I0.1
= Q0.2
///////////////////////////////////////
The following is the VB source code
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim x1
Dim p11, p12, p13, p14, p15, p16, p17, p18
Function CRC16(data() As Byte) As String
Dim CRC16Lo As Byte, CRC16Hi As Byte "CRC Register
Dim CL As Byte, CH As Byte "Polynomial Code & HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim i As Integer
Dim Flag As Integer
CRC16Lo = &HFF
CRC16Hi = &HFF
CL = &H1
CH = &HA0
For i = 0 To UBound(data)
CRC16Lo = CRC16Lo Xor data(i) "Each data is XORed with the CRC register
For Flag = 0 To 7
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2 "High bit right shift one bit
CRC16Lo = CRC16Lo \ 2 "Low right shift one bit
If ((SaveHi And &H1) = &H1) Then "If the last bit of the high byte is 1
CRC16Lo = CRC16Lo Or &H80 "The lower byte is shifted right after the first 1
End If "other automatically fills 0
If ((SaveLo And &H1) = &H1) Then "If the LSB is 1, then XOR with the polynomial code
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next Flag
Next i
Dim ReturnData(1) As Byte
ReturnData(0) = CRC16Hi "CRC high
ReturnData(1) = CRC16Lo "CRC low"
CRC16 = ReturnData
End Function
Private Sub About_Click()
frmAbout.Show
End Sub
Private Sub Form_Load() "Initialization
Timer1.Enabled = False "Timer 1 is invalid
Timer1.Interval = 1000 "Timer 1 time is 1S
Timer2.Enabled = True "Timer 2 is valid
Timer2.Interval = 1000 "Timer 2 time is 1S
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text8.Text = ""
Text10.Text = ""
MSComm1.CommPort = 1 "Set the port number
MSComm1.Settings = "9600,n,8,1" "Set communication baud rate
MSComm1.InBufferSize = 1024 "receive buffer size
MSComm1.OutBufferSize = 1024 "Output buffer size
MSComm1.InputMode = comInputModeBinary "Transfer in binary
MSComm1.RThreshold = 1 "
MSComm1.SThreshold = 0
MSComm1.InputLen = 0 "Read all characters of the receive buffer
MSComm1.OutBufferCount = 0 "Empty send buffer
MSComm1.InBufferCount = 0 "Empty receive buffer
If MSComm1.PortOpen = False Then
Command3.Caption = "Open Serial Port"
Else
Command3.Caption = "Close Serial Port"
End If
End Sub
Private Sub Command1_Click()
Timer1.Enabled = True
End Sub
Private Sub Command2_Click() "Exit program, timer 1 is invalid
Timer1.Enabled = False
Cls
Unload Me
End Sub
Private Sub Command3_Click()
On Error Resume Next
If MSComm1.PortOpen = False Then
MSComm1.PortOpen = True
Else
MSComm1.PortOpen = False
End If
If MSComm1.PortOpen Then "Open the close button to display text
Command3.Caption = "Close Serial Port"
Else
Command3.Caption = "Open Serial Port"
End If
If Err Then "Failed to open the serial port, an error message is displayed.
MsgBox Error$, 48, "Error Message"
Exit Sub
End If
End Sub
Private Sub Timer1_Timer() "Read V memory area data
Dim CRC() As Byte
Dim FGetData As String
Dim aa() As Byte
Dim s As String
Dim str As String
Dim i As Integer
Dim bb, cc As String
ReDim aa(5) As Byte "Define dynamic arrays
Aa(0) = &HC
Aa(1) = &H3
Aa(2) = &H0
Aa(3) = &H32
Aa(4) = &H0
Aa(5) = &H4
CRC = CRC16(aa)
Str = CRC
s = ""
For i = 1 To LenB(str)
s = s + Hex(AscB(MidB(str, i, 1)))
Next i
Bb = Right(s, 2)
Cc = Mid(s, 1, 2)
If Len(s) < 4 Then
Cc = Mid(s, 1, 1)
End If
ReDim Preserve aa(0 To 7) As Byte
Aa(6) = Val("&H" & bb)
Aa(7) = Val("&H" & cc)
MSComm1.OutBufferCount = 0 "Empty output register
MSComm1.Output = aa
FGetData = ReceiveData
Text5.Text = FGetData
P11 = Val("&H" & Mid(FGetData, 7, 4))
P12 = Val("&H" & Mid(FGetData, 11, 4))
P13 = Val("&H" & Mid(FGetData, 15, 4))
P14 = Val("&H" & Mid(FGetData, 19, 4))
End Sub
Private Sub Command4_Click() "I state
Dim CRC() As Byte
Dim FGetData As String
Dim aa() As Byte
Dim s As String
Dim str As String
Dim i As Integer
Dim bb, cc As String
ReDim aa(5) As Byte "Define dynamic arrays
Aa(0) = &HC
Aa(1) = &H2
Aa(2) = &H0
Aa(3) = &H0
Aa(4) = &H0
Aa(5) = &H1
CRC = CRC16(aa)
Str = CRC
s = ""
For i = 1 To LenB(str)
s = s + Hex(AscB(MidB(str, i, 1)))
Next i
Bb = Right(s, 2)
Cc = Mid(s, 1, 2)
If Len(s) < 4 Then
Cc = Mid(s, 1, 1)
End If
ReDim Preserve aa(0 To 7) As Byte
Aa(6) = Val("&H" & bb)
Aa(7) = Val("&H" & cc)
MSComm1.OutBufferCount = 0 "Empty output register
MSComm1.Output = aa
FGetData = IReceiveData
P17 = Val("&H" & Mid(FGetData, 7, 2))
P18 = Val("&H" & Mid(FGetData, 9, 2))
End Sub
Private Sub Command5_Click() "Q Status
Dim CRC() As Byte
Dim FGetData As String
Dim aa() As Byte
Dim s As String
Dim str As String
Dim i As Integer
Dim bb, cc As String
ReDim aa(5) As Byte "Define dynamic arrays
Aa(0) = &HC
Aa(1) = &H1
Aa(2) = &H0
Aa(3) = &H0
Aa(4) = &H0
Aa(5) = &H1
CRC = CRC16(aa)
Str = CRC
s = ""
For i = 1 To LenB(str)
s = s + Hex(AscB(MidB(str, i, 1)))
Next i
Bb = Right(s, 2)
Cc = Mid(s, 1, 2)
If Len(s) < 4 Then
Cc = Mid(s, 1, 1)
End If
ReDim Preserve aa(0 To 7) As Byte
Aa(6) = Val("&H" & bb)
Aa(7) = Val("&H" & cc)
MSComm1.OutBufferCount = 0 "Empty output register
MSComm1.Output = aa
FGetData = QReceiveData
P15 = Val("&H" & Mid(FGetData, 7, 2))
P16 = Val("&H" & Mid(FGetData, 9, 2))
End Sub
Private Function ReceiveData() As String "Returns V memory area data
Dim FGetData As String
Dim t1 As Long
Dim av As Variant
Dim i As Integer
Dim ReDataLen As Integer
FGetData = ""
T1 = GetTickCount() "take time, do delay
Do "loop waiting to receive data
DoEvents
If MSComm1.InBufferCount > 0 Then "The serial port has data.
ReDataLen = MSComm1.InBufferCount "Get data length
Av = MSComm1.Input "Remove serial data
For i = 0 To ReDataLen - 1
FGetData = FGetData & Right("00" & Hex(av(i)), 2)
Next i
End If
If Len(FGetData) >= 6 Then
If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 + 8 Then
ReceiveData = FGetData
Exit Function
End If
End If
If GetTickCount - t1 > 2000 Then "2 seconds after the harvest is over, it will not be received.
ReceiveData = ""
Exit Function
End If
Loop
End Function
Private Function IReceiveData() As String "Reverse I state
Dim FGetData As String
Dim t1 As Long
Dim av As Variant
Dim i As Integer
Dim ReDataLen As Integer
FGetData = ""
T1 = GetTickCount() "take time, do delay
Do "loop waiting to receive data
DoEvents
If MSComm1.InBufferCount > 0 Then "The serial port has data.
ReDataLen = MSComm1.InBufferCount "Get data length
Av = MSComm1.Input "Remove serial data
For i = 0 To ReDataLen - 1
FGetData = FGetData & Right("00" & Hex(av(i)), 2)
Next i
End If
If Len(FGetData) >= 6 Then
If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 + 8 Then
IReceiveData = FGetData
Exit Function
End If
End If
If GetTickCount - t1 > 2000 Then "2 seconds after the harvest is over, it will not be received.
IReceiveData = ""
Exit Function
End If
Loop
End Function
Private Function QReceiveData() As String "Reverse Q state"
Dim FGetData As String
Dim t1 As Long
Dim av As Variant
Dim i As Integer
Dim ReDataLen As Integer
FGetData = ""
T1 = GetTickCount() "take time, do delay
Do "loop waiting to receive data
DoEvents
If MSComm1.InBufferCount > 0 Then "The serial port has data.
ReDataLen = MSComm1.InBufferCount "Get data length
Av = MSComm1.Input "Remove serial data
For i = 0 To ReDataLen - 1
FGetData = FGetData & Right("00" & Hex(av(i)), 2)
Next i
End If
If Len(FGetData) >= 6 Then
If Len(FGetData) > Val("&H" & Mid(FGetData, 5, 2)) * 2 + 8 Then
QReceiveData = FGetData
Exit Function
End If
End If
If GetTickCount - t1 > 2000 Then "2 seconds after the harvest is over, it will not be received.
QReceiveData = ""
Exit Function
End If
Loop
End Function
Private Sub Timer2_Timer()
X1 = x1 + 1
Text10.Text = str(x1)
Text1.Text = str(p11)
Text2.Text = str(p12)
Text3.Text = str(p13)
Text4.Text = str(p14)
Text7.Text = str(p15)
If Text7.Text = 1 Then Text7.BackColor = RGB(255, 0, 255)
If Text7.Text = 0 Then Text7.BackColor = RGB(0, 255, 255)
"Text6.Text = str(p16)
Text9.Text = str(p17)
If Text9.Text = 1 Then Text9.BackColor = RGB(255, 0, 255)
If Text9.Text = 0 Then Text9.BackColor = RGB(0, 255, 255)
"Text8.Text = str(p18)
End Sub
This classification is CAS Applied Chemistry Materials Co.,Ltd power Electrical Tapes series.
According to the material, it is mainly divided into ,Insulating Tape,Heat Shrink Tape,Wire Tape,silicone rubber, Ethylene Propylene Rubber Tape, polyethylene tape, Chlorinated Polyethylene Tape and so on.
Under this classification, tapes have different functions for you to choose, such as waterproof, armored, insulation protection, color display and temperature indication, self-curing at normal temperature tape, flame retardant tape, stress control tape and so on.
CAS Applied Chemistry Materials Co.,Ltd.is famous
as:
1. Provided products for the State Grid in 2008 to ensure the smooth progress
of the Beijing Olympic Games.
2. Provided products and services to China Southern Power Grid in 2010 to
ensure the smooth progress of the Guangzhou Asian Games.
3. Occupied more than 50% of insulation protection field of overhead contact
line for China High Speed Rail Market.
4. has been Excellent supplier and long-term partner of Aviation Industry
Corporation of China.
5. Provided core technology and products for power cable accessories (electrical
stress control).
Electrical Tapes,Insulating Tape,Heat Shrink Tape,Wire Tape
CAS Applied Chemistry Materials Co.,Ltd. , https://www.casac1997.com