Additional Serial Routines
' MoreSerial.Bas
'
' Provides addtional rotuines to output a byte quantity in binary, in
' two digit hexadecimal, in decimal with a fixed number of places and
' format a single with two places afetr the decimal point
'
' Sub PutBBinary(ByVal Q as Byte) ' outputs Q in the form 1001 0111
' Sub PutNibBinary(ByVal Q as Byte) ' outputs a nibble in binary
'
' Sub PutBHex(ByVal Q as Byte) ' outputs Q in hex format; e.g., 3E
' Sub PutNibHex(By Val Q) ' outtputs a single nibble in hex
'
' Sub PutBDecimalFixed(ByVal Q as Byte, ByVal Places as Byte)
' Outputs decimal quantity with specific number of places with no
' leading zero suppression. This is particularly useful in displaying
' such quantities as time; 12:01:03 (vs 12:1:3).
'
' Sub PutSFixed2(ByVal Q as Single)
' Display a Single in the form XXX.YY with two places after the dec point.
'
' Compile with SerialPort.Bas which is provided by NetMedia.
'
' copyright, Peter H. Anderson, Baltimore, MD, Nov, '99
Sub Main()
Dim N as Byte, V as Single
Call OpenSerialPort(1, 19200)
Call PutBBinary(204) ' display 204 in binary
Call PutByte(Asc(" "))
Call PutBHex(204) ' in hex
Call PutByte(Asc(" "))
Call PutBDecimalFixed(3, 2) ' should display as 03
Call NewLine()
For N = 1 To 10 ' generate some Singles
' and display with two digits
' after the decimal point
V = 72.0 + 0.51 * CSng(N)
Call PutSFixed2(V)
Call NewLine()
Next
End Sub
Sub PutBBinary(ByVal Q as Byte)
' Outputs quantity Q in binary format with a space between the high
' and low nibbles.
Call PutNibBinary(Q\16) ' output the high byte
Call PutByte(Asc(" ")) ' space between nibbles
Call PutNibBinary(Q AND bx00001111)
End Sub
Sub PutNibBinary(ByVal Q as Byte)
Dim N as Byte
For N = 1 To 4
If ((Q AND bx00001000) <> 0) Then
Call PutB(1)
Else
Call PutB(0)
End If
Q = Q * 2
Next
End Sub
Sub PutBHex(ByVal Q as Byte)
' diplays Q in two digit Hex format
Call PutNibHex(Q\16)
Call PutNibHex(Q AND bx00001111)
End Sub
Sub PutNibHex(ByVal Q as Byte)
If (Q<10) Then
Call PutB(Q)
Else
Call PutByte(Q - 10 + Asc("A"))
End If
End Sub
Sub PutBDecimalFixed(ByVal Q as Byte, ByVal Places as Byte)
' Outputs Q to number of places specified with no leading zero
' suppression. Useful in displaying quantities such as time
If (Places = 3) Then
Call PutB(Q\100)
End If
Q = Q MOD 100
If (Places = 2) Then
Call PutB(Q\10)
End If
Q = Q MOD 10
Call PutB(Q)
End Sub
Sub PutSFixed2(ByVal Q as Single)
' Outputs a Single in the form of XXX.YY
'
' Note that the whole part of Q is limited to values in the range of
' 0 to +255. Needs work to accommodate negative numbers.
'
' Note that this version does not round. That is, 2.499999 will be output
' as 2.50
Dim Whole as Byte, Fract as Byte
Dim FractSingle as Single
Whole = CByte(Q)
FractSingle = (Q - CSng(Whole)) * 100.0
Fract = CByte(FractSingle)
Call PutB(Whole)
Call PutByte(Asc("."))
Call PutBDecimalFixed(Fract, 2)
End Sub