Link to home
Create AccountLog in
Avatar of rohanbagde
rohanbagde

asked on

Send and recieve a Hex String from a serial port?

I need to communicate with a serial port. I want to send Hex values (AAH 01H 00H 07H 03H 05H) to the serial port & receive the decimal values of the sent string ( 170 11 00 77 33 55) in a textbox.
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

Hello,

I don't have much knowledge about serial port communications myself, but you
could have a look at these links:

Article:
http://www.bitwisemag.com/copy/vb/vb1.html

Example COM Detect
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=57069&lngWId=1

Example Send & Receive to com port
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=31818&lngWId=1
You need to use the MSComm control.

Here is an example.
https://www.experts-exchange.com/questions/20859694/Basic-comms-coding.html

The way that the data goes up and down the line is always the same - calling it Hex or Decimal is just a difference in presentation.

&H10 is the same as 16
Avatar of rohanbagde
rohanbagde

ASKER

Have been using MSComm control. Heres a bit of the coding

dim CommHexValue as string
dim SendString as string
dim ReceiveString as string

'accepts hex value entered by user from text1
CommHexValue = Text1.Text

'converts hex value entered by user... but cant convert a string... viz AAH 01H 00H 07H 03H 05H
SendString = CLng(Replace(CommHexValue, "0x", "&H"))

'sends string using MSComm control
MSComm1.Output = SendString

    Do
        DoEvents
    Loop Until MSComm1.InBufferCount >= 2

    ReceivedString = MSComm1.Input
    text2.Text = text2.Text & " " & ReceivedString


//Want to get 170 01 00 07 03 05 as RecievedString from MSComm1.input.  (but getting 17010735)
Dose this help?
Function ShowCharCode(strData As String) As String
    Dim i As Integer
    
    For i = 1 To Len(strData)
        ShowCharCode = ShowCharCode & " " & Asc(Mid$(strData, i, 1))
    Next i
    ShowCharCode = Trim$(ShowCharCode)
End Function

Open in new window

how do i send a string to the serial port? eg: AAH 01H 00H 07H 03H 05H

'is this right?
SendString = Chr$(&HAA) & Chr$(&H00) & Chr$(&H01) & Chr$(&H07) & Chr$(&H03) & Chr$(&H05)
mscomm1.output=SendString

'Now how do i convert the string?
SendString = CLng(Replace(CommHexValue, "0x", "&H")) 'this doesnt work

'Now the same string is sent back by the serial port. I need to view this received string in a textbox. How can I get the output in the textbox as "170 01 00 07 03 05"?
Oops! Dint see the Function written.
how do i call the above function?
getting an "argument not optional" error
Still getting the same output....

(17010735)
It is not possible for me  test, but if you receive a string like this: "17010735", it would appear that the device has already converted the string for hex display. There are no spaces or leading zeros, so it is not possible to tell where the characters begin and end.

Otherwise, I would save the whole string and process it at the end of the message.

Do
        DoEvents
    Loop Until MSComm1.InBufferCount >= 2

    ReceivedString = ReceivedString & MSComm1.Input
'...
text2.Text = Showcharcode( ReceivedString)


However, it should still work if you concatenate the hex display in the textbox.

Do
        DoEvents
    Loop Until MSComm1.InBufferCount >= 2

    ReceivedString = ReceivedString & " " & ShowCharCode(MSComm1.Input)

can i display the MSComm1.input string in hex? Or more precisely; If MSComm1.output = "AA 01 00 07 03 05" , how do I get MSComm1.input = "AA 01 00 07 03 05" displayed in the textbox?
I'm not sure that I understand the question. If you have a string to send than you can display it in hex in a textbox, if you want to:

txtInput.Text = ShowCharCode(strInputString)
I think I chose some misleading names. This is better

txtOutput.Text = ShowCharCode(strOutputString)

What I meant was I want the serial port to respond by sending me the (same string, in this case) string in hex.

If, for example, I had a module connected to the serial port & the module responded if I sent a string to it.
Consider this : my MSComm1.output is "AA 01 00 07 03 05". On receiving, the module would send a acknowlegdement like "AA 0E 0D 00 00 00" or whatever.

Currently I send & receive strings but the received string is in converted format. I want to see the Hex string.
I am still confused about where the difficulty lies.

Unless your device need to receive, or can send the interpreted hex characters, you just send the string. If if contains non-displayable characters, you can append them with Chr$(n) as you know.
So you can send "Hello" & Chr$(9) & "World"

If it is necessary for any reason, you can convert that to display in Hex, i.e.:

"72 101 108 108 111 9 87 111 114 108 100"

If you wanted to, you could send that Hex display format to the device instead of the unconverted string and, if the device understands that notation, it will do whatever it is programmed to do with it.
If it simply echoes back to you what is sent, then it will echo the Hex string back. and you could display that hex or convert it back to the characters that you started with.
I am sending "AA 01 00 07 03 05" using MSComm1.output
Consider my Sync routine. Now the textbox (txtbox) shows "170 1 0 7 3 5". Which is MSComm1.input after conversion. If I dont convert it, I see some garbage values.


I want the textbox to show the hex string I sent. "AA 01 00 07 03 05"
Private Sub cmdSync_Click()
Dim str As String
Dim strData As String
Dim StrSync As String
Dim strOutput As String
Dim i, j As Integer
    
    StrSync = Chr$(&HAA) & Chr$(&HD) & Chr$(&H0) & Chr$(&H0) & Chr$(&H0) & Chr$(&H0)
    Label1.Caption = StrSync
    MSComm1.Output = StrSync
    
    
    Do
        DoEvents
    Loop Until MSComm1.InBufferCount >= 2
       
    str = MSComm1.Input
    For i = 1 To Len(str)
        strData = strData & " " & Asc(Mid$(str, i, 1))
    Next i
    strData = Trim$(strData)
    
    txtBox.Text = strData
    
   
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
thanks!
that did it.