-50-
6
SAMPLE PROGRAM (For MODBUS RTU Protocol)
Programming tool: Visual Basic 5.0 or latter.
Create the form and program shown below and then run it. Then the data of the SE3000 series field
scanners are displayed in the TEXT box.
Dim commCondition As Boolean
‘Communications condition flag
Dim TOut As Boolean
‘Communications timeout flag
Private Sub Form_Load()
’Initialization of controls
Command1.Caption = “Transmission”
Label1.Caption = “Send data”
Label3.Caption = “Receive CH1 CH2 CH3
CH4 CH5 CH6”
Text1.Text = “” ‘For send data display
Text2.Text = “” ‘For receive data display
‘Programming of communications specifications
MSComm1.CommPort = 1 ‘Comm. Port=1
MSComm1.InputMode = comInputModeBinary
‘Comm. Mode=Binary(Modbus RTU)
MSComm1.Settings = "9600,n,8,1"
‘Comm. characters 9600bps,8N1
End Sub
’Click on transmission button
Private Sub Command1_Click()
If (Not commCondition) Then
commCondition = True
Text1.Text = ""
Modbus_com
End If
End Sub
‘Timer for communications timeout detect
Private Sub Timer1_Timer()
TOut = True
End Sub
’Communications for MODBUS
Private Sub Modbus_com()
Dim a(7) As Byte ‘Send data
Dim b() As Byte ‘Receive data
Dim data(30) As Double ‘Data of SE3000
Dim dataTex As String
Dim dataLine As String
Dim Rnum As Integer ‘Number of receive data
dataLine = ""
‘Data request command (CH
1~
CH6)
a(0) = 1 ‘Instruments address of SE3000
a(1) = 4 ‘Function code
a(2) = 0 ‘Reference No.(H)
a(3) = 100 ‘Reference No.(L)
a(4) = 0 ‘Number of Reference No.(H)
a(5) = 12 ‘Number of Reference No.(L)
Rnum = 29 ‘Corect receive number from SE3000=29
'Calculation of CRC----------start here---------------------
Static D(200) As Integer
For i = 0 To 5
D(i + 1) = a(i)
Next i
D(0) = &HFFFF
For i = 1 To 6
D(0) = D(0) Xor D(i)
For j = 1 To 8
cy = D(0) And &H1
If D(0) < 0 Then
p = &H4000
D(0) = D(0) And &H7FFF
Else
p = 0
End If
D(0) = D(0) ¥ 2
D(0) = D(0) Or p
If cy = 1 Then D(0) = D(0) Xor &HA001
Next j
Next i
If D(0) < 0 Then
p = &H80
D(0) = D(0) And &H7FFF
Else
p = 0
End If
c1 = D(0) And &HFF
c2 = (D(0) And &H7F00) ¥ 256
c2 = c2 Or p
'Calculation of CRC-------------End here------------------
a(6) = c1 'CRC-H
a(7) = c2 'CRC-L
'Display of transmitting data
Text1.Text = ""
For i = 0 To 7
Text1.Text = Text1.Text & Hex$(a(i)) & ","
Next
If (Not MSComm1.PortOpen) Then MSComm1.PortOpen = True
MSComm1.Output = a 'Transmission
Do
DoEvents
Loop Until MSComm1.OutBufferCount = 0
'Loop until end of transmission
TOut = False
'Start timer of commicationq timeout detect
Timer1.Interval = 1000 'Timeout=1sec
Timer1.Enabled = True
'Wait for receiving the correct number characters or timeout
Do Until (MSComm1.InBufferCount = Rnum Or TOut = True)
DoEvents
Loop
Timer1.Enabled = False
MSComm1.InputLen = 0
Dim DataH As Long
If Not TOut Then 'Correct receiving
Counter = MSComm1.InBufferCount
b = MSComm1.Input
MSComm1.PortOpen = False
'Conversion of binary data to real data
For i = 0 To (Counter - 5) ¥ 4 - 1
DataH = b(i * 4 + 3)
DataH = DataH * 256 + b(i * 4 + 4)
If DataH > 32768 Then
'Data < 0
data(i) = -1 * (65536 - DataH) / 10 ^ b(i * 4 + 6)
Else
'Data > 0
data(i) = DataH / 10 ^ b(i * 4 + 6)
End If
'Making 9 characters for display
dataTex = CStr(data(i))
dataTex = Right$(" " & dataTex, 9)
dataLine = dataLine & dataTex
Next i
Text2.Text = " " & dataLine
Else
'Time out
Text2.Text = "TIME OUT ERROR"
End If
If commCondition Then
commCondition = False
End If
End Sub
Mscomm
Timer