C# 从串行端口读取值
我试图在unity中创建一个程序,从serialport读取值,我的程序由于C# 从串行端口读取值,c#,unity3d,C#,Unity3d,我试图在unity中创建一个程序,从serialport读取值,我的程序由于serial.ReadLine()而冻结,如果我将其更改为serial.ReadByte(),它将读取字节,现在唯一的选项是我想知道如何将这些字节转换为字符串。 我的代码如下: using UnityEngine; using System.Collections; using System.IO.Ports; using System; using System.Threading; using UnityEngine
serial.ReadLine()
而冻结,如果我将其更改为serial.ReadByte()
,它将读取字节,现在唯一的选项是我想知道如何将这些字节转换为字符串。
我的代码如下:
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System;
using System.Threading;
using UnityEngine.UI;
public class leti3 : MonoBehaviour {
public SerialPort serial = new SerialPort("COM5", 115200);
public GameObject personaje;
// Use this for initialization
void Start()
{
serial.Open();
//serial.ReadTimeout = 1;
}
// Update is called once per frame
void Update()
{
if (serial.IsOpen)
{
try
{
print(serial.ReadByte()); //reads bytes
}
catch (System.Exception)
{
Debug.Log("Error!");
}
}
}
}
ReadLine
被阻塞。它将阻止执行,直到到达行尾。您希望改为使用事件
在处理程序中,可用于获取缓冲区中当前的字符串。您必须管理该字符串可能只是您收到的消息的一部分
或者,您可以将字节读入数组,然后使用适当的编码
如果您不能使用datareceived
并且必须使用阻塞ReadByte
,那么您应该仍然能够在更新中执行类似的操作:
var toRead = serial.BytesToRead(); // make sure there actually are bytes to read first
for (int i=0; i < toRead; i++)
{
// where msgArray is an array for storing the incoming bytes
// and idx is the current idx in that array
msgArray[idx] = serial.ReadByte(); // this should be quick, because it's already
// in the buffer
if (msgArray[idx] = newLineChar) // whatever newLineChar you are using
{
// Read your string
var msg = Encoding.ASCII.GetString(msgArray,0,idx); // or whatever encoding you
// are using
idx = 0;
}
else
{
idx++;
}
}
var toRead=serial.BytesToRead();//确保确实有字节要首先读取
for(int i=0;i
ReadLine
被阻塞。它将阻止执行,直到到达行尾。您希望改为使用事件
在处理程序中,可用于获取缓冲区中当前的字符串。您必须管理该字符串可能只是您收到的消息的一部分
或者,您可以将字节读入数组,然后使用适当的编码
如果您不能使用datareceived
并且必须使用阻塞ReadByte
,那么您应该仍然能够在更新中执行类似的操作:
var toRead = serial.BytesToRead(); // make sure there actually are bytes to read first
for (int i=0; i < toRead; i++)
{
// where msgArray is an array for storing the incoming bytes
// and idx is the current idx in that array
msgArray[idx] = serial.ReadByte(); // this should be quick, because it's already
// in the buffer
if (msgArray[idx] = newLineChar) // whatever newLineChar you are using
{
// Read your string
var msg = Encoding.ASCII.GetString(msgArray,0,idx); // or whatever encoding you
// are using
idx = 0;
}
else
{
idx++;
}
}
var toRead=serial.BytesToRead();//确保确实有字节要首先读取
for(int i=0;i
NB您可能需要设置许多质量。握手、.DataBits、超时等。NB您可能需要设置许多质量。握手、.DataBits、超时等。Unity使用旧版本的Mono,DataReceived
在此版本中不起作用。上次我检查时,您可以发送到串行,但无法接收。这即使在使用线程时也适用。他唯一的选择似乎是在C++中制作插件。听起来很疯狂,但大多数Unity用户就是这样使用串口的,直到这个问题解决。是的,它确实不能正常工作,因为在简单c#程序中,readline工作得很好,但在unity@anthraxa:然后你仍然可以ReadByte
到一个数组中,然后使用Encoding.GetString
在你到达行的末尾时将其转换为字符串。是的,这是我知道的唯一选项,感谢您的帮助,guysUnity使用旧版本的Mono,DataReceived
在此版本中不起作用。上次我检查时,您可以发送到串行,但无法接收。这即使在使用线程时也适用。他唯一的选择似乎是在C++中制作插件。听起来很疯狂,但大多数Unity用户就是这样使用串口的,直到这个问题解决。是的,它确实不能正常工作,因为在简单c#程序中,readline工作得很好,但在unity@anthraxa:然后你仍然可以ReadByte
到一个数组中,然后使用Encoding.GetString
在你到达行的末尾时将其转换为字符串。是的,这是我知道的唯一选项,谢谢大家的帮助