Modbus-RTU Frame

J

Thread Starter

Jung-Hyuck Huh

Let me know the Perfect Frame.
The following is that I have got from the Manufacturer.
I couldn't guess to make perfect frame of the following.

Request Frame
- Slave Address : 0x01
- Function : 0x03
- Starting Address HI : 0x00
- Starting Address LO : 0x00
- No. of Points HI : 0x00
- No. of Points LO : 0x04
- CRC16 HI : 0x44
- CRC16 LO : 0x09

I guess Request Frame should be either
0103000000044409 OR
0103000000042C09.
But these don't be worked at all.

Please let me know whether it is right or not.
Thanking you.
 
>I guess Request Frame should be either
>0103000000044409 OR
>0103000000042C09.

01 03 00 00 00 04 44 09
is the frame with the correct CRC.
 
> Request Frame
> - Slave Address : 0x01
> - Function : 0x03
> - Starting Address HI : 0x00
> - Starting Address LO : 0x00
> - No. of Points HI : 0x00
> - No. of Points LO : 0x04
> - CRC16 HI : 0x44
> - CRC16 LO : 0x09
>
> 0103000000044409
This seems to be ok, although i did not check if the checksum is ok?
Did you verify that the settings of the serial port are ok?
Baudrate, Start/Stop Bits, Parity

Here is our Modbus class
http://tinyurl.com/az4llre
You might compare it with your solution.
 
J

Jung-Hyuck Huh

I have set on right ways.
Could you check the following code out??
I can't check why it doesn't work on.
I already have checked the connection and data transfer with proper program. Everything is right !

====================================<pre>
Imports System.Net.Sockets
Imports System.Text

Public Class Form1

'&#52488;&#44592;&#44050; &#49444;&#51221;
Private Sub InitForm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("CRC16")
ComboBox2.Items.Add("HEX")
ComboBox1.SelectedIndex = 0
ComboBox2.SelectedIndex = 0
ComboBox1.Select()


ipAdrTextBox.Text = "192.168.0.100"
portTextBox.Text = "502"

End Sub

'&#54840;&#52636;&#49884; CRC&#47484; &#44228;&#49328;
Private Sub GetCRC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CRCTextBox.Text = CrcMaker(makeCRCTextBox.Text)
End Sub

'&#44592;&#48376; &#49444;&#51221;&#46108; &#44050;&#51012; &#49324;&#50857;&#54644; &#53685;&#49888; - Device Address &#44050; &#51069;&#44592;
Private Sub AddressRead_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Try
'CRCTextBox.Text = "ff030006000171d5"
'.Text = "0103000000044409"
'CRCTextBox.Text = "01 03 00 00 00 04 44 09"

CRCTextBox.Text = "00000000060103000000044409"
'CRCTextBox.Text = "000000000006090300000001"


'ff 03 0006 0001 71d5
'&#51204;&#49569;&#54616;&#45716; &#44050;&#51008; hex(16&#51652;&#49688;)&#47196; &#51060;&#47336;&#50612;&#51256; &#51080;&#45796;

'ff - Device Address / 0xff&#45716; Broad casting&#51012; &#51032;&#48120;
'03 - read&#45716; 0x03, write&#45716; 0x06
'0006 - Resister Map&#51032; Address
'0001 - read&#51032; &#44221;&#50864;&#50640;&#45716; &#51069;&#50612;&#50732; word &#44600;&#51060;
' write&#51032; &#44221;&#50864;&#50640;&#45716; &#50424; &#44050;
'71d5 - CRC &#44160;&#49324;&#47484; &#50948;&#54620; &#44050;

'ip, port, &#51204;&#49569; &#44050;&#51012; &#53581;&#49828;&#53944; &#48149;&#49828;&#50640;&#49436; &#44032;&#51256;&#50740;
Dim server_ip As String = ipAdrTextBox.Text
Dim port_num As Int32 = Convert.ToInt32(portTextBox.Text)
Dim message As String = CRCTextBox.Text

'&#51204;&#49569;&#54624; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText("Send Data" & vbTab & ": ")
Dim a As Byte() = StrToByte(message)
printHex(a)

'&#44208;&#44284; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText(vbCrLf & "Received Data" & vbTab & ": ")
Dim b As Byte() = TcpSocket(server_ip, port_num, message)
printHex(b)


