当前位置: 首页 > wzjs >正文

江苏扬州建设局网站虚拟空间能建多个网站

江苏扬州建设局网站,虚拟空间能建多个网站,比较好的友链平台,互联网与网站有哪些一、本文介绍在windows环境下unity与usb串口进行通信的代码 web版本的放在下一个文章 注: 1.我的硬件是检测磁阻液位,用到四字节十六进制浮点数,所以这里会直接转换到十进制。 2.我的硬件会返回9字节响应,所以我会限制响应长度…

一、本文介绍在windows环境下unity与usb串口进行通信的代码

web版本的放在下一个文章

注:

1.我的硬件是检测磁阻液位,用到四字节十六进制浮点数,所以这里会直接转换到十进制。

2.我的硬件会返回9字节响应,所以我会限制响应长度,可以进行适当更改

重要:再开始前一定要改为.NET 4.x,或者.NET Framework,因为.NET standard 2.0或者2.1不包括需要用到的using System.IO.Ports;更换路径为File/Build Setting/Player Settings.../Other Settings/Api Compatibility Level

二、脚本

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Threading;
using UnityEngine;public class SerialPortCommunicationManager : MonoBehaviour
{private static SerialPortCommunicationManager _instance;public static SerialPortCommunicationManager Instance{get{if (_instance == null){GameObject obj = new GameObject("SerialPortCommunicationManager");_instance = obj.AddComponent<SerialPortCommunicationManager>();DontDestroyOnLoad(obj);}return _instance;}}public string portName;// 串口号public int baudRate;// 波特率public int dataBit;//数据位public Parity parity;//校验位public StopBits stopBit;//停止位private SerialPort sp = new SerialPort();private List<byte> receiveBuffer = new List<byte>(); // 用于存储接收到的字节private const int RESPONSE_LENGTH = 9; // 每组响应的长度(9 个字节)/// <summary>/// 打开端口/// </summary>public void OpenPort(){sp = new SerialPort(portName, baudRate, parity, dataBit, stopBit);sp.ReadTimeout = 20;try{sp.Open();Debug.Log("端口已打开");}catch (Exception e){Debug.LogError("端口未打开: " + e.Message);}}/// <summary>/// 关闭端口/// </summary>public void ClosePort(){try{sp.Close();sp.Dispose();Debug.Log("端口已关闭");}catch (Exception e){Debug.LogError("端口无法关闭: " + e.Message);}}/// <summary>/// 检查串口是否打开/// </summary>public bool IsPortOpen(){return sp != null && sp.IsOpen;}/// <summary>/// 发送数据/// </summary>public void SendData(byte[] dataStr){if (sp != null && sp.IsOpen){sp.Write(dataStr, 0, dataStr.Length);Debug.LogWarning("发送成功: " + BitConverter.ToString(dataStr).Replace("-", " "));}else{Debug.LogError("串口未打开,无法发送数据!");}}/// <summary>/// 接收端口数据/// </summary>public void DataReceiveFun(){while (true){if (sp != null && sp.IsOpen){try{int bytesToRead = sp.BytesToRead;if (bytesToRead > 0){byte[] buffer = new byte[bytesToRead];int bytesRead = sp.Read(buffer, 0, bytesToRead);if (bytesRead > 0){// 将读取到的数据添加到缓冲区receiveBuffer.AddRange(buffer);// 按 9 个字节为单位处理数据while (receiveBuffer.Count >= RESPONSE_LENGTH){byte[] response = receiveBuffer.GetRange(0, RESPONSE_LENGTH).ToArray();receiveBuffer.RemoveRange(0, RESPONSE_LENGTH); // 移除已处理的数据// 打印原始响应数据string responseHex = BitConverter.ToString(response).Replace("-", " ");Debug.Log($"收到响应: {responseHex}");// 解析浮点数数据if (response.Length == RESPONSE_LENGTH && response[0] == 0x01 && response[1] == 0x04){// 假设从第 3 字节起 4 个字节是 IEEE 754 单精度浮点数byte[] floatBytes = new byte[4];Array.Copy(response, 3, floatBytes, 0, 4); // 提取第 3 到第 6 字节// 将字节转换为浮点数float floatValue = ConvertHexToFloat(floatBytes);Debug.Log($"解析得到的浮点数: {floatValue}");}else{Debug.LogWarning("响应格式错误!");}}}}}catch (Exception e){Debug.LogError($"接收数据异常: {e.GetType()} - {e.Message}");}}Thread.Sleep(10); // 降低 CPU 占用,10 毫秒足够快}}/// <summary>/// 将 4 个字节转换为 IEEE 754 单精度浮点数/// </summary>private float ConvertHexToFloat(byte[] bytes){// 确保字节顺序正确(大端或小端取决于你的设备,这里假设是大端)if (BitConverter.IsLittleEndian){Array.Reverse(bytes); // 如果是小端机器,翻转字节顺序}// 直接使用 BitConverter 将字节转换为浮点数float result = BitConverter.ToSingle(bytes, 0);return result;}// 字符串转字节流 推荐public 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;}
}

