+ Reply to Thread
Results 1 to 3 of 3

Writing Hex values to seriall port - formatting problem

  1. #1
    Registered User
    Join Date
    04-19-2013
    Location
    NJ
    MS-Off Ver
    Excel 2010
    Posts
    2

    Writing Hex values to seriall port - formatting problem

    I am trying to write hex values to a serial com port using VBA in an excel spread sheet. Here is a segment of the code where I am having my problem:
    ----------------------------
    Private Sub CommandButton2_Click()
    Dim intPortID As Integer ' Ex. 1, 2, 3, 4 for COM1 - COM4
    Dim lngStatus As Long
    Dim strData As String

    intPortID = 1
    strData = chr (&H10)


    lngStatus = CommWrite(intPortID, strData)

    End Sub
    _________________________

    I connected a serial port protocol analyzer to com port to monitor the output. When I execute this code I get what I am looking for. That is two hex characters are output ( 1 0 ). I experimented with other formats and for the sake of discussion this is what was put out on the port for the other formats:

    strData = hex (10) puts out " 4 1"
    strData = char (10) puts out " 0 A "
    strData = (10) puts out " 3 1 30 "

    All of these makes sense and verifies that the format of " chr(&H10) is the one I want to use. This is where my problem starts. What I need to do is have the code read the value of a cell from the spread sheet and use that rather than the hard coded "10". This is the code I put in to read the value in cell M25.

    strData = (Worksheets("Sheet1").Range("M25"))

    I should note that on the spreadsheet itsell, M25 is formatted as text rather than a number. I had to do this because some the values that will have a leading zero. For example 04, 0A, etc.

    When I use this line, the output to serial port is " 31 30" the Asci equivalent of 10. And here is where my problem comes in. When I try to apply the " Chr (&H( )) formatting, the compiler does not like it It tells me it expected an expression and highlights the &. I tried all the permutations I could think of to apply the formatting and have not been successful.

    To summarize, I want to read a two character text value from a cell on a spreadsheet and output them as a hex value on the serial port. I am unable to apply the formatting that I know works.

    Thank you for your help.

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Writing Hex values to seriall port - formatting problem

    Try setting the &H formatting to a string variable first, for example:
    Please Login or Register  to view this content.
    There is probably a way to combine the two lines but I can't think what it is.

  3. #3
    Registered User
    Join Date
    04-19-2013
    Location
    NJ
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Writing Hex values to seriall port - formatting problem

    Thank you yudlugar. That worked for me.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1