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.
Bookmarks