Note that prefix mode can only work if both sides of the connection follow the protocol.
Change AStream.InitializePrefix to AStream.Initialize to disable prefix mode.
You've probably already moved on by now, but astream.Initialize has three parameters, not four:
Sub Process_Globals
Private sp As Serial
Private astream As AsyncStreams
End Sub
Sub AppStart (Form1 As Form, Args() As String)
sp.Initialize("sp")
Log(sp.ListPorts)
sp.Open("COM11")
sp.SetParams(115200, 8, 1, 0)
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
End Sub
Sub AStream_NewData (Buffer() As Byte)
Log(BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub
Waiting for debugger to connect...
Program started.
(ArrayList) [COM11]
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1100
load:0x40078000,len:9232
load:0x40080400,len:6400
entry 0x400806a8
Starting BLE work!
Characteristic defined! Now you can read it in your phone!
With serial-over-USB, watch out for data split over several AStream_NewData events.
Eg, if the sending program sends a 100-byte packet, then a few ms pause, then another 100-byte packet, then the receiving program might have 3 NewData events: eg the first NewData has 80 bytes of the first packet; the second NewData has 20 bytes of the first packet and 60 bytes of the second packet; and the third NewData has 40 bytes of the second packet.
Admittedly the problem might not occur very often if there are significant gaps between the packets, but being intermittent just makes the problem more insidious.
www,b4x.com/help/jserial.html
ReadingIntervalTimer
or maybe PurgePort via a timer
Yes, but no matter how long you make it, there is always a chance that you might start sending just as the timeout expires, and the packet will still be split.Surely, somewhere there is a timeout setting that I can extend?
I think it's more that the serial port driver author realised they couldn't cover every possible use case, and so rather than complicate their code by adding special cases each time somebody rolls their own unique protocol, they've just made it as simple as it can be.Madness not to have.
Thank you for the confirmation that my crystal ball is not totally kaput. It's had a few misses recently.This is exactly what is happening.
That may well do the trick, as long as:I've set sp.ReadingThreadInterval=100 which has totally sorted it!
If you're just sending text lines, then it's not hard to accumulate the incoming characters into a buffer and then dole them out when you have a complete line, eg:I don't have time for handshaking or packet checksum etc..
Dim TextBuffer As String 'global serial port input buffer
Sub AStream_NewData (Buffer() As Byte)
'add to buffer
TextBuffer = TextBuffer & BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Dim EOLChar As String = Char(13) 'or 10 or 0 or whatever
'dole out lines (if any)
Do While True
Dim EOLCharAt As Int = TextBuffer.IndexOf(EOLChar)
If EOLCharAt = -1 Then
Exit
End If
Dim TextLine As String = TextBuffer.SubString2(0, EOLCharAt) 'get next line
TextBuffer = TextBuffer.SubString(EOLCharAt + 1) 'remove from buffer
HandleTextLine(TextLine)
Loop
End Sub
'''Do While True
''' Dim EOLCharAt As Int = TextBuffer.IndexOf(EOLChar)
''' If EOLCharAt = -1 Then
''' Exit
''' End If
Do While TextBuffer.Contains(EOLChar)
Dim EOLCharAt As Int = TextBuffer.IndexOf(EOLChar) 'should always find EOLChar
Thank you for the confirmation that my crystal ball is not totally kaput. It's had a few misses recently.
That may well do the trick, as long as:
1/ you can live with the delay that it probably introduces
2/ you don't send packets closer together than the period (100 ms?)
Depending on how the timeout works, there is still a chance that packets might be split.
eg, the timeout could be:
1/ every 100 ms, check the serial port
2/ 100 ms after receiving the first character into an empty buffer
3/ 100 ms since the last character received
Option 1, which simply checks the serial port every 100 ms, might happen to check it midway through receiving a packet, and give you what it's got at that point, ie, the front part of a packet; 100 ms later, you'll get the back part of the packet.