3

我正在尝试统一打开一个 SerialPort。它在编辑器和单声道构建中工作。但是当我使用 iL2CPP 脚本后端构建游戏时。它抛出带有消息的异常The port \\.\COM3 does not exist.

这与 Unity IL2CPP 构建有关吗?

Unity播放器设置:

脚本运行时版本: .Net 4.x 等效版本

API 兼容性级别: .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());
    }
} 
4

0 回答 0