当前位置:网站首页>Unity obtains serial port data
Unity obtains serial port data
2022-06-30 05:11:00 【Shannan HSY】
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
using System;
using System.Threading;
using UnityEngine.UI;
public class PortManager {
private SerialPort sp = null;// port
bool isActive = false;
private Thread athread;
public Action<string> ReadDataEvent;
object lockd = new object();
/// <summary>
/// Initialize port
/// </summary>
public void InitPort(string portName,int baudRate) {
isActive = true;
sp = new SerialPort("\\\\.\\" + portName, baudRate);
}
/// <summary>
/// Open the port
/// </summary>
public void OpenPort() {
try {
sp.Open();
} catch( Exception ex ) {
Debug.Log(ex.Message);
}
}
/// <summary>
/// Close the port
/// </summary>
public void ClosePort() {
try {
sp.Close();
} catch( Exception ex ) {
Debug.Log(ex.Message);
}
}
/// <summary>
/// Get all ports
/// </summary>
/// <returns></returns>
public string[] GetAllPorts() {
return SerialPort.GetPortNames();
}
/// <summary>
/// Start reading data
/// </summary>
public void StartReadData() {
athread = new Thread(new ThreadStart(GoThread));
athread.IsBackground = true;
athread.Start();
}
void GoThread() {
while( isActive ) {
lock( lockd ) {
ReadPortData();
}
Thread.Sleep(10);
}
}
private void ReadPortData() {
if( sp != null && sp.IsOpen ) {
try {
string portValue = sp.ReadLine();
if( !string.IsNullOrEmpty(portValue) && ReadDataEvent != null ) {
ReadDataEvent(portValue);
}
} catch( Exception ex ) {
Debug.Log(ex.Message);
}
} else {
Debug.LogError(" Please check the port settings !");
}
}
}边栏推荐
- 003-JS-DOM-Attr-innerText
- Under what conditions does the Z-index attribute expire?
- Oracle-数据的基本操作
- Solution to Autowired annotation warning
- Preorder traversal of Li Kou 589:n fork tree
- Unity notes_ SQL Function
- Ugui uses its own function to realize reverse mask
- Force buckle 977 Square of ordered array
- UnityEngine. JsonUtility. The pit of fromjason()
- Unity lens making
猜你喜欢
随机推荐
Go Land no tests were Run: FMT cannot be used. Printf () & lt; BUG & gt;
OpenGL draws model on QT platform to solve the problem of initializing VAO and VBO
Unity multiple UI page turning left and right
Unity a* road finding force planning
Intellj idea jars projects containing external lib to other project reference methods - jars
Chapter 11 advanced data management of OpenGL super classic (version 7)
Deeply understand the function calling process of C language
Detailed explanation of sorting sort method of JS array
LXC 和 LXD 容器总结
Force buckle 704 Binary search
Unity/ue reads OPC UA and OPC Da data (UE4)
Unity scroll view element drag and drop to automatically adsorb centering and card effect
Ugui uses its own function to realize reverse mask
Unity script life cycle and execution sequence
Harbor API 2.0 query
Records of problems encountered in unity + hololens development
Unity profiler performance analysis
Pytorch的安装以及入门使用
Revit Secondary Development - - Project use Panel features not opened
One command to run rancher









