I am trying to Open a SerialPort in unity. It is working in editor and mono build. But when I build the game with iL2CPP scripting backend. It is throwing an exception with message The port \\.\COM3 does not exist.
Is this something related to Unity IL2CPP build?
Unity player settings:
Scripting Runtime Version: .Net 4.x Equivalent
API compatibility Level: .NET 4.x
public void OpenSerialPort()
{
try
{
string ComPort = @"\\.\COM3"; //I even tried "COM3"
int BaudRate = 9600;
// Initialise the serial port
SerialPort = new SerialPort(ComPort, BaudRate);
SerialPort.ReadTimeout = ReadTimeout;
SerialPort.WriteTimeout = WriteTimeout;
SerialPort.DtrEnable = true;
SerialPort.RtsEnable = true;
// Open the serial port
SerialPort.Open();
Debug.LogError("SerialPort successfully opened!");
}
catch (UnauthorizedAccessException ex)
{
// Failed to open com port or start serial thread
Debug.LogError("UnauthorizedAccessException: " + ex.Message.ToString());
}
catch (ArgumentOutOfRangeException ex)
{
// Failed to open com port or start serial thread
Debug.LogError("ArgumentOutOfRangeException: " + ex.Message.ToString());
}
catch (ArgumentException ex)
{
// Failed to open com port or start serial thread
Debug.LogError("ArgumentException : " + ex.Message.ToString());
}
catch (InvalidOperationException ex)
{
// Failed to open com port or start serial thread
Debug.LogError("ArgumentException : " + ex.Message.ToString());
}
catch (IOException ex)
{
// Failed to open com port or start serial thread
Debug.LogError("IOException : " + ex.Message.ToString());
}
}