小编用的版本是2018.4.0
首先想使用Unity开发串口通信,必须要做的 一点就是 要使用Mono.NET 2.0/4.0/其他
如下图:
不修改的话是不能进行串口开发的,可能Unity不想让大家用来干些别的事吧 (ˉ▽ ̄~)
public static string portName;//串口号
public static int baudRate;//波特率
public static Parity parity;//校验位
public static int dataBit;//数据位
public static StopBits stopBit;//停止位
///
/// 打开端口
///
public static void OpenPort()
{
sp = new SerialPort(portName, baudRate, parity, dataBit, stopBit);
sp.ReadTimeout = 1000;
try
{
sp.Open();
Debug.Log("打开端口成功");
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
///
/// 发送数据
///
///
public static void SendData(byte[] dataStr)
{
sp.Write(dataStr, 0, dataStr.Length);
Debug.LogWarning("发送成功");
}
///
/// 接收端口数据
///
public void DataReceiveFun()
{
while (true)
{
if (sp != null && sp.IsOpen)
{
try
{
bytes = sp.Read(buffer, 0, buffer.Length);
sp.DiscardOutBuffer();
if (bytes == 0)
{
continue;
}
else
{
for (int i = 0; i < buffer.Length; i++)
{
Debug.Log(buffer[i].ToString("x8"));
}
}
}
catch (Exception e)
{
Debug.Log(e);
}
}
Thread.Sleep(500);
}
}
//十进制转二进制
Console.WriteLine(Convert.ToString(69, 2));
//十进制转八进制
Console.WriteLine(Convert.ToString(69, 8));
//十进制转十六进制
Console.WriteLine(Convert.ToString(69, 16));
//二进制转十进制
Console.WriteLine(Convert.ToInt32("100111101", 2));
//八进制转十进制
Console.WriteLine(Convert.ToInt32("76", 8));
//16进制转换10进制
Console.WriteLine(Convert.ToInt32("FF", 16));
///
/// 字符串转字节流
///
///
///
public static byte[] HexStringToBytes(string hexStr)
{
if (string.IsNullOrEmpty(hexStr))
{
return new byte[0];
}
if (hexStr.StartsWith("0x"))
{
hexStr = hexStr.Remove(0, 2);
}
var count = hexStr.Length;
var byteCount = count / 2;
var result = new byte[byteCount];
for (int ii = 0; ii < byteCount; ++ii)
{
var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
result[ii] = tempBytes;
}
return result;
}
//第二种 常用转换方式 推荐
public static byte[] Convert16(string strText)
{
strText = strText.Replace(" ", "");
byte[] bText = new byte[strText.Length / 2];
for (int i = 0; i < strText.Length / 2; i++)
{
bText[i] = Convert.ToByte(Convert.ToInt32(strText.Substring(i * 2, 2), 16));
}
return bText;
}
推荐使用第二种转换方式,多次测试可行很稳定。
拿走不谢,直接转换成字符串, 有了转字符串,肯定要有再转回来的方法,对不对
///
/// 字节流转字符串
///
///
///
public static string BytesTohexString(byte[] bytes)
{
if (bytes == null || bytes.Length < 1)
{
return string.Empty;
}
var count = bytes.Length;
var cache = new StringBuilder();
cache.Append("0x");
for (int ii = 0; ii < count; ++ii)
{
var tempHex = Convert.ToString(bytes[ii], 16).ToUpper();
cache.Append(tempHex.Length == 1 ? "0" + tempHex : tempHex);
}
return cache.ToString();
}
这个就用处不大了,大家斟酌使用~~
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System.Threading;
using System;
using System.Text;
///
/// 串口通信
///
public class SerialPortControl
{
public static string portName;//串口号
public static int baudRate;//波特率
public static Parity parity;//校验位
public static int dataBit;//数据位
public static StopBits stopBit;//停止位
static SerialPort sp = new SerialPort();
///
/// 串口号
///
///
///
public static string PortName(string portname)
{
portName = portname;
return portName;
}
///
/// 波特率
///
///
///
public static int BauRate(int baud)
{
baudRate = baud;
return baudRate;
}
///
/// 校验位
///
///
///
public static Parity Paritys(Parity paritys)
{
parity = paritys;
return parity;
}
///
/// 数据位
///
///
///
public static int DataBits(int dataBits)
{
dataBit = dataBits;
return dataBit;
}
///
/// 停止位
///
///
///
public static StopBits StopBitss(StopBits stopBits)
{
stopBit = stopBits;
return stopBit;
}
///
/// 字节流转字符串
///
///
///
public static string BytesTohexString(byte[] bytes)
{
if (bytes == null || bytes.Length < 1)
{
return string.Empty;
}
var count = bytes.Length;
var cache = new StringBuilder();
cache.Append("0x");
for (int ii = 0; ii < count; ++ii)
{
var tempHex = Convert.ToString(bytes[ii], 16).ToUpper();
cache.Append(tempHex.Length == 1 ? "0" + tempHex : tempHex);
}
return cache.ToString();
}
///
/// 字符串转字节流
///
///
///
public static byte[] HexStringToBytes(string hexStr)
{
if (string.IsNullOrEmpty(hexStr))
{
return new byte[0];
}
if (hexStr.StartsWith("0x"))
{
hexStr = hexStr.Remove(0, 2);
}
var count = hexStr.Length;
var byteCount = count / 2;
var result = new byte[byteCount];
for (int ii = 0; ii < byteCount; ++ii)
{
var tempBytes = Byte.Parse(hexStr.Substring(2 * ii, 2), System.Globalization.NumberStyles.HexNumber);
result[ii] = tempBytes;
}
return result;
}
//字符串转字节流 推荐======
public static byte[] Convert16(string strText)
{
strText = strText.Replace(" ", "");
byte[] bText = new byte[strText.Length / 2];
for (int i = 0; i < strText.Length / 2; i++)
{
bText[i] = Convert.ToByte(Convert.ToInt32(strText.Substring(i * 2, 2), 16));
}
return bText;
}
///
/// 打开端口
///
public static void OpenPort()
{
sp = new SerialPort(portName, baudRate, parity, dataBit, stopBit);
sp.ReadTimeout = 1000;
try
{
sp.Open();
Debug.Log("打开端口成功");
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
///
/// 关闭端口
///
public static void ClosePort()
{
try
{
sp.Close();
sp.Dispose();
Debug.Log("关闭端口");
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
byte[] buffer = new byte[5];
int bytes = 0;
///
/// 接收端口数据
///
public void DataReceiveFun()
{
while (true)
{
if (sp != null && sp.IsOpen)
{
try
{
bytes = sp.Read(buffer, 0, buffer.Length);
sp.DiscardOutBuffer();
if (bytes == 0)
{
continue;
}
else
{
for (int i = 0; i < buffer.Length; i++)
{
Debug.Log(buffer[i].ToString("x8"));
}
}
}
catch (Exception e)
{
Debug.Log(e);
}
}
Thread.Sleep(500);
}
}
///
/// 发送数据
///
///
public static void SendData(byte[] dataStr)
{
sp.Write(dataStr, 0, dataStr.Length);
Debug.LogWarning("发送成功");
}
}