1
0
mirror of https://github.com/xiaopeng12138/MaiDXR.git synced 2024-12-18 11:55:52 +01:00
MaiDXR/Assets/Scripts/IOs/Serial/SerialManager.cs

128 lines
3.4 KiB
C#
Raw Normal View History

2022-08-12 02:20:00 +02:00
using UnityEngine;
using System.IO.Ports;
using System;
using System.Collections;
2022-08-16 23:22:52 +02:00
using System.Threading;
2022-08-12 02:20:00 +02:00
public class SerialManager : MonoBehaviour
{
static SerialPort p1Serial = new SerialPort ("COM5", 9600);
static SerialPort p2Serial = new SerialPort ("COM6", 9600);
2022-08-16 23:22:52 +02:00
static byte[] settingPacket = new byte[6] {40, 0, 0, 0, 0, 41};
2022-08-12 02:20:00 +02:00
static byte[] touchData = new byte[9] {40, 0, 0, 0, 0, 0, 0, 0, 41};
static byte[] touchData2 = new byte[9] {40, 0, 0, 0, 0, 0, 0, 0, 41};
public static bool startUp = false; //use ture for default start up state to prevent restart game
2022-08-16 23:22:52 +02:00
static string recivData;
private Thread touchThread;
private Queue touchQueue;
2022-08-12 02:20:00 +02:00
void Start()
{
try
{
Debug.Log("Try start Serial");
p1Serial.Open();
p2Serial.Open();
}
catch (Exception ex)
{
Console.WriteLine($"Failed to Open Serial Ports: {ex}");
}
2022-08-16 23:22:52 +02:00
touchQueue = Queue.Synchronized(new Queue());
touchThread = new Thread(TouchThread);
InvokeRepeating("PingTouchThread", 0, 1);
TouchPanelManager.touchDidChange += PingTouchThread;
touchThread.Start();
2022-08-12 02:20:00 +02:00
Debug.Log("Serial Started");
}
void Update()
{
if (Input.GetKeyDown(KeyCode.T))
startUp = !startUp;
}
2022-08-16 23:22:52 +02:00
private void PingTouchThread()
{
touchQueue.Enqueue(1);
}
private void TouchThread()
{
while(true)
{
if(p1Serial.IsOpen)
ReadData(p1Serial);
if(p2Serial.IsOpen)
ReadData(p2Serial);
if(touchQueue.Count > 0)
{
touchQueue.Dequeue();
UpdateTouch();
}
}
}
2022-08-12 02:20:00 +02:00
private void OnDestroy()
{
2022-08-16 23:22:52 +02:00
touchThread.Abort();
2022-08-12 02:20:00 +02:00
p1Serial.Close();
p2Serial.Close();
}
2022-08-16 23:22:52 +02:00
private void ReadData(SerialPort Serial)
2022-08-12 02:20:00 +02:00
{
if (Serial.BytesToRead == 6)
{
recivData = Serial.ReadExisting();
TouchSetUp(Serial, recivData);
}
}
2022-08-16 23:22:52 +02:00
private void TouchSetUp(SerialPort Serial, string data)
2022-08-12 02:20:00 +02:00
{
switch (Convert.ToByte(data[3]))
{
case 76:
case 69:
startUp = false;
break;
case 114:
case 107:
for (int i=1; i<5; i++)
settingPacket[i] = Convert.ToByte(data[i]);
Serial.Write(settingPacket, 0, settingPacket.Length);
break;
case 65:
startUp = true;
break;
}
}
2022-08-16 23:22:52 +02:00
public static void SendTouch(SerialPort Serial, byte[] data)
2022-08-12 02:20:00 +02:00
{
if (startUp)
2022-08-16 23:22:52 +02:00
Serial.Write(data, 0, 9);
2022-08-12 02:20:00 +02:00
}
public static void UpdateTouch()
{
if (!startUp)
return;
2022-08-16 23:22:52 +02:00
SendTouch(p1Serial, touchData);
SendTouch(p2Serial, touchData2);
2022-08-12 02:20:00 +02:00
}
public static void ChangeTouch(bool isP1, int Area, bool State)
{
if (isP1)
ByteArrayExt.SetBit(touchData, Area+8, State);
else
ByteArrayExt.SetBit(touchData2, Area+8, State);
}
}
public static class ByteArrayExt
{
public static byte[] SetBit(this byte[] self, int index, bool value)
{
var bitArray = new BitArray(self);
bitArray.Set(index, value);
bitArray.CopyTo(self, 0);
return self;
}
}