Click here to Skip to main content
16,017,617 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm send rs232 command Hex to temp&humidity sensor follow up manual. but the result not work

Dim data As Byte() = {&H1, &H4, &H0, &H1, &H0, &H1, &H60, &HA}
SerialPort1.Write(data, 0, 8)

Result In manual = 0x131

my program = 01 04 02 01 2B F8 BF

What I have tried:

Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'ReceivedText(SerialPort1.ReadExisting())
Dim Count As Integer = SerialPort1.BytesToRead
Dim Buffer(0 To Count - 1) As Byte
Dim Text2Display As String = ""

SerialPort1.Read(Buffer, 0, Count)

For I As Integer = 0 To Buffer.GetUpperBound(0)
Text2Display += Buffer(I).ToString("X2") & " "
Next

ReceivedText(Text2Display)

End Sub

Private Sub ReceivedText(ByVal [text] As String)

If Me.rtbReceived.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.rtbReceived.Text &= [text] & vbNewLine
End If

End Sub


End Class
Posted
Updated 26-Jun-19 22:53pm
Comments
OriginalGriff 28-Jun-19 0:36am    
And that is all you need to know.
It tells you exactly what top send, and what it gives as a response.
It says it's using MODBUS protocol, and explains what the data is.
01 04 02 01 2B F8 BF is correct: look at the "Response Temperature Value from Slave" and the data is all there.
Take your message, check the DA is correct, check the CRC-16 is valid, then you can check the bytes count, and get the temp data.
That's pretty simple stuff (except the CRC, which takes a bit of work, but there are loads of examples of that out there:
http://comfiletech.com/blog/modbus-crc-algorithm-for-net/
for example.

What that looks like is one of these:
1) A floating point number probably recieved least significant byte first,
2) The wrong ports set up - I'd start by double checking the manual for the baud rate and stop bits settings.
3) The data is packaged in some way: it looks like the data you show is STX ByteCount1 ByteCount0, then ByteCount data bytes and possibly a checksum?

Seriously, I'd check the manual, and use HyperTerminal to make sure you can communicate with the device correctly before I started writing code. If you don't you have no idea where problems might be.
 
Share this answer
 
In order to get better help you shoul detail your scenario (possibly including more info about the device). Anyway our Griff already gave you the right suggestion: establish the communication with a terminal emulator program (I would use PuTTY - a free SSH and telnet client for Windows[^]) and the modify your program in order to reproduce a successfull communication.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900