07-24-2011 08:52 PM - edited 07-24-2011 08:58 PM
i want to implement the modbus RTU protocol under VB6. i choose the NI control which has the functions to Async read and write rather than MScomm.
but there is a question i can not figure out.
i use a Byte array to catch the return data. By GetBytesAtPort, i know there is only 7 bytes send by modbus slave.
but it seems take 14 bytes array to hold, so i can't parse them to Hex and do next job. why? and how to make it right?
thanks very much
code samples
Private Sub Command2_Click()
Dim temp2(7) As Byte
temp2(0) = &H1
temp2(1) = &H3
temp2(2) = &H7
temp2(3) = &HD2
temp2(4) = &H0
temp2(5) = &H1
temp2(6) = &H25
temp2(7) = &H47
CWSerial1.WriteAsync temp2
end sub
Private Sub CWSerial1_WriteComplete()
Dim num As Long
Dim da() As Byte
num = CWSerial1.GetBytesAtPort
da = CWSerial1.Read
num = CWSerial1.GetBytesAtPort
End Sub
07-28-2011 01:04 PM
Hey defyG,
From your sample code it looks like you may not be using the "num" that you set in the read.
For example,
Dim byt as Long
Dim data
byt = CWSerial1.GetBytesAtPort()
data = CWSerial1.Read(byt)
the read call has the parameter set to the number of bytes "byt".
This can be viewed at the knowledgebase article located here.
07-28-2011 10:00 PM - edited 07-28-2011 10:09 PM
hey, jake.
thanks for your help.i think i miss some info to make the question more clear ,and seems the question i had asked was solved by StrConv.
And there is another one.
As your recommendation , i have use the CWSerial1.GetBytesAtPort which return the right number of bytes.
And i need the return data in Hex form. so i use StrConv to parse codes.it works fine.
But , when the return data more than n bytes(n>11, which get from GetBytesAtPort ), the byte array i get is always (n-1)bytes.
(not 0 or 1 using to start counting make the different ) it just not right.
I think the way i transforms codes is not so good to achieve the job,but how? thanks again
the codes as below
Private Sub Form_Load()
CWSerial1.BaudRate = 9600
CWSerial1.Parity = cwParityNone
CWSerial1.ComPort = 1
CWSerial1.DataBits = 8
CWSerial1.StopBits = cwStopBitsOne
CWSerial1.ReceiveBufferSize = 1000
CWSerial1.Configure
CWSerial1.DataAsString = False
End Sub
Private Sub Command2_Click()
Dim str1 As String
Dim i, j As Long
Dim da() As Byte
str1 = Text2.Text
str1 = str1 + CRC_16(str1)
ReDim temp(0 To Len(str1) / 2 - 1) As Byte
For i = 0 To (Len(str1) / 2) - 1
temp(i) = Val("&H" & (Mid(str1, 2 * i + 1, 2)))
Next i
CWSerial1.WriteAsync temp
end sub
Private Sub CWSerial1_WriteComplete()
Dim num, datalen, i As Long
Dim da, str1 As String
Dim da2() As Byte
num = CWSerial1.GetBytesAtPort
str1 = ""
da = CWSerial1.Read(num)
da2 = StrConv(da, vbFromUnicode)
datalen = da2(2)
For i = 0 To datalen - 1
str1 = str1 + Format$(Hex(da2(3 + i)), "00")
Next i
MsgBox str1
End Sub
07-29-2011 12:02 AM
the problem solved
just set "CWSerial1.DataAsString = False " before "CWSerial1.Configure"
i can receive the exact right data as a bytes array in any Variant type variable.
No need to parse the code by StrConv. it's really easier to handle than MS ones. thanks