dwilches/Ardity

Exception: The semaphore timeout period has expired error

Closed this issue · 6 comments

Hello

I am trying to set up a project using an Arduino to control motion in Unity, and Ardity seems to be the perfect tool for the job. A colleague is working on this same project (but using Mac and a different Arduino) and has got everything working nicely. :)

However, at the moment I am stuck trying to establish a serial connection between an Arduino (Nano 33 BLE Sense) and Ardity/Unity (Windows 10). I have all the COM ports, .net 4.X settings configured correctly AFAIK, and followed your setup guide, but when I set up the connection and run the basic tests (the one that should print "Arduino is alive!"), I receive the "Connection established" message but can get no serial messages from the Arduino.

Attempting to send messages (keypresses for the readwrite example) from Ardity to the Arduino is similarly not working: no messages reach the Arduino, and after the third attempt/keypress I get the error message "Exception: The semaphore timeout period has expired". This time the Arduino detects that the serial connection is active after the first attempt/keypress, but it gets stuck 'on' without producing any further effect.

I have been investigating what might be blocking the messages on the ostensibly active serial connection (including the suggestions here: #13), but have so far drawn a blank. I'd really appreciate any insights you have here!

Best wishes

Martin

Hello Martin!

Thanks for using Ardity. Are you seeing the Exception: The semaphore timeout period has expired message in the Arduino console or in Unity?
If it's in Unity, could you post here the full stack trace? I have not seen that before.

Thanks for your reply. The error appears in the Unity console. Here is the stack trace:

Exception: The semaphore timeout period has expired.
StackTrace: at System.IO.Ports.WinSerialStream.ReportIOError (System.String optional_arg) [0x00039] in :0
at System.IO.Ports.WinSerialStream.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x00099] in :0
at System.IO.Ports.SerialPort.Write (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x0003a] in :0
at System.IO.Ports.SerialPort.Write (System.String text) [0x00021] in :0
at System.IO.Ports.SerialPort.WriteLine (System.String text) [0x0000d] in :0
at (wrapper remoting-invoke-with-check) System.IO.Ports.SerialPort.WriteLine(string)
at SerialThreadLines.SendToWire (System.Object message, System.IO.Ports.SerialPort serialPort) [0x00001] in D:\Users\mtwee\Documents\Unity\Projects\Arduino_Test\Assets\Ardity\Scripts\Threads\SerialThreadLines.cs:34
at AbstractSerialThread.RunOnce () [0x00015] in D:\Users\mtwee\Documents\Unity\Projects\Arduino_Test\Assets\Ardity\Scripts\Threads\AbstractSerialThread.cs:242
at AbstractSerialThread.RunForever () [0x0000f] in D:\Users\mtwee\Documents\Unity\Projects\Arduino_Test\Assets\Ardity\Scripts\Threads\AbstractSerialThread.cs:142
UnityEngine.Debug:LogWarning(Object)
AbstractSerialThread:RunForever() (at Assets/Ardity/Scripts/Threads/AbstractSerialThread.cs:149)
System.Threading.ThreadHelper:ThreadStart()

This is followed by:

Connection attempt failed or disconnection detected
UnityEngine.Debug:Log(Object)
SampleUserPolling_ReadWrite:Update() (at Assets/Ardity/Scripts/Samples/SampleUserPolling_ReadWrite.cs:63)

Then:

Connection established
UnityEngine.Debug:Log(Object)
SampleUserPolling_ReadWrite:Update() (at Assets/Ardity/Scripts/Samples/SampleUserPolling_ReadWrite.cs:61)

Thanks, and sorry it took me a while to get back to you. I have now tried it with PuTTY and at first it appears to be working as it happily receives the "Arduino is alive" message.

I have been using Serial Port Monitor to investigate and noticed that when I use PuTTY, the string from Serial.println() is broken down into individual characters, one per IRP_MJ_READ function. This is followed by one more IRP_MJ_READ that reports a STATUS_TIMEOUT before the loop repeats.

When I use the built-in serial monitor for Arduino instead of PuTTY, the string appears intact in on one IRP_MJ_READ function in Serial Port Monitor. This is followed by two functions: IRP_MJ_DEVICE_CONTROL (IOCTL_SERIAL_WAIT_ON_MASK) and IRP_MJ_DEVICE_CONTROL (IOCTL_SERIAL_GET_COMMSTATUS) before the loop repeats. These functions do not appear when I use PuTTY.

I suspect that this difference is what's causing the problem when communicating with Ardity but don't know how to solve it. Unfortunately, I don't have any other Arduino to test with.

I realise this is now outside your remit as it seems to have nothing to do with Ardity itself so no worries if you can't suggest any more solutions, but I'd greatly appreciate any further insights you have.

Edit: for the sake of completeness: when connected to Unity via Ardity, Serial Port Monitor shows only the IRP_MJ_READ function and reports STATUS_TIMEOUT every time, 10 times a second.

Hello,

I'm not familiar with the low level driver calls in Windows, so I can just guess how to troubleshoot it.

What I recommend is to start tweaking how Ardity uses the C#'s class SerialPort, maybe setting one of the properties there allows you to communicate with your device. I remember someone who once had issues with their program, solved them by setting the flag DtrEnable.

If you want to go this route, check how the SerialPort class is used in AbstractSerialThread, there is where you can start tweaking it.

If you find a solution, let me know so I can add it to the docs!

Hope this helps.

Thanks for the tips – I'll post updates if and when I make progress