Ok, so I am successfully sending serial data, as shown here.
Now, the problem is that I can't receive data. well, to be exact, now that I have ported the remaining code over from my Windows Library, I can't receive data any more (yes, I was receiving it previously).
My Windows Library worked by polling the serial port when I wanted to, not by using events. With AsyncStreams, this isn't possible - you apparently get an event raised when data is received.
I don't get these events now (where I did previously).
Here is my code to receive the data:
The Stream is initialised here:
'Queue' is a list that I can check (or poll) when I want to. I do it in the ReadFromSerialPort() Subroutine.
Port, Stream and Queue are defined in Class_Globals, and initialised elsewhere.
It's kind of like the app is too wound-up to give time over to receive the data. And yes, I do use Doevents() and a Wait() Sub.
Can anyone see anything that I am doing wrong?
Now, the problem is that I can't receive data. well, to be exact, now that I have ported the remaining code over from my Windows Library, I can't receive data any more (yes, I was receiving it previously).
My Windows Library worked by polling the serial port when I wanted to, not by using events. With AsyncStreams, this isn't possible - you apparently get an event raised when data is received.
I don't get these events now (where I did previously).
Here is my code to receive the data:
B4X:
Private Sub Stream_NewData(Buffer() As Byte)
Private NewData As String = BytesToString(Buffer, 0, Buffer.Length, "ISO8859_1")
Private i As Int = 0
Log("Received string:" & NewData)
For i = 0 To NewData.Length-1
Queue.Add(NewData.SubString2(i,i))
Next
End Sub
The Stream is initialised here:
B4X:
Private Sub InitialiseStream As Boolean
Try
Stream.Initialize(Port.InputStream, Port.OutputStream, "Stream")
Log("Stream initialised")
Return True
Catch
Log("** Error initialising stream")
Return False
End Try
End Sub
'Queue' is a list that I can check (or poll) when I want to. I do it in the ReadFromSerialPort() Subroutine.
B4X:
Private Sub ReadFromSerialPort(IgnoreNULL As Boolean) As Int
Private ReceivedByte As Int = -1
Do While (ReceivedByte = 0 AND IgnoreNULL = True) OR ReceivedByte = -1
If Queue.Size > 0 Then
ReceivedByte = Queue.Get(0)
Queue.RemoveAt(0)
End If
DoEvents
Loop
Return ReceivedByte
End Sub
Port, Stream and Queue are defined in Class_Globals, and initialised elsewhere.
B4X:
Sub Class_Globals
Public Port As Serial
Public BluetoothConnected As Boolean = False
Public PairedMACAddress As String = ""
Public Stream As AsyncStreams
Private Queue As List
Private PartNumber As String = ""
Private Faults As List
It's kind of like the app is too wound-up to give time over to receive the data. And yes, I do use Doevents() and a Wait() Sub.
Can anyone see anything that I am doing wrong?