88 lines
2.3 KiB
C#
88 lines
2.3 KiB
C#
// using System;
|
||
// using System.IO.Ports;
|
||
// using GeneralTools;
|
||
// using UnityEngine;
|
||
|
||
// public class SerialPortSender : MonoBehaviour
|
||
// {
|
||
// public static SerialPortSender Instance { get; private set; }
|
||
// private SerialPort serialPort;
|
||
|
||
// // 端口号根据你的实际设备设置(如 "COM3")
|
||
// string portName = "COM1";
|
||
// int baudRate = 9600;
|
||
|
||
// void Awake()
|
||
// {
|
||
// Instance = this;
|
||
// portName = ConfigHelper.PortName;
|
||
// baudRate = ConfigHelper.BaudRate;
|
||
// OpenSerialPort();
|
||
|
||
// }
|
||
|
||
// void OnApplicationQuit()
|
||
// {
|
||
// CloseSerialPort();
|
||
// }
|
||
|
||
// public void OpenSerialPort()
|
||
// {
|
||
// try
|
||
// {
|
||
// serialPort = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One);
|
||
// serialPort.Open();
|
||
// serialPort.ReadTimeout = 1000;
|
||
// serialPort.WriteTimeout = 1000;
|
||
// Debug.Log("串口打开成功");
|
||
// }
|
||
// catch (Exception e)
|
||
// {
|
||
// Debug.LogError("串口打开失败: " + e.Message);
|
||
// }
|
||
// }
|
||
|
||
// public void CloseSerialPort()
|
||
// {
|
||
// if (serialPort != null && serialPort.IsOpen)
|
||
// {
|
||
// serialPort.Close();
|
||
// Debug.Log("串口关闭");
|
||
// }
|
||
// }
|
||
|
||
// /// <summary>
|
||
// /// 发送十六进制命令,例如 "FF 01 02"
|
||
// /// </summary>
|
||
// public void SendHexCommand(string hexCommand)
|
||
// {
|
||
// if (serialPort == null || !serialPort.IsOpen)
|
||
// {
|
||
// Debug.LogWarning("串口未打开!");
|
||
// return;
|
||
// }
|
||
|
||
// try
|
||
// {
|
||
// byte[] buffer = HexStringToBytes(hexCommand);
|
||
// serialPort.Write(buffer, 0, buffer.Length);
|
||
// Debug.Log("发送指令: " + hexCommand);
|
||
// }
|
||
// catch (Exception ex)
|
||
// {
|
||
// Debug.LogError("发送失败: " + ex.Message);
|
||
// }
|
||
// }
|
||
|
||
// private byte[] HexStringToBytes(string hex)
|
||
// {
|
||
// string[] hexValues = hex.Trim().Split(' ');
|
||
// byte[] bytes = new byte[hexValues.Length];
|
||
// for (int i = 0; i < hexValues.Length; i++)
|
||
// {
|
||
// bytes[i] = Convert.ToByte(hexValues[i], 16);
|
||
// }
|
||
// return bytes;
|
||
// }
|
||
// }
|