using UnityEngine;
using System;
using System.IO.Ports;
using UnityEngine.UI;
public class spSend : MonoBehaviour
{
public SerialPort sp;
/* public Text text;*/
static public byte[] strSend = new byte[3];
void Start()
{
sp = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
//串口初始化
try
{
sp.Open();
Debug.Log("成功打开");
//text.text = "成功打开";
}
catch (Exception ex)
{
//text.text = ex.ToString();
Debug.Log(ex);
}
}
//关闭串口
void OnApplicationQuit()
{
sp.Close();
Application.Quit();
}
//发送
public void SendData(byte[] data)
{
if (sp.IsOpen)
{
//text.text = data[0].ToString();
sp.Write(data, 0, data.Length);
}
}
}
使用
public void SendMsg(string s)
{

本文详细介绍如何在Unity中实现串口通信,包括串口初始化、数据发送与接收的完整流程,以及如何将字符串转换为十六进制字节数据进行传输。
最低0.47元/天 解锁文章
4068

被折叠的 条评论
为什么被折叠?



