Why are serial ports not working as expected in Unity?

I need help, I'm desperate

During the two weeks I was working on my project, this uses serial communication (serial PIC board). I have to establish a connection, but I cannot receive data from the COM port. I have read some forums and the reason for the problem is an incomplete implementation of the System.IO.Ports class .

When I try to receive COM port data, the SerialDataReceivedEventHandler event (represents the method that will handle the SerialPort object's DataReceived event.) Is not called or activated. I tried to solve it but I didn't find a definitive solution. I decided to prove an external DLL, but a friend told me the problem would continue, in fact I did it and got the same problem: SerialDataReceivedEventHandler doesn't work. Also, someone recommended that I use a secondary thread, although I don't understand how to do it at all.

I wrote a program in Visual C # and everything works fine. I am intrigued.

I need to find a solution, some idea or some good documentation. If anyone knows something about this, please help me.

I need to understand the reason for this in order to continue.

+3


source to share


2 answers


Unity is based on Mono and Mono does not fully implement the Serial class, in particular no notifications are implemented (e.g. SerialDataReceivedEvent).

This is why it works in Visual Studio and not Unity.

Here are the differences between Mono and the full .NET implementation of the Serial class:

Excerpt from http://www.mono-project.com/archived/howtosystemioports/#limitations



"Limitation At the time of this writing, there are several limitations that need to be taken into account:

1) No event notification for received serial data. If you want to receive data, you need to set a timeout and see the received data by polling ReadByte () if you think there may be data.

2) It is only necessary to read data in byte [] format - there is no char [] support. You have to do your own byte reading and translate that to your encoding.

3) DiscardNull, ParityReplace, ReceivedBytesThreshold are not implemented. "

+1


source


I think this is because Unity is based on Mono instead of .Net and a fairly old version. You couldn't use Linq on iOS devices for a long time due to AOT errors, and the localization implementation was a bug (or at least it was in previous versions of Unity that I tried to work with). I couldn't even find the source of System.IO.Ports in the Unity Mono fork source , so it's surprising that it compiles at all.



0


source







All Articles