- Home /
Unity Arduino Interface & Multithreading
Hi everyone, I am working on a project that requires unity arduino interface and multithreading. My unity arduino interface code works fine without the multithreading. When multithreading is integrated, unity just freezes. I have used debug statements to identify that unity freezes at the serial port's readByte function. Therefore, anyone that has encountered this, please feel free to comment on this. Thank you so much for your time! The following is the c# code:
// Multi-thread Declarations & Synchronization Locks
Thread data_collection;
Thread data_processing;
bool obtain_stop;
bool process_stop;
bool obtain_start;
bool process_start;
AutoResetEvent obtainData_sync;
AutoResetEvent processData_sync;
// Serial Port Declaration
SerialPort sp_in = new SerialPort("COM4", 9600);
void Start()
{
// Serial Port initializations
sp_in.Open();
sp_in.ReadTimeout = 1000;
sp_in.WriteTimeout = 1000;
sp_in.Parity = System.IO.Ports.Parity.None;
sp_in.StopBits = System.IO.Ports.StopBits.One;
sp_in.Handshake = System.IO.Ports.Handshake.None;
sp_in.DtrEnable = true;
sp_in.RtsEnable = true;
// Start data collection Thread
obtain_stop = false;
process_stop = false;
obtain_start = false;
process_start = false;
data_collection = new Thread(obtainData);
data_collection.Start();
data_processing = new Thread(process_data);
data_processing.Start();
// Synchronization
obtainData_sync = new AutoResetEvent(false);
processData_sync = new AutoResetEvent(false);
}
void Update()
{
if (sp_in.IsOpen)
{
try
{
obtain_start = true;
}
catch (System.Exception)
{
}
}
void obtainData()
{
while (!obtain_stop){
if (obtain_start){
counter = 0;
while (counter < 9)
{
Debug.Log((char)sp_in.ReadByte());
}
}
}
}
public void OnApplicationQuit(){
obtain_stop = true;
process_stop = true;
data_collection.Abort();
data_processing.Abort();
}
public void OnDestroy(){
obtain_stop = true;
process_stop = true;
data_collection.Abort();
data_processing.Abort();
}
It's hard to tell what exactly goes wrong here. However here are a few things that are wrong or that you may check:
You have to set the serial port parameters and settings before you open the port.
Currently you never close the port. You should close it in OnDestroy / OnApplicationQuit. You may want to create a seperate method for shutting down your threads and the serial port.
Using Thread.Abort should be avoided if possible. It does not guarantee that the thread actually ter$$anonymous$$ates. The Abort method tries to throw a thread abort exception within the thread context but that does not always work.
You set your read timeout to 1 sec but you do not handle any timeout exceptions inside your "obtainData" thread. That means when a time out exception is thrown your thread will ter$$anonymous$$ate silently.
Your try-catch block inside Update makes no sense. The code in the try block can not throw any exception.
You created those two AutoResetEvents but don't use them anywhere. So your code just looks very unfinished / not well planned.
Inside your obtainData thread the counter doesn't seem to have any function as the variable is never changed.
Note if you don't properly ter$$anonymous$$ate / exit threads you create when testing inside the editor it can freeze the editor as threads can outlive the run-mode
Your answer
Follow this Question
Related Questions
MonoBehaviour.Invoke and Threading 2 Answers
MultiThreading Logic Error? 1 Answer
Multithreading freezes editor 1 Answer
> 0.9f progress of SceneManager.LoadSceneAsync isn't 0 Answers
How to stop waiting on Serial Output 2 Answers