-
Sunday serial
Ok D, here's what I came up with.
"Ready" may be redundant.
I eliminated the array copy.
I show a test on lowbit0 and highbit0 only -- works fien on all other bits.
Also, I'm not really implementing the second qualifier but I could.
It does react instantaneously with my data stream -- as soon as I switch on or off
I know it's not perfect but I'm (a little) proud of it for a beginner?
Tell me what you think.
Next step, I need to show 16 labels that react to my high bits.
(visible = true, visible = false and learn more about the cross thread thing).
Code:
Public Class Form1
Dim encoding As System.Text.Encoding
Dim WithEvents serport As New IO.Ports.SerialPort
Dim rcvdata(19) As Byte
Dim testbyte As Byte = 170
Dim testbyte2 As Byte = 255
Dim lowval As Byte
Dim highval As Byte
Dim lowpos As Integer
Dim highpos As Integer
Dim lowbit0 As Boolean
Dim highbit0 As Boolean
Dim ready As Boolean
Dim q1pos As Integer
Dim q2pos As Integer
Public Sub datareceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serport.DataReceived
serport.Read(rcvdata, 0, 20)
ready = True
If ready = True Then
findit()
End If
End Sub
Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With serport
.PortName = "com1"
.BaudRate = 1200
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = Handshake.None
.ReceivedBytesThreshold = 15
.ReadTimeout = 3000
.Encoding = encoding.GetEncoding(1252) 'for char > 127
End With
serport.Open()
End Sub
Public Sub findit()
serport.Close()
ready = False
q1pos = Array.IndexOf(rcvdata, testbyte) 'position of qualifier 170
q2pos = Array.IndexOf(rcvdata, testbyte2) 'position of qualifier 255
lowpos = (q1pos + 2) 'position of lower byte
highpos = (q1pos + 3) 'position of higher byte
lowval = rcvdata(lowpos) 'value of lower byte
highval = rcvdata(highpos) 'value of higher byte
If (lowval And Convert.ToInt32(2 ^ 0)) = Convert.ToInt32(2 ^ 0) Then
lowbit0 = True 'etc do for all 8 bits
Else
lowbit0 = False
End If
If (highval And Convert.ToInt32(2 ^ 0)) = Convert.ToInt32(2 ^ 0) Then
highbit0 = True 'etc do for all 8 bits
Else
highbit0 = False
End If
If lowbit0 = False Then
Console.Beep(500, 100)
End If
If lowbit0 = True Then
Console.Beep(1000, 100)
End If
Array.Clear(rcvdata, 0, 20)
serport.Open()
ready = True
End Sub