Fetching a Character Array from the Serial Input
' SerIn_2.Bas
'
' Illustrates how to fetch a null terminated array of characters
' from the serial input buffer. If, not an null "string", then
' displays.
'
' Note. One thing this routine taught me was that I don't know anything
' about handling strings using VB. Thus, I resorted to using the "C"
' language approach of an array of chars with a null termination.
'
' In GetCharArray, parameter Times is passed to indicate the number
' of times to look for each character. If no character is received
' the "string" is returned as an empty string.
'
' If the character is either <CR> or <LF>, it is assummed that the
' "string" is completed.
'
' If the number of characters received is such that there is no room
' remaining for the null terminator and an empty "string" is returned.
'
' Copyright, Peter H. Anderson, Baltimore, MD, Oct, '99
Sub Main()
Dim s(1 to 8) as Byte ' note this is an array of bytes
' vs a String
Call OpenSerialPort(2, 9600)
Do
Call GetCharArray(s, 10000)
If (s(1) <> 0) Then ' If not a null "string"
Call PutCharArray(s) ' display it
Call NewLine()
End If
Loop
End Sub
Sub GetCharArray(ByRef s() as Byte, ByVal Times as Integer)
Dim N as Integer
Dim Index as Integer
Dim StringFetched as Byte, Character as Byte, Success as Boolean
s(1) = 0 ' null string
Index = 1
StringFetched = 0 ' false
For N = 1 TO Times
Call GetByte(character, Success)
If (Success) Then
If ((Character = 13) OR (Character = 10)) then
StringFetched = 1 ' "string" successfully fetched
s(Index) = 0
Exit For
Else
s(Index) = Character
Index = Index + 1
If (Index = 8) Then ' no room for null terminator
StringFetched = 0 ' not a valid "string"
Exit For
End If
N = 1 ' start scan times again
End If
End If
Next
If (StringFetched = 0) Then ' if invalid then return null "string"
s(1) = 0
End If
End Sub
Sub PutCharArray(ByRef s() as Byte)
' Ouput each character until null terminator.
Dim N as Integer
For N = 1 To 8
If (s(N) = 0) Then
Exit For
End If
Call PutByte(s(N))
Next
End Sub