Solved

MSCOmm output

Posted on 2000-04-12
18
1,508 Views
Last Modified: 2007-10-18
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
Comment
Question by:jscharpf
  • 5
  • 5
  • 3
  • +3
18 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2708758
http://www.vbweb.co.uk/tutorials/com_ports.htm
some info you may find useful
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2708773
"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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2708801
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 32

Expert Comment

by:Erick37
ID: 2708997
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2709024
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
 
LVL 9

Expert Comment

by:Dalin
ID: 2709049
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
 
LVL 9

Expert Comment

by:Dalin
ID: 2709052
Sorry, I meant to say:
Redim OutData(&HFF)
                    For i = 0 to &HFF
                          ' assign data to the array
                           OutData(i) = i
                         Next i
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2709153
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2709162
Oops, posted to wrong window. Ignore my previous comment.
0
 
LVL 3

Expert Comment

by:Joebob
ID: 2709404
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
 

Author Comment

by:jscharpf
ID: 2709508
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
 

Author Comment

by:jscharpf
ID: 2709819
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
 
LVL 9

Accepted Solution

by:
Dalin earned 300 total points
ID: 2711771
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
 

Author Comment

by:jscharpf
ID: 2711878
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
 
LVL 32

Expert Comment

by:Erick37
ID: 2711993
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
 

Author Comment

by:jscharpf
ID: 2712262
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
 

Author Comment

by:jscharpf
ID: 2712271
thanks again
0
 

Expert Comment

by:jchew
ID: 7532568
<ping>
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question