交互测试效果
1.准备开发环境
1.下载安装LeapMotion SDK (大家可以自行下载)
2.Unity 编辑器我选择的是2018.4.32f1
2.功能部分
1.确定要连接的串口号和波特率 程序直接上代码
using KoboldCom;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class MyPorts : MonoBehaviour
{
// Start is called before the first frame update
Communicator communicator;
public bool isHex = true;
public Queue<String[]> DistanceData;
public static MyPorts _instance;
string[] config;
//----
public string[] configData;
public int COM;
public int BTL;
public Text txtResult;
private void Awake()
{
DistanceData = new Queue<string[]>();
_instance = this;
}
void Start()
{
#if UNITY_EDITOR
config = File.ReadAllLines(Application.streamingAssetsPath + "/Config.txt");
#endif
#if UNITY_STANDALONE_WIN
config = File.ReadAllLines(Application.dataPath + "/StreamingAssets/Config.txt");
#endif
//config = File.ReadAllLines(Application.streamingAssetsPath + "/Config.txt");
//创建通讯器
//-----
GetConfigData();
COM = int.Parse(configData[0]);
BTL = int.Parse(configData[1]);
communicator = new Communicator(new SerialPort(), null);
openCom();
StartCoroutine(QingQiu());
}
IEnumerator QingQiu()
{
while (true)
{
yield return null;
if (LeapGestureTool.instance.isQin)
{
//Debug.Log(((int)LeapGestureTool.instance.distance/4).ToString("X2"));
btnSend(((int)LeapGestureTool.instance.distance / 4).ToString("X2"));
}
}
}
public void openCom()
{
if (communicator == null)
{
// Debug.Log("communicator 未初始化");
txtResult.text = "communicator 未初始化";
return;
}
SerialPortSetting sps = new SerialPortSetting();
sps.Baudrate = BTL; //波特率
sps.Port = COM;//串口号Com3
if (communicator.Com.Open(sps))
{
//Debug.Log("串口打开成功");
txtResult.text = "串口打开成功";
}
else
{
txtResult.text = "串口打开失败";
// Debug.Log("串口打开失败");
}
}
private void OnDestroy()
{
closeCom();
}
public void closeCom()
{
if (communicator == null)
{
Debug.Log("communicator 未初始化");
return;
}
communicator.Com.Close();
}
public void btnSend(string senddata)
{
sendData(strToToHexByte(senddata));
//sendData(Encoding.UTF8.GetBytes(senddata));
}
void Update()
{
if (communicator != null && communicator.Com.IsOpen && communicator.Com.BytesToRead > 0)
{
if (isHex)
{
int count = communicator.Com.BytesToRead;
byte[] buffer = new byte[count];
communicator.Com.Read(buffer, 0, count);
OnRawDataReceived(buffer);
}
else
{
string data = communicator.Com.ReadLine();
Debug.Log("data: " + data);
if (data.Length > 30)
{
DistanceData.Enqueue(data.Split(' '));
}
}
}
}
private void OnRawDataReceived(byte[] bytes)
{
StringBuilder builder = new StringBuilder();
foreach (byte b in bytes)
{
builder.Append(b.ToString("X2") + " ");
}
//Debug.Log("收到串口数据:" + builder.ToString());
//if (builder.ToString().CompareTo("99 02 0D 0A ") == 0)
//{
// Debug.Log("对了");
//}
//else
//{
// Debug.Log("收到的数据有出入");
//}
}
public void sendData(byte[] d)
{
if (communicator == null)
{
Debug.Log("communicator 未初始化");
return;
}
if (d == null || d.Length == 0)
{
Debug.Log("发送数据为空");
return;
}
try
{
communicator.Com.Write(d, 0, d.Length);
}
catch (Exception e)
{
print("发送失败");
}
}
public string[] GetConfigData()
{
configData = new string[config.Length];
for (int i = 0; i < configData.Length; i++)
{
configData[i] = config[i].Substring(config[i].IndexOf(':') + 1);
}
return configData;
}
private static byte[] strToToHexByte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return returnBytes;
}
}
2.LeapMotion 代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Leap.Unity;
using Leap;
public class LeapGestureTool : MonoBehaviour
{
public static LeapGestureTool instance;
private Controller leapController;
public Camera leapCamera;
public bool isQin = false;
public float distance = 0;
private void Awake()
{
instance = this;
}
void Start()
{
// 创建LeapMotion控制器实例
leapController = new Controller();
}
void Update()
{
// 检查是否连接了Leap Motion控制器
if (!leapController.IsConnected)
{
Debug.Log("Leap Motion未连接");
return;
}
// 获取最新的帧数据
Frame frame = leapController.Frame();
// 获取第一个检测到的手部
if (frame.Hands.Count > 0)
{
isQin = true;
Hand hand = frame.Hands[0];
// 获取手的位置
Vector3 handPosition = hand.PalmPosition.ToVector3();
// 获取相机的位置
Vector3 cameraPosition = leapCamera.transform.position;
// 计算人手与相机之间的距离
distance = Vector3.Distance(handPosition, cameraPosition);
Debug.Log("人手与Leap Motion相机的距离为:" + distance);
}
else
{
isQin = false;
//Debug.Log("手不在检测范围");
}
}
}