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

查看:23
本文介绍了为什么串口在 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.

我用 Visual 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.

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

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

摘自 http://www.mono-project.com/archived/howtosystemioports/#限制

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

"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天全站免登陆