gpt4 book ai didi

c# - Unity无法打开COM口

转载 作者:太空狗 更新时间:2023-10-30 00:41:46 24 4
gpt4 key购买 nike

我正在使用 arduino 板将串行数据传送到 Unity。我使用电路板随附的读取模拟电压样本以及调试日志中愉快显示的输出来完成这项工作。

但是,现在当我运行 Unity 时出现以下错误:

IOException: The port `COM11' does not exist.

我已将我的 COM 端口更改为各种数字,但它们都返回相同的错误。

我的串口读取代码是这样的:

SerialPort stream = null;
string data = "Ready";


private float DataTimer = 2.0f;
private float TimeToCheckStream = 0.1f; // check data every second
public string COMPort = "";
public int baudRate = 9600;



void Awake ()
{

stream = new SerialPort(COMPort,baudRate); //originally 9600
Debug.Log ("Initialized stream");

LogWriter writer = LogWriter.Instance;
writer.WriteToLog( COMPort);
}



void Start ()
{
// LogWriter writer = LogWriter.Instance;
// writer.WriteToLog("Testing test");

if ( stream != null )
{
if ( stream.IsOpen ) // close if already open
{
stream.Close();
Debug.Log ("Closed stream");
}

stream.Open();
Debug.Log ("Opened stream");
}

else
{
Debug.Log ("ERROR: Uninitialized stream");
}

}


void Update ()
{

if(DataTimer < TimeToCheckStream)
{
DataTimer += Time.deltaTime;
}
else
{
DataTimer = 0.0f;

if ( stream != null )
{
if ( stream.IsOpen )
{
// if stream is open do things in here
stream.ReadLine();
Debug.Log(stream.ReadLine().ToString());
}
}

else
{
Debug.Log ("NULL stream");
}
}
}



void OnGUI ()
{
GUI.Label ( new Rect(500,10,300,100), data );
}



void OnApplicationQuit ()
{

if ( stream != null )
{
stream.Close();
}
}

为什么我的 COM 端口会突然决定自行关闭?

最佳答案

您可以访问您的 COM11 并将其替换为 \\.\COM11你必须写:

myPort= new SerialPort("\\\\.\\COM11",9600);

看看Microsoft's Website

关于c# - Unity无法打开COM口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18790411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com