[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.vb.general.discussion

Printing Raw bytes from a com port

IJALAB

10/5/2010 5:03:00 PM

I am new to VB coding, I have been asked to print the raw bytes that
comes to a serial port on to the form. i added the following four
lines and used existing RxDisplay function. The output on the form is
hex bytes. they are asking me to print ASCII/raw by tes on to the
form. I dont know how to achieve the same. please help with this
rxdisplay function itself thanks a lot.


Do While TempLong <> -1
TempLong = MyPort.ReadByte
TempStr2 = TempStr2 & CStr(TempLong)
Loop

TempStr2 = vbCrLf & vbCrLf & "Received:" & TempStr2
RxDisplay False, TempStr2

Public Sub RxDisplay(Filter As Boolean, StringToDisplay As String)

Dim StringLength As Integer
Dim CRPosition As Integer
Dim RecordPosition As Integer
Dim StrPosition As Integer
Dim CommaPassPosition As Integer
Dim LogString As String
Dim NumScroll As Long
Dim TimeWithMsec As String
Dim PerlLine As Boolean


'Perl script output lines always start with: vbcrlf & vbcrlf & "$"
PerlLine = False
If Mid(StringToDisplay, 5, 1) = "$" Then
PerlLine = True
End If

TimeWithMsec = TimeToMillisecondString

'Put time tag on first display line for received data
If Left(StringToDisplay, 2) = vbCrLf Then
If Mid(StringToDisplay, 3, 2) = vbCrLf Then
StringLength = Len(StringToDisplay)
StringToDisplay = vbCrLf & vbCrLf & "(" & TimeWithMsec & ") " &
Right(StringToDisplay, StringLength - 4)
End If
End If

'Is message logging turned on?
If LogStatus = True Then
'Write this string to the log file, but delete leading CR/LF
If Left(StringToDisplay, 2) = vbCrLf Then
StringLength = Len(StringToDisplay)
Print #LogFileNumber, Right(StringToDisplay, StringLength - 2)
Else
Print #LogFileNumber, StringToDisplay
End If
End If

'Is auto logging turned on?
If FileOpened(AutoLogFileNumber) = True Then
'Write this string to the log file, but delete leading CR/LF
If Left(StringToDisplay, 2) = vbCrLf Then
StringLength = Len(StringToDisplay)
Print #AutoLogFileNumber, Right(StringToDisplay, StringLength -
2)
Else
Print #AutoLogFileNumber, StringToDisplay
End If
End If

'Perl sciprt output goes to log (above) but not to screen
If PerlLine Then
Exit Sub
End If

'Send data to composite display
CompositeDisplay (StringToDisplay)

'Should the display be filtered?
If Filter = True Then
'Don't display the data when the calling
'routine says it should be filtered
Exit Sub
End If

'Is this a "record pass" message, seen only during download?
RecordPosition = InStr(StringToDisplay, "Record ")
If RecordPosition <> 0 Then
CommaPassPosition = InStr(StringToDisplay, ", pass")
If CommaPassPosition <> 0 Then
'This is a "record pass" message
If Right(TotalRxDisplay, 6) = ", pass" Then
'The previous message on the screen is a "record pass"
message,
'delete it before displaying the new message
StringLength = Len(StringToDisplay)
TotalRxDisplay = Left(TotalRxDisplay, (Len(TotalRxDisplay) -
StringLength))
End If
End If
End If

'Append new string to what is already in the display window
TotalRxDisplay = TotalRxDisplay & StringToDisplay

'If we have displayed more than 20000 characters,
'delete the first half of them - starting with
'the next message (carriage return)
StringLength = Len(TotalRxDisplay)
If StringLength >= 20000 Then
CRPosition = InStr(10000, TotalRxDisplay, Chr(13))
TotalRxDisplay = Right(TotalRxDisplay, (StringLength - CRPosition
- 1))
End If

'Display the new info
Form1.RxWindow.Text = TotalRxDisplay

'Scroll 1
'Set cursor to end of data so that the end
'is displayed instead of the start
'StringLength = Len(TotalRxDisplay)
'Form1.RxWindow.SelStart = StringLength

'Scroll 2
'Scroll down the display to put the newest line on the screen
NumScroll = SendMessage(Form1.RxWindow.hWnd, EM_LINESCROLL, 0,
10000)

End Sub
2 Answers

Dee Earley

10/6/2010 8:01:00 AM

0

On 05/10/2010 18:02, IJALAB wrote:
> I am new to VB coding, I have been asked to print the raw bytes that
> comes to a serial port on to the form. i added the following four
> lines and used existing RxDisplay function. The output on the form is
> hex bytes. they are asking me to print ASCII/raw by tes on to the
> form. I dont know how to achieve the same. please help with this
> rxdisplay function itself thanks a lot.

Erm, That code is fairly hard to follow, but the key part seems to be
the first few lines:

> Do While TempLong<> -1
> TempLong = MyPort.ReadByte
> TempStr2 = TempStr2& CStr(TempLong)
> Loop

Reads in a single byte then appends the numerical value to the string.
This means you will get a long string of numbers such as:
"7210110810811136" for "Hello$"
Note that this is NOT hex and you can't tell when each char begins and ends.

If TempLong is the ascii character (Why not a byte?), then you can use
Hex() to show the hex value ("48656C6C6F24", 2 bytes each), chr() to
show the textual value ("Hello$") or CStr() & " " to show a space
separated decimal ascii values.

> Public Sub RxDisplay(Filter As Boolean, StringToDisplay As String)
Pretty much displays the value as is but adds data/time stamps, etc.

--
Dee Earley (dee.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)

Dee Earley

10/6/2010 8:07:00 AM

0

On 06/10/2010 09:01, Dee Earley wrote:
> On 05/10/2010 18:02, IJALAB wrote:
>> I am new to VB coding, I have been asked to print the raw bytes that
>> comes to a serial port on to the form. i added the following four
>> lines and used existing RxDisplay function. The output on the form is
>> hex bytes. they are asking me to print ASCII/raw by tes on to the
>> form. I dont know how to achieve the same. please help with this
>> rxdisplay function itself thanks a lot.
>
> Erm, That code is fairly hard to follow, but the key part seems to be
> the first few lines:
>
>> Do While TempLong<> -1
>> TempLong = MyPort.ReadByte
>> TempStr2 = TempStr2& CStr(TempLong)
>> Loop
>
> Reads in a single byte then appends the numerical value to the string.
> This means you will get a long string of numbers such as:
> "7210110810811136" for "Hello$"
> Note that this is NOT hex and you can't tell when each char begins and
> ends.
>
> If TempLong is the ascii character (Why not a byte?), then you can use
> Hex() to show the hex value ("48656C6C6F24", 2 bytes each), chr() to
> show the textual value ("Hello$") or CStr() & " " to show a space
> separated decimal ascii values.

Note that unless the bytes really are just ASCII data, you can't safely
hold arbitrary binary data in a string.

--
Dee Earley (dee.earley@icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)