'&#51060; &#44221;&#50864; &#48155;&#45716; &#45936;&#51060;&#53552;
'byte &#48176;&#50676;&#51064; &#44208;&#44284; &#45936;&#51060;&#53552; b&#50640;&#49436; b[4]&#44050;&#51060; Device Address&#51060;&#45796;
'&#50696;> &#44208;&#44284;&#44050;&#51060; [FF 03 02 00 FE 10 10] &#51004;&#47196; &#45208;&#50772;&#51012; &#44221;&#50864;
'5&#48264;&#51704; &#44050;&#51064; FE&#44032; &#50896;&#54616;&#45716; Device Address &#44050;&#51012; &#45208;&#53440;&#45240;&#45796;

Catch ex As Exception
MsgBox("&#51204;&#49569; &#49892;&#54056;")

Finally
logText.AppendText(vbCrLf & vbCrLf)
logText.ScrollToCaret()

End Try
End Sub

'&#49444;&#51221;&#46108; &#44050;&#51004;&#47196; &#53685;&#49888; - Channel 1&#51032; &#52852;&#50868;&#53552;&#44050; &#51069;&#44592;
Private Sub CounterRead_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Try
'CRCTextBox.Text = "FF030066000231CA"
'CRCTextBox.Text = "0103000000044409"
CRCTextBox.Text = "0103000000044409"
'FF 03 0066 0002 31CA

'ip, port, &#51204;&#49569; &#44050;&#51012; &#53581;&#49828;&#53944; &#48149;&#49828;&#50640;&#49436; &#44032;&#51256;&#50740;
Dim server_ip As String = ipAdrTextBox.Text
Dim port_num As Int32 = Convert.ToInt32(portTextBox.Text)
Dim message As String = CRCTextBox.Text

'&#51204;&#49569;&#54624; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText("Send Data" & vbTab & ": ")
Dim a As Byte() = StrToByte(message)
printHex(a)

'&#44208;&#44284; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText(vbCrLf & "Received Data" & vbTab & ": ")
Dim b As Byte() = TcpSocket(server_ip, port_num, message)
printHex(b)

'&#51060; &#44221;&#50864; &#48155;&#45716; &#45936;&#51060;&#53552;
'byte &#48176;&#50676;&#51064; &#44208;&#44284; &#45936;&#51060;&#53552; b&#50640;&#49436; b[3] ~ b[7]
'b[3],b[4] -> (unsigned short) b1&#51004;&#47196; &#48320;&#54872;, b[5], b[6] -> (unsigned short) b2&#47196; &#48320;&#54872;&#54620; &#45796;&#51020;
'b1 * 65535 + b2&#47484; &#50672;&#49328;&#54616;&#47732; &#44208;&#44284;&#44032; &#45208;&#50728;&#45796;
'&#50696;> &#44208;&#44284;&#44032; [FF 03 04 00 00 00 10 E4 30]&#51004;&#47196; &#45208;&#50772;&#51012; &#44221;&#50864;
'b[3], b[4] = 00 00 -> 0x0000 = 0
'b[5], b[6] = 00 10 -> 0x0010 = 16
'0 * 65535 + 16 = 16

'int32&#47196; &#44228;&#49328;&#54624; &#49688;&#46020; &#51080;&#45796;
'&#51060; &#44221;&#50864; byte &#48176;&#50676;&#51012; &#49352;&#47196; &#47564;&#46304;&#45796;
'Dim bCount As Byte = {b[6], b[5], b[4], b[3]}
'Dim nCount As Int32 = BitConverter.ToInt32(bCount, 0)
'nCount&#44032; &#44396;&#54616;&#47140;&#45716; &#44050;&#51060;&#45796;.

Catch ex As Exception
MsgBox("&#51204;&#49569; &#49892;&#54056;")

Finally
logText.AppendText(vbCrLf & vbCrLf)
logText.ScrollToCaret()

End Try
End Sub

'&#49444;&#51221;&#46108; &#44050;&#51004;&#47196; &#53685;&#49888; - 1&#48264; &#52292;&#45328;&#51032; &#49345;&#53468; &#48320;&#44221; (Active)

Private Sub Active_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
Try
'CRCTextBox.Text = "FF06006500FE0D8B"
'CRCTextBox.Text = "0103000000044409"
CRCTextBox.Text = "0103000000042C09"
'FF 06 0065 00FE 0D8B

