Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1554
  • Last Modified:

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
0
jscharpf
Asked:
jscharpf
  • 5
  • 5
  • 3
  • +3
1 Solution
 
AzraSoundCommented:
http://www.vbweb.co.uk/tutorials/com_ports.htm
some info you may find useful
0
 
AzraSoundCommented:
"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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Erick37Commented:
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
0
 
Erick37Commented:
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
0
 
DalinCommented:
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
0
 
DalinCommented:
Sorry, I meant to say:
Redim OutData(&HFF)
                    For i = 0 to &HFF
                          ' assign data to the array
                           OutData(i) = i
                         Next i
0
 
Erick37Commented:
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

0
 
Erick37Commented:
Oops, posted to wrong window. Ignore my previous comment.
0
 
JoebobCommented:
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.
0
 
jscharpfAuthor Commented:
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
0
 
jscharpfAuthor Commented:
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

0
 
DalinCommented:
Jeff,
To receive the binary data from the comport, it is simple to get the data:
Dim MyInData() as Byte
Dim lCount as Integer 'If you set the buffer greater then 32K, you need a long

lCount = MSComm1.InbufferCount
Redim MyInData(lCount-1)
MyInData=MSComm1.Input

Now you get the byte data, the trick is to convert back to ASCII. You need to know how the sender is converted to byte before you know how to convert it back.
We know a byte has 8 bite that can hold a number from 0-255.  However, there
more than 255 ASCII chars(Include unreadable ones like CR, LF, STX, etc) in the ASCII table. So normally it requires two bytes for each char(Double Byte Char. Set). Now, with 2 bytes you have in creased the number much greater than you ever need for the char set. So many people only use a couple of bits in the 2nd byte for the char,  while use the rest of the 2nd byte for other information. The problem is that some people use the high bits of the 2nd byte, some use the lower bites.  Also there are people just use one byte (single byte) for the send the Readable chars. You should be able to find this infor from the sender's user's Guide.  If you could not find it, I would try to take two byte at a time, try something like:
Dim lReturn as String
For i = 1 to lCount-1 step 2
       lReturn = lreturn & Chr(myInData(i) * 256 + myInData(i-1))
    Next i

See if the results makes sense.
Let me know how it turned out.

With regarding to the points, since you mentioned to add some points, I would suggest you award those points to Eric37 with a question addressed to him, something like for eric37 only as the question title, and "Thank you for the help in such such question as the content of the question.

Good luck
       
0
 
jscharpfAuthor Commented:
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
0
 
Erick37Commented:
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.
0
 
jscharpfAuthor Commented:
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
0
 
jscharpfAuthor Commented:
thanks again
0
 
jchewCommented:
<ping>
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now