Quantcast
Channel: Xojo Programming Forum - Latest topics
Viewing all articles
Browse latest Browse all 3852

SerialConnection.Flush bug? (continued)

$
0
0

Continuing the discussion from Serial.Flush bug?:

Yep, took me some time to figure this out as well. And then I forgot since I haven’t used flush for anything anymore and then recently stepped into the same trap again :slight_smile:

But yes, it’s absolute true that SerialConnection.Flush will wipe the receive buffers, which is NOT what the documentation would have you believe.

Here is a little test code to demonstrate this. All you need is to create a new Desktop project with a TextArea and a Serial Connection, and plug in a serial port adapter with a loopback jumper.

#if TargetWindows
  SerialConnection1.Device = SerialDevice.WithName("COM3")
#ElseIf TargetMacOS
  SerialConnection1.Device = SerialDevice.WithName("/dev/cu.usbserial-FTDGUV4P")
#ElseIf TargetLinux
  SerialConnection1.Device = SerialDevice.WithName("/dev/ttyUSB0")
#EndIf

// Install a jumper between the TX and RX pins of the serial port connector

var p as SerialConnection = SerialConnection1
var t as DesktopTextArea = TextArea1

// Open the port
p.Connect

// Write Data
var s as String = "Hello World"
t.AddText("Sending: " + s + EndOfLine)
p.Write(s)
p.XmitWait

// wait for data to arrive
while p.BytesAvailable < s.Length
  p.Poll
wend

// Check the receive buffer
t.AddText("BytesAvailable: " + p.BytesAvailable.ToString + ", " _
+ p.LookAhead.DefineEncoding(Encodings.ASCII) + EndOfLine)

// Flush the port
t.AddText("Flushing the port" + EndOfLine)
p.Flush

// Check the receive buffer
t.AddText("BytesAvailable: " + p.BytesAvailable.ToString + ", " _
+ p.LookAhead.DefineEncoding(Encodings.ASCII) + EndOfLine)

p.Close

Should this perhaps be reported as a bug if the intended behavior of SerialConnection.Flush is to only flush out the transmit buffer and leave the receive buffer alone? Or is wiping both buffer actually the intended behavior and the documentation needs to be updated?

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 3852

Trending Articles