'ip, port, &#51204;&#49569; &#44050;&#51012; &#53581;&#49828;&#53944; &#48149;&#49828;&#50640;&#49436; &#44032;&#51256;&#50740;
Dim server_ip As String = ipAdrTextBox.Text
Dim port_num As Int32 = Convert.ToInt32(portTextBox.Text)
Dim message As String = CRCTextBox.Text

'&#51204;&#49569;&#54624; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText("Send Data" & vbTab & ": ")
Dim a As Byte() = StrToByte(message)
printHex(a)

'&#44208;&#44284; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText(vbCrLf & "Received Data" & vbTab & ": ")
Dim b As Byte() = TcpSocket(server_ip, port_num, message)
printHex(b)

'Write &#49884;&#50640;&#45716; &#48152;&#54872;&#44050;&#51060; &#51032;&#48120;&#50630;&#45796;
'&#51228;&#45824;&#47196; &#51204;&#49569;&#46104;&#50632;&#45716;&#47564; &#54869;&#51064;&#54616;&#46020;&#47197; &#54620;&#45796;

Catch ex As Exception
MsgBox("&#51204;&#49569; &#49892;&#54056;")

Finally
logText.AppendText(vbCrLf & vbCrLf)
logText.ScrollToCaret()

End Try
End Sub

'&#49444;&#51221;&#46108; &#44050;&#51004;&#47196; &#53685;&#49888; - 1&#48264; &#52292;&#45328;&#51032; &#49345;&#53468; &#48320;&#44221; (Inactive)
Private Sub Inactive_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
Try
'CRCTextBox.Text = "FF06006500FFCC4B"
'CRCTextBox.Text = "0103000000044409"
CRCTextBox.Text = "0103000000042C09"
'FF 06 0065 00FF CC4B

'ip, port, &#51204;&#49569; &#44050;&#51012; &#53581;&#49828;&#53944; &#48149;&#49828;&#50640;&#49436; &#44032;&#51256;&#50740;
Dim server_ip As String = ipAdrTextBox.Text
Dim port_num As Int32 = Convert.ToInt32(portTextBox.Text)
Dim message As String = CRCTextBox.Text

'&#51204;&#49569;&#54624; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText("Send Data" & vbTab & ": ")
Dim a As Byte() = StrToByte(message)
printHex(a)

'&#44208;&#44284; &#45936;&#51060;&#53552;&#47484; &#52636;&#47141;
logText.AppendText(vbCrLf & "Received Data" & vbTab & ": ")
Dim b As Byte() = TcpSocket(server_ip, port_num, message)
printHex(b)

'Write &#49884;&#50640;&#45716; &#48152;&#54872;&#44050;&#51060; &#51032;&#48120;&#50630;&#45796;
'&#51228;&#45824;&#47196; &#51204;&#49569;&#46104;&#50632;&#45716;&#47564; &#54869;&#51064;&#54616;&#46020;&#47197; &#54620;&#45796;

Catch ex As Exception
MsgBox("&#51204;&#49569; &#49892;&#54056;")

Finally
logText.AppendText(vbCrLf & vbCrLf)
logText.ScrollToCaret()

End Try
End Sub




'&#51077;&#47141;&#46108; &#44050;&#51012; &#49324;&#50857;&#54644; &#53685;&#49888;
Private Sub DataSend_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
Try
Dim server_ip As String = ipAdrTextBox.Text
Dim port_num As Int32 = Convert.ToInt32(portTextBox.Text)
Dim message As String = CRCTextBox.Text

If message.Length > 4 Then

logText.AppendText("Send Data" & vbTab & ": ")
Dim a As Byte() = StrToByte(message)
printHex(a)

logText.AppendText(vbCrLf & "Received Data" & vbTab & ": ")
Dim b As Byte() = TcpSocket(server_ip, port_num, message)
printHex(b)

End If

Catch ex As Exception
MsgBox("&#51204;&#49569; &#49892;&#54056;")

Finally
logText.AppendText(vbCrLf & vbCrLf)
logText.ScrollToCaret()

End Try
End Sub


#Region "&#54632;&#49688;&#47785;&#47197;"

'String&#51012; &#48155;&#50500;&#49436; byte &#48176;&#50676;&#47196; &#48320;&#54872;&#54644;&#49436; &#46028;&#47140;&#51456;&#45796;
Public Function StrToByte(ByVal str As String) As Byte()
Dim bytes(str.Length / 2 - 1) As Byte

