Solved

Serial comms buffer problem

Posted on 1998-12-03
3
167 Views
Last Modified: 2010-05-03
In VB5 when receiving 256 bytes of binary data through the serial port, on tranferring from com1input to a string, the data is truncated to less than 256 bytes, and the MSB is lost. This is in binary tranfer mode. Why is this, and how can I just receive 256 bytes of raw data.
0
Comment
Question by:JulianThomas
  • 2
3 Comments
 
LVL 9

Expert Comment

by:Dalin
ID: 1448035
JulianThomas,
Could you post the code you have?
0
 

Author Comment

by:JulianThomas
ID: 1448036
****************************************************************  
    Dim BUFFER As Variant
    Dim INSTRING As String

Private Sub Command1_Click()
End
End Sub

Private Sub Command2_Click()
MSComm1.Output = "S" + Chr$(&H0E)
End Sub

Private Sub Command3_Click()
Print Len(INSTRING)
Print INSTRING
BV = Asc(Mid$(INSTRING, 1, 1))
End Sub

Private Sub Form_Load()

    MSComm1.InBufferSize = 1024
    MSComm1.InputMode = comInputModeText
    MSComm1.CommPort = 2
    MSComm1.Settings = "19200,N,8,1"
    MSComm1.InputLen = 0
    MSComm1.PortOpen = True
 
End Sub

Private Sub MSComm1_OnComm()
    INSTRING = INSTRING + MSComm1.Input
End Sub
****************************************************************

      When I send "S" + chr$(&H0E) the target unit sends back 257
bytes. I then wait until mscomm1 input buffer has 257 bytes in it.
If I use cominputmodetext then when i use INSTRING=mscomm1.input
the length of INSTRING varies even though the mscomm1 input buffer
had 257 bytes in it. If i use cominputmodebinary then i get half
the number of bytes in INSTRING as there were in the input buffer
and those bytes all have the top bit set to zero.

0
 
LVL 9

Accepted Solution

by:
Dalin earned 100 total points
ID: 1448037
Julian,
1. Make sure the device you receive data from is send you binary or Text, and then select the mode accordingly.
2. If the device is send you binary data, you need to put them in a byte array, and them convert back to text.  Instring is a string so that would not work.
For example, if you receive 2 bytes: the first bytes has a value of 1  and the 2nd byte has 9, you will get 19 in your current code, where as the real value depending on the way it was encoded, the two byte should total 264, now you may find a different ASC char for that. Image the first byte is 1 and the 2nd is 107, your cur code give you 2107. That is why your length vary.

Try:
Dim myBuffer as Variant
Dim myData() as Byte

' Store binary data in myBuffer, since it is variant.
myBuffer = MSComm1.Input

' Assign to byte array for processing
myData = Buffer

' Now you need to convert myData to ASC.

Let me know if I can be further help.
Regards
Dalin





 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now