Serial comms buffer problem

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.
Who is Participating?
DalinConnect With a Mentor Commented:
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.

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.

Could you post the code you have?
JulianThomasAuthor Commented:
    Dim BUFFER As Variant
    Dim INSTRING As String

Private Sub Command1_Click()
End Sub

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

Private Sub Command3_Click()
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()
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.