Home > Software engineering >  Use the modbus rtu protocol and instrument communication problems
Use the modbus rtu protocol and instrument communication problems

Time:09-30

Written in VB, protocol is modbus rtu
Have a piece of instrument, now is the one of the parameters of read and write little doubt always don't understand
Please help analysis is my code problem or the limitation of instrument

Assuming that this parameter is the SR, from my software to modify its value, and cannot be modified more than 3276.7 the largest number
If shown on the meter exceeds 6553.5, then I on the software of communication over the show has been 3276.8

A, I want to modify the SR parameters for a number greater than 3276.7, 4000.0, such as instrument communication manual instructions decimal point do not transmit
I am going to put the SR modification into real I will send 4000 to 40000, converted to hexadecimal is 9 c40

Send frame command
Address function code first word word the value of the CRC
02 06 00 A1 9 c 40 B0EB

Normally receives commands frame should be such a
02 06 00 A1 9 c 40 B0EB

But why return on my software is ???
My own analysis estimation is CRC calculation error caused by the instrument return error data,
I use the previous numerical some calculations are on other CRC calculation tool B0D8
On my software testing as long as the SR this parameter into greater than 3276.7 data is to be sent more than 32767
Can't modify, less than the number can properly modify

Two, now besides the problems the SR parameters from the meter reading
As long as the instrument of SR this parameter is greater than 6553.5, the software has been on display 3276.8

Send frame command
00 00 02 03 A1 01 D5DB

As long as SR> 6553.5 so the returned data are the following
02 03 02 FF FF FDF4

Limit value and check the read and write as if written just stuck in Ingter int - the range of 32768-32767
And read as if String String a maximum of 65535, and the two related, I am a novice what trouble help see

Three, here attached my code

The SR read:

TxtSegRate. Text=Euro3504 (. TxtDevAdd. Text, 3, 0, "A1", 0, 1, True)/10

SR:

Private Sub cmdSegRate_Click ()

StrData=https://bbs.csdn.net/topics/Replace (Format (DEC_to_HEX txtSegRate. Text * (10), "@ @ @ @"), ""," 0 ")

Call Euro3504 txtDevAdd. Text, (6, 0, "A1", the Left (strData, 2), Right (strData, 2), True)

End Sub


Module code:
Public Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)

The Public Function Euro3504 (A1 As String, A2 As String, A3 As String, A4 As String, A5 As String, A6 As String, Flag As Boolean) As String

Dim CRC_JS () As Byte
Dim CRCC () As Byte

Dim Reply As String
Dim R_S As the Variant
Dim RR () As Byte
Dim As Integer I

'the MESSAGE FRAME FORMAT MESSAGE FRAME FORMAT

ReDim CRCC (5)
CRCC (0)="& amp; H + A1
"CRCC (1)="& amp; H + A2
"CRCC (2)="& amp; H + A3
"CRCC (3)="& amp; H + A4
"CRCC (4)="& amp; H + A5
"CRCC (5)="& amp; H "+ A6

CRC_JS=CRC16 (CRCC) CRC efficacy calculation function called '

If Flag=True Then

Eurotherm. MSComm1. Send frame commands Output=CRCC '
Eurotherm. MSComm1. The Output=CRC_JS
Eurotherm. MSComm1. OutBufferCount=0 'remove transmission buffer

Delay 300 'more than 35 ms can

R_S=Eurotherm. MSComm1. Input
Eurotherm. MSComm1. InBufferCount=0 'remove transmission buffer

RR=R_S

Reply=""

For I=0 To UBound (RR)

Reply=Reply + Right (" 0 "+ Hex (Int (Str (RR (I)))), 2) +"
"
Next

End the If

If the Reply & lt;> "" Then

Euro3504=HEX_to_DEC (Mid (Reply, 7, 4)) 'turn to decimal

'show negative, eliminate Segment A1, what address this parameter as long as show that more than 3278.6 shows negative
'because the decimal point is not transmitted is read out & gt; 32786
If Euro3504 & gt; 32786 And A4 & lt;> "A1" Then
Euro3504=Euro3504-65536
End the If

End the If


End the Function


As Public Function HEX_to_DEC (ByVal Hex String) As Long 'hexadecimal converted to a decimal
Dim As Long I
Dim As Long b

Hex=UCase (Hex)
For I=1 To Len (Hex)
Select Case mids (Hex, Len (Hex) - I + 1, 1)
A Case of "0" : b=b + 16 ^ (I - 1) * 0
A Case of "1" : b=b + 16 ^ (I - 1) * 1
Case "2" : b=b + 16 ^ (I - 1) * 2
Case "3" : b=b + 16 ^ (I - 1) * 3
A Case of "4" : b=b + 16 ^ (I - 1) * 4
Case "5" : b=b + 16 ^ (I - 1) * 5
A Case of "6" : b=b + 16 ^ (I - 1) * 6
Case "7" : b=b + 16 ^ (I - 1) * 7
A Case of "8" : b=b + 16 ^ (I - 1) * 8
"9" Case: b=b + 16 ^ (I - 1) * 9
Case "A" : b=b + 16 ^ (I - 1) * 10
Case "B" : B=B + 16 ^ (I - 1) * 11
Case "C" : b=b + 16 ^ (I - 1) * 12
Case "D" : b=b + 16 ^ (I - 1) * 13
Case "E" : b=b + 16 ^ (I - 1) * 14
Case "F" : b=b + 16 ^ (I - 1) * 15
End the Select
Next I
HEX_to_DEC=b
End the Function


Public Function DEC_to_HEX (Dec As Long) As String 'decimal into hexadecimal
Dim As a String
DEC_to_HEX=""
The Do While Dec & gt; 0
A=CStr (Dec Mod 16)
Select a Case a
Case "10" : a="a"
Case "11" : a="B"
Case "12" : a="C"
Case "13" : a="D"
Case "14" : a="E"
Case "15" : a="F"
End the Select
DEC_to_HEX=a & amp; DEC_to_HEX
Dec=Dec 16
\Loop
End the Function


Public Sub Delay (mmSec As Long) 'Delay ms level process

Dim start As Single
DoEvents
Sleep mmSec
'DoEvents

End Sub


The Public Function CRC16 (data () As Byte) As String 'CRC16 parity Function

Dim CRC16Lo As Byte, CRC16Hi As Byte CRC register
'
Dim CL As Byte, CH As Byte 'polynomial code & amp; HA001

Dim SaveHi As Byte, SaveLo As Byte

Dim As Integer I

Dim Flag As Integer

CRC16Lo=& amp; HFF

CRC16Hi=& amp; HFF

CL=& amp; H1

CH=& amp; HA0

For I=0 To UBound (data)

CRC16Lo=CRC16Lo Xor data (I) 'every data with CRC register for exclusive or

For Flag=0 To 7

SaveHi=CRC16Hi
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related