Jtro的技术分享:unity的串口通讯
2020-07-07 本文已影响0人
UnityPlane
今天一天时间都在面向Google编程.这也蛮头疼的,哈哈

串行接口(串口)通常指COM接口,是采用串行通信方式的扩展接口。串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另一根线接收数据。特别适用于无网络通信。软硬件结合.
首先需要一块硬件开发同学的一块板子,里面刷好了他预制的"系统",然后给你一份测试说明:
然后你还需要一个串口测试工具,这个工具我会放在下方评论区
按照硬件开发同学约定好的,设置串口通讯工具的配置,点击"手动发送",即可看到返回的消息
到这里,确定这个串口通讯正常.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System.Threading;
using System;
using System.Text;
using UnityEngine.UI;
using System.IO;
public class PortController : MonoBehaviour
{
//public GUIText gui;
//定义基本信息
public string portName = "COM3";
public int baudRate = 115200;
public Parity parity = Parity.None;
public int dataBits = 8;
public StopBits stopBits = StopBits.One;
public string[] data;//用于存储6位数据
SerialPort sp = null;//串口控制
Thread dataReceiveThread;
//发送
string message = "";
string filename;
//byte[] message=new byte[8];
void Start()
{
filename = Application.dataPath + @"\StreamingAssets\配置文件.txt";
portName = ReadALine(filename, 1);
baudRate = int.Parse(ReadALine(filename, 3));
OpenPort();//打开串口
dataReceiveThread = new Thread(new ThreadStart(DataReceiveFunction));//数据接收线程
dataReceiveThread.Start();
WriteData(strToToHexByte("01 03 00 00 00 03 05 CB "));
}
public void OpenPort()
{
sp = new SerialPort(portName, baudRate, parity, dataBits, stopBits);
sp.ReadTimeout = 400;
try
{
sp.Open();
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
}
public void WriteData(byte[] bys)
{
if (sp.IsOpen)
{
sp.Write(bys, 0, bys.Length);
Debug.Log("发送完成");
}
}
void OnApplicationQuit()
{
ClosePort();
}
public void ClosePort()
{
try
{
sp.Close();
}
catch (Exception ex)
{
Debug.Log(ex.Message);
}
}
//数据接收功能
void DataReceiveFunction()
{
byte[] buffer = new byte[128];
int bytes = 0;
//定义协议
//int flag0 = 0xFF;
//int flag1 = 0xAA;
int index = 0;//用于记录此时的数据次序
while (true)
{
Thread.Sleep(100);
if (sp != null && sp.IsOpen)
{
try
{
bytes = sp.Read(buffer, 0, buffer.Length);
data = new string[bytes];
for (int i = 0; i < bytes; i++)
{
data[index] = buffer[i].ToString("X2");
index++;
}
Debug.Log(buffer[0]);
Debug.Log(data[0]);
//for (int i = 0; i < bytes; i++)
//{
// if (buffer[i] == flag0 || buffer[i] == flag1)
// {
// index = 0;//次序归0
// continue;
// }
// else
// {
// if (index >= data.Length) index = data.Length - 1;//理论上不应该会进入此判断,但是由于传输的误码,导致数据的丢失,使得标志位与数据个数出错
// data[index] = buffer[i];//将数据存入data中
// index++;
// }
//}
}
catch (Exception ex)
{
Debug.Log(ex.Message);
if (ex.GetType() != typeof(ThreadAbortException))
{
}
}
}
}
}
public string ReadALine(string FileName, int linenumber) // 参数1:打开的文件(路径+文件命),参数2:读取的行数
{
string[] strs = File.ReadAllLines(FileName);
if (linenumber == 0)
{
return "0";
}
else
{
return strs[linenumber];
}
}
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
IEnumerator Wirte(byte[] bsy)
{
yield return new WaitForSeconds(0.5f);
WriteData(bsy);
}
}
注意,这里只是hex格式的数据文件,也就是16进制的格式,其他格式的用到了我在更新.
运行之后的结果: