COM 端口无法打开 Unity

COM Port cannot be opened Unity(COM 端口无法打开 Unity)

本文介绍了COM 端口无法打开 Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 arduino 板将串行数据传送到 Unity.我使用电路板附带的读取模拟电压样本和调试日志中愉快地显示的输出进行了这项工作.

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

<块引用>

IOException:端口COM11"不存在.

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

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

SerialPort 流 = null;字符串数据=准备好";私有浮点数据定时器 = 2.0f;私人浮动 TimeToCheckStream = 0.1f;//每秒检查一次数据公共字符串 COMPort = "";公共 int 波特率 = 9600;无效唤醒(){流 = 新串行端口(COMPort,波特率);//原来是9600Debug.Log("初始化流");LogWriter writer = LogWriter.Instance;writer.WriteToLog(COMPort);}无效开始(){//LogWriter writer = LogWriter.Instance;//writer.WriteToLog("测试测试");如果(流!= null){if (stream.IsOpen)//如果已经打开则关闭{流.关闭();Debug.Log("关闭的流");}流.打开();Debug.Log("打开的流");}别的{Debug.Log("错误:未初始化的流");}}无效更新(){if(DataTimer < TimeToCheckStream){DataTimer += Time.deltaTime;}别的{数据定时器 = 0.0f;如果(流!= null){如果(流.IsOpen){//如果流是打开的,请在此处执行操作流.ReadLine();Debug.Log(stream.ReadLine().ToString());}}别的{Debug.Log("NULL 流");}}}无效的 OnGUI (){GUI.Label(新矩形(500,10,300,100),数据);}无效 OnApplicationQuit (){如果(流!= null){流.关闭();}}

我的 COM 端口突然决定自行关闭有什么原因吗?

解决方案

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

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

看看 微软网站 p>

I'm using an arduino board to communicate serial data into Unity. I had this working using the read analog voltage sample that comes with the board and the output from that happily displayed in the debug log.

However, now when I run Unity i get the following error:

IOException: The port `COM11' does not exist.

I've changed my COM port to be a variety of numbers but they all come back with the same error.

My serial port reading code is thus:

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();
    }
}

Is there any reason as to why my COM port would suddenly decide to close itself?

解决方案

You can access your COM11 with replacing it with \.COM11 You must write:

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

Take a look at Microsoft's Website

这篇关于COM 端口无法打开 Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:COM 端口无法打开 Unity

基础教程推荐