三、使用

using System.Collections;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading;
using UnityEngine;public class UseSerialPort : MonoBehaviour
{private bool isRunning = true; // 控制发送循环的标志void Start(){SerialPortCommunicationManager.Instance.portName="COM3";      // 串口号SerialPortCommunicationManager.Instance.baudRate=9600;         // 波特率SerialPortCommunicationManager.Instance.dataBit=8;           // 数据位SerialPortCommunicationManager.Instance.parity=Parity.None;  // 校验位SerialPortCommunicationManager.Instance.stopBit=StopBits.One; // 停止位SerialPortCommunicationManager.Instance.OpenPort(); // 打开串口// 启动接收线程new Thread(() =>{SerialPortCommunicationManager control = new SerialPortCommunicationManager();control.DataReceiveFun(); // 持续监听数据}).Start();// 启动发送协程,持续发送命令StartCoroutine(SendCommandRoutine());}/// <summary>/// 持续发送命令/// </summary>/// <returns></returns>IEnumerator SendCommandRoutine(){// 这里是我要发送的命令,并且会接收到9字节响应,所以会限制响应长度string commandHex = "01 04 00 02 00 02 D0 0B"; // 与日志中的命令一致byte[] commandBytes = SerialPortCommunicationManager.Instance.Convert16(commandHex.Replace(" ", ""));while (isRunning){SerialPortCommunicationManager.Instance.SendData(commandBytes);// 等待时间yield return new WaitForSeconds(0.1f); }}
}

四、结尾

有任何错误请指出,补充请评论,看到会第一时间回复,谢谢。


文章转载自:

http://ao8O5tUg.cyjdr.cn
http://NVRFRevI.cyjdr.cn
http://zneu81Vy.cyjdr.cn
http://hrwutlq5.cyjdr.cn
http://KD9kNkXV.cyjdr.cn
http://zWuc396N.cyjdr.cn
http://ed35igL7.cyjdr.cn
http://rRJkS3VK.cyjdr.cn
http://kN7TxNnT.cyjdr.cn
http://aqaFMRGk.cyjdr.cn
http://eIquRzXe.cyjdr.cn
http://g4OBz1ZS.cyjdr.cn
http://hD7F9eak.cyjdr.cn
http://wZQ8SNPm.cyjdr.cn
http://au8s71bB.cyjdr.cn
http://h9R8kbWU.cyjdr.cn
http://zp6JmHP5.cyjdr.cn
http://8GXoBIM9.cyjdr.cn
http://ruDuGQ4a.cyjdr.cn
http://IqnuHS7r.cyjdr.cn
http://Ef6tSbjr.cyjdr.cn
http://wedyMZ1Z.cyjdr.cn
http://51RbvHHq.cyjdr.cn
http://4FLrJm6o.cyjdr.cn
http://sztQik4p.cyjdr.cn
http://xoOZuh6T.cyjdr.cn
http://tRchiSEU.cyjdr.cn
http://eaIfLxgL.cyjdr.cn
http://RBhjNe7f.cyjdr.cn
http://z0uuDBe6.cyjdr.cn
http://www.dtcms.com/wzjs/751429.html

相关文章:

  • 社区网站优化桂林市区地图
  • 网站开发入门书籍推荐wordpress怎么提权
  • 广州南沙建设交通网站做网站收费
  • 北京网站建设取名字大全免费查询
  • 西宁市城东区住房和建设局网站少儿编程加盟费一般多少钱
  • 电子商务网站的建站流程制作企业网站费用明细
  • WordPress能够做小说网站吗wordpress 引用 格式
  • 北京建设协会网站网页设计大作业
  • 临湘网站wordpress能用代码吗
  • 国外网站设计的网站phpstudy如何建设网站
  • 网站建设教程学校网站设计接单
  • 做视频网站需要什么证件网络推广专员好做吗
  • 建设网站的页面设计苏州网站设计电话
  • 株洲网站排名无锡新吴区住房建设交通局网站
  • 世界杯直播观看网站三大框架网站开发
  • 企业网站的基本类型包括wordpress主题 摄影师
  • 网站建设管理工作经验介绍网站开发专家:php+mysql网站开发技术与典型案例导航
  • 越秀微网站建设婚纱网站策划书模板
  • 用dw制作视频网站开源小程序模板
  • 龙江网站开发网站备案管局审核
  • 上海网站备案查询微信公众号平台入口官网
  • 网站运营主要做什么wordpress print_r
  • 2021不良正能量免费网站网站排名优化软件哪家好
  • 公司网站设计意见收集自己给公司做网站
  • 网站挂标 怎么做阿里巴巴电子商务网站
  • 阿里巴巴网站策划书企业网站建设报告
  • 免费网站设计软件wordpress自动标签
  • 曲沃网站开发网络推广的几种主要方法
  • asp做的手机网站网页设计表单制作代码
  • 制作企业网站怎么报价南昌网站建设推广专家