CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2001
    Posts
    20

    Read Binary value from MSComm!!!!

    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


  2. #2
    Join Date
    Sep 2000
    Location
    Sweden
    Posts
    14

    Re: Read Binary value from MSComm!!!!

    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


  3. #3
    Join Date
    Jun 1999
    Location
    Switzerland
    Posts
    398

    Re: Read Binary value from MSComm!!!!

    See MSComm's InputMode and NullDiscard properties.

    Leica Geosystems - when it has to be right

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Read Binary value from MSComm!!!!

    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
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured