为什么串口不能在Unity中正常工作? [英] Why don't serial ports work properly in Unity?

查看:213
本文介绍了为什么串口不能在Unity中正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助,我很绝望

在我从事项目的两个星期中,这使用串行端口通信(PIC串行板).我必须建立连接,但无法从COM端口获取数据.我读过一些论坛,问题的原因似乎是 System.IO.Ports类的实现不完整.

During two weeks I have been working in my project, this uses serial port communication (a PIC serial board). I got to set the connection up, but I can not get data from the COM port. I've read some forums and the cause of the problem seems to be the incomplete implementation of System.IO.Ports class.

当我尝试获取COM端口的数据时,未调用或激活事件 SerialDataReceivedEventHandler (表示将处理SerialPort对象的DataReceived事件的方法.).我试图解决它,但找不到最终解决方案.我本想证明是一个外部DLL,但是一个朋友告诉我该问题将继续存在,实际上我做到了,并遇到了相同的问题: SerialDataReceivedEventHandler 不起作用.另外,有人建议我使用辅助线程,尽管我根本不知道该怎么做.

When I try to get data of the COM port, the event SerialDataReceivedEventHandler (represents the method that will handle the DataReceived event of a SerialPort object.) is not called or activated. I tried to resolve it but I don't find a definitive solution. I thought to prove a external DLL, but a friend told me that the problem will go on, in fact I did it and got the same problem: SerialDataReceivedEventHandler does not work. Also, someone recommended me using a secondary thread, although I don´t understand how to do it at all.

我用可视C#编写了一个程序,并且一切正常.我很感兴趣.

I wrote a program in visual C# and everything works fine. I'm intrigued.

我需要找到解决方案,一些想法或好的文档.如果有人对此有所了解,请帮助我.

I need to find a solution, some idea or good documentation. If there's someone knows something about it, help me please.

我需要了解导致这种情况的原因.

I need to understand the cause of this to continue.

推荐答案

Unity基于Mono,而Mono并没有完全实现Serial类,特别是没有实现任何通知(例如SerialDataReceivedEvent).

Unity is based on Mono, and Mono doesn't implement completely the Serial class, in particular there are no notifications implemented (such as SerialDataReceivedEvent).

这就是为什么它可以在Visual Studio中运行,而不能在Unity中运行的原因.

That's why it works in Visual Studio, and not in Unity.

以下是Mono和Serial类的完整.NET实现之间的区别:

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

http://www.mono-project.com/archived/howtosystemioports中提取/#limitations

限制 在撰写本文时,有一些必须注意的限制:

"Limitations At the time of this writing, there are a a few limitations that one must take note:

1) 没有针对接收到的串行数据的事件通知. .如果要接收数据,则必须设置超时并通过轮询ReadByte()来监视接收到的数据. ),当您认为可能有数据时.

1) There is no event notification for received serial data. If you want to receive data, one must set a timeout and watch for received data by polling ReadByte() when you think there might be data.

2)一个必须只能读取byte []格式的数据–不支持char [].您必须自己读取字节并将其转换为编码.

2) One must Read data in byte[] format only – there is no char[] support. You must do your own reading of bytes and translate that into your encoding.

3)未实现DiscardNull,ParityReplace,ReceivedBytesThreshold."

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

这篇关于为什么串口不能在Unity中正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