bytes(0) = Convert.ToByte(Microsoft.VisualBasic.Left(str, 2), 16)

For index = 3 To str.Length Step 2

bytes((index - 1) / 2) = Convert.ToByte(Mid(str, index, 2), 16)

Next

Return bytes

End Function

'&#51648;&#51221;&#46108; ip&#50752; &#54252;&#53944; &#49707;&#51088;&#47484; &#49324;&#50857;&#54644; &#47700;&#49464;&#51648;&#47484; &#51204;&#45804;&#54616;&#44256; &#44536; &#44208;&#44284;&#47484; &#48148;&#51060;&#53944; &#48176;&#50676;&#47196; &#48155;&#45716;&#45796;
Private Function TcpSocket(ByVal ip As String, ByVal port As Int32, ByVal msg As String) As Byte()

Dim tcpClient As New System.Net.Sockets.TcpClient()
tcpClient.SendTimeout = 1
tcpClient.ReceiveTimeout = 10000

tcpClient.SendBufferSize = 512
tcpClient.ReceiveBufferSize = 512



Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Dim sendBytes As Byte() = StrToByte(msg)

Dim receiveBytes As Byte()

If sendBytes(1) = 6 Then
ReDim receiveBytes(sendBytes.Length)
Else
ReDim receiveBytes((sendBytes(4) * 16 + sendBytes(5)) * 2 + 4)
End If


Try
tcpClient.Connect(ip, port)


Dim networkStream As NetworkStream = tcpClient.GetStream()

If networkStream.CanWrite And networkStream.CanRead Then


networkStream.Write(sendBytes, 0, sendBytes.Length)

networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))


For index = 0 To receiveBytes.Length - 1
receiveBytes(index) = bytes(index)
Next

tcpClient.Close()

'Return receiveBytes

Else
If Not networkStream.CanRead Then
MsgBox("Cannot read data to this stream")
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
MsgBox("Cannot write data from this stream")
tcpClient.Close()
End If
End If
End If

tcpClient.Close()

Catch ex As SocketException
MsgBox("&#49548;&#53011; &#51217;&#44540; &#50724;&#47448;")

Catch ex As ObjectDisposedException
MsgBox("&#49548;&#53011; &#45803;&#55192; &#50724;&#47448;")

Catch es As Exception
MsgBox("&#50724;&#47448; &#48156;&#49373;")
End Try

Return receiveBytes

End Function

'&#47928;&#51088;&#50676;&#51032; CRC&#47484; &#49373;&#49457;&#54620;&#45796;
Private Function CrcMaker(ByVal str As String) As String
Dim strtemp As String = str
If (strtemp.Length Mod 2 Or strtemp.Length = 0) Then
MessageBox.Show("&#51096;&#47803;&#46108; &#51077;&#47141;")
Return ""
End If

Dim usCRC As System.UInt32
Dim bytTemp As Byte
usCRC = &HFFFF
Try
For i As Integer = 0 To strtemp.Length - 2 Step 2
bytTemp = Convert.ToUInt32(strtemp.Substring(i, 2), 16)
usCRC = Convert.ToUInt32(usCRC Xor bytTemp)
For j As Integer = 1 To 8
If (usCRC And 1) Then
usCRC = usCRC >> 1
usCRC = Convert.ToUInt32(usCRC Xor Convert.ToUInt32(&HA001))
Else
usCRC = usCRC >> 1
End If
Next
Next


Return strtemp + Hex(usCRC).Substring(2, 2) + Hex(usCRC).Substring(0, 2)
Catch ex As Exception
MsgBox("&#47928;&#51088; &#51077;&#47141; &#50724;&#47448;")
Return ""
End Try


End Function

'&#48148;&#51060;&#53944; &#48176;&#50676;&#51012; &#54757;&#49324;&#44050;&#51004;&#47196; &#48372;&#50668;&#51456;&#45796;
Public Sub printHex(ByVal bytes As Byte())
For index = 0 To bytes.Length - 1
If Hex(bytes(index)).Length = 1 Then
logText.AppendText("0" & Hex(bytes(index)) & " ")
Else
logText.AppendText(Hex(bytes(index)) & " ")
End If
Next
End Sub

#End Region

End Class</pre>
================================================
 
Top