Link to home
Start Free TrialLog in
Avatar of jscharpf
jscharpf

asked on

MSCOmm output

Is there a way to send binary data out the MSCOMM control or just out a comm port from within vb? I have an application where a serial device needs to receive bytes of data. I'm not sure if I'm doing this right, but for example, the device needs to receive :
  16 02 15 10 16 03
This is what that manufactures book says, but it says each of these are bytes, not individual ascii characters.
If I try to build a string, am I not by default making them ascii?
I will give more info if needed..

Thanks
Jeff Scharpf
Avatar of AzraSound
AzraSound
Flag of United States of America image

http://www.vbweb.co.uk/tutorials/com_ports.htm
some info you may find useful
"The Output property can transmit text data or binary data. To send text data using the Output property, you must specify a Variant that contains a string. To send binary data, you must pass a Variant which contains a byte array to the Output property.
Normally, if you are sending an ANSI string to an application, you can send it as text data. If you have data that contains embedded control characters, Null characters, etc., then you will want to pass it as binary data."

taken from this site:
http://www.yes-tele.com/mscomm.html
Option Explicit

Private Sub Command1_Click()
    'Send binary data
    MSComm1.Output = Chr(16) & Chr(2) & Chr(15) & Chr(10) & Chr(16) & Chr(3)
End Sub

Private Sub Form_Load()
    On Error GoTo ERRHANDLER
    With MSComm1 'Settings will change according to device
    'Setup the Baud, Parity, Data Bits, Stop Bits
    .Settings = "9600,n,8,1"
    'Setup Port number
    .CommPort = 2
    'Setup handshaking
    .Handshaking = comRTS
    'Try to open comm port
    .PortOpen = True
    End With
    Exit Sub
ERRHANDLER:
    MsgBox Err.Description
End Sub

Private Sub Form_Unload(Cancel As Integer)
    With MSComm1
    'Close the Port
    If .PortOpen Then
        .PortOpen = False
    End If
    End With
End Sub
Actually, this will send the 6 bytes, not the Unicode 12 bytes:

Private Sub Command1_Click()
    'Send binary data
    Dim sBuffer As Variant
    sBuffer = Chr(16) & Chr(2) & Chr(15) & Chr(10) & Chr(16) & Chr(3)
    sBuffer = StrConv(sBuffer, vbFromUnicode)
    MSComm1.Output = sBuffer
End Sub
Avatar of Dalin
Dalin

There are a few way of doing it. Iwould suggest using the ByteArray to hold your data and send it . It is more reliable.

Example:
Dim OutData() as Byte
Dim i as Integer

Redim OutData(&HFF)
For i = 0 to &HFF
       OutData(i) = 1
     Next i

MSComm1.Output = OutData()

The otherway it to do it Through DBCS. There is a few artical with step by step instructions.
http://support.microsoft.com/support/kb/articles/Q158/0/08.asp?LNG=ENG&SA=ALLKB&FR=0

Regards
Sorry, I meant to say:
Redim OutData(&HFF)
                    For i = 0 to &HFF
                          ' assign data to the array
                           OutData(i) = i
                         Next i
This seems to work fine:

Option Explicit

Private Function GetForm(ByVal sFormName As String) As Form
    sFormName = Trim(sFormName)
    On Error GoTo CreateFormError
    'Add to collection and return the form
    Set GetForm = Forms.Add(sFormName)
    Exit Function
CreateFormError:
    MsgBox "Error in " & Chr(34) & sFormName & Chr(34) & _
        vbCrLf & Err.Description
End Function

Private Sub Command1_Click()
    Dim frm1 As Form, frm2 As Form
    Set frm1 = GetForm("Form2")
    Set frm2 = GetForm("Form2")
    frm1.Caption = "First"
    frm2.Caption = "Second"
    frm1.Show
    frm2.Show
End Sub

Oops, posted to wrong window. Ignore my previous comment.
Just a quick suggestion.  Check the number base for the values.  By experience, it is very common for hardware makers to use hexidecimal for everything.
Avatar of jscharpf

ASKER

Thanks for all the input.
I will give each a try and see what I can get working.
I will get back asap with the points...

js
Thanks everyone, that got me on the right track..
Sorry Erick, but I couldn't get that to work..so I will have to give the points to Dalin. I wish I could give one person 80% and another 20%, lol.

But.. before I do.. I'll add another 100 points Dalin if you can show me how to READ bytes back in. I'm using the MSCOmm to send a byte array out as per your example, and the instrument I'm talking to sends back a reply which is a bunch of bytes also. When I use .input to get whats in the buffer, I get a string but if I send it to a text box i get ||||||. I'd like to convert these to values somehow (I know they are real values as I am watching the RS-232 line with an analyzer).

Thanks again. I'll wait a day or so then post the points ..

Jeff

ASKER CERTIFIED SOLUTION
Avatar of Dalin
Dalin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dalin,

Thanks
I will try this and let you know how it works.
Then I will award you 300 points and Erick 100 points for the effort.

I'll get back to you later today.

Jeff
The MSComm control also has an InputMode property which sets the input to Text or Binary data.

From Help:

 .InputMode = comInputModeText '0 (Default) Data is retrieved through the Input property as text.

 .InputMode = comInputModeBinary '1 Data is retrieved through the Input property as binary data.

Remarks

The InputMode property determines how data will be retrieved through the Input property. The data will either be retrieved as string or as binary data in a byte array.
Use comInputModeText for data that uses the ANSI character set. Use comInputModeBinary for all other data such as data that has embedded control characters, Nulls, etc.
Thanks this did the trick!
I will give Dalin 300 points and
resubmit a question for Erick to give 100 points. Sound fair?

This has been a huge help for me.

Now I'm faced with the wonderful task of deciphering all this byte data and making use of it, lol!

Jeff
thanks again
<ping>