This project has moved and is read-only. For the latest updates, please go here.

BluethoothCommunication retruns System.ArgumentOutOfRangeException on ConnectAsync call

Jan 2, 2014 at 2:30 PM
I can't use BluetoothCommunication. Every time I call ConnectAsync I get a System.ArgumentOutOfRangeException. If I drilldown the exception I can see the message Non-negative number required.\r\nParameter name: count. It fails on byte[] b = _reader.ReadBytes(size); (in SerialPortDatReceived) because the size returned by the previous call (int size = _reader.ReadInt16();) is -1.

Everything works fine if I use UsbCommunication.

Here is the call stack when it fails.

at System.IO.BinaryReader.ReadBytes(Int32 count)
at Lego.Ev3.Desktop.BluetoothCommunication.SerialPortDataReceived(Object sender, SerialDataReceivedEventArgs e) in C:\Users\Eric\documents\visual studio 2013\Projects\WpfApplication1\legoev3\Lego.Ev3.Desktop\BluetoothCommunication.cs:line 51
at System.IO.Ports.SerialPort.CatchReceivedEvents(Object src, SerialDataReceivedEventArgs e)
at System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
Jan 2, 2014 at 8:55 PM
That's rather odd. Can you look at the _serialPort.BaseStream property and determine if there's any actual data in the stream when the SerialPortDataReceived event is fired?
Jan 5, 2014 at 4:33 PM
If I try to manually read byes from the streeam I'm able to read up to 9 bytes. Here there are: FF-FF-55-04-00-38-00-00-C4. That's why when the _read.ReadInt16() is called I received a -1 value (FFFF is -1). These are the only bytes available in the stream.
Jan 9, 2014 at 4:13 AM
Hmm...I can't seem to reproduce this here. Can you give me any more details? Does this happen the instant you try to connect to the device? Is it in reply to some command you're sending? Even the sequence of bytes you're seeing is odd and doesn't follow the pattern of a LEGO brick reply. First two bytes are size, 3rd and 4th bytes are the sequence number (0x0455 would be something that's been running quite a while...that's 1109 in base 10, which means the brick has received 1109 commands before that failure), and byte 5 is the reply type, which should be 0x02/3/4/5, never 0x00.

Dumb question: are you certain you're connecting to the LEGO EV3 brick and not some other serial device?