Click to See Complete Forum and Search --> : Read Binary value from MSComm!!!!
Dommy
October 17th, 2001, 06:36 AM
Hi,
I'm trying to send a hex value from a device to the serial port. I need to read the hex value from the port but if it's in text mode then I get a letter, not the actual hex, so how do I go about getting the hex value? ie The hex value is obviously sent digitally so, get the '101010101' or whatever, instead of the letter... I am baffled and I can't use asc() as it's not the ascii value of the letter that I'm after, it's the hex value. (Not the hex of the asc either!) And hex() can't be done on a string... I'm am completely baffled!!! Any help, any code, any ideas!!! Plllllllleaaaaase! Maybe there's another way or an easier thing I'm missing...
cheers
Dom
:D
Xplorer
October 17th, 2001, 07:06 AM
Hi,
I think this is the exact same problem I encounterd a couple of days ago.
Say you send 255 (FF) to the serial port, then MSComm will interpret this as the character "ÿ" (if it is in text mode) because the ASCII code for "ÿ" is 255. To get the integer value of this (255) you can use Asc(), and you can use Hex$() to get the hexadecimal value (FF) in a string.
I don't know if this helped any, but it worked for me...
/Xplorer
Markus W.
October 31st, 2001, 01:39 AM
See MSComm's InputMode and NullDiscard properties.
Iouri
October 31st, 2001, 08:01 AM
Try this example that I found somewhere
The following sample reads from the com port looking for temperature data and displaying it on a label control.
option Explicit
Dim Temperature as Single
private Sub MScomm1_OnComm()
static Buffer as string
Dim i as Integer, ts as string
on error resume next
Select Case MScomm1.CommEvent
' Handle each event or error by placing ' code below each case statement
' Errors
Case comEventBreak ' A Break was received.
Case comEventFrame ' Framing error
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB]
' Events
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of chars
com.InputLen = 0
'retrieve all chars
Buffer = Buffer & MScomm1.input
'search for CH01 data (look for what the particular device sends)
i = InStr(Buffer, "CH01:")
'if string found and the temp data is there then grab it
Do While i > 0 And len(Buffer) > (i + 6)
'grab the numeric part of the data
ts = mid(Buffer, i + 5, 6)
'convert to number
Temperature = Val(Trim(ts))
'display Temp and/or do whatever
lblTemp.Caption = Format(Temperature, "##0.0 C")
'discard used part of string
Buffer = mid(Buffer, i + 11)
'see if more data is available for CH01
i = InStr(Buffer, "CH01:")
Loop
Case comEvSend ' There are SThreshold number of chars in the TX buffer
Case comEvEOF ' An EOF character was found in the input stream
End Select
End Sub
private Sub Form_Load()
on error resume next
MScomm1.CommPort = 2
MScomm1.Settings = "9600,n,8,1"
'no handshaking
MScomm1.Handshaking = comNone
'ample buffer size
MScomm1.InBufferSize = 2048
'fire the RECEIVE event when 1 or more chars waiting
MScomm1.RThreshold = 1
'don't fire the SEND event
MScomm1.SThreshold = 0
'open port
MScomm1.PortOpen = true
If Not MScomm1.PortOpen then
MsgBox "Unable to open COM" & MScomm1.CommPort, vbCritical, "error"
End
End If
End Sub
private Sub Form_Unload(Cancel as Integer)
on error resume next
'don't leave port open, release it
MScomm1.PortOpen = false
End Sub
Iouri Boutchkine
iouri@hotsheet.com
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.