Files
Hexapod_Simulation_Unity/Assets/Serial.cs
T

68 lines
1.4 KiB
C#
Raw Normal View History

2018-11-12 23:57:23 +01:00
using UnityEngine;
using System.Collections;
using System.IO.Ports;
public class Serial {
private static Serial instance;
private Serial() { }
~Serial() {
serialPort.Close();
}
public static Serial Instance
{
get
{
if (instance == null)
{
instance = new Serial();
}
return instance;
}
}
2018-11-13 23:09:58 +01:00
public bool enabled = false;
2018-11-12 23:57:23 +01:00
SerialPort serialPort;
public void send(int start, int a, int b, int c)
{
2018-11-15 16:37:54 +01:00
if (serialPort == null)
{
serialPort = new SerialPort(@"\\.\" + "COM10", 115200);
try
{
serialPort.Open();
serialPort.DiscardOutBuffer();
serialPort.DiscardInBuffer();
enabled = true;
}
catch
{
enabled = false;
}
}
2018-11-13 23:09:58 +01:00
if (enabled)
2018-11-12 23:57:23 +01:00
{
2018-11-13 23:09:58 +01:00
if (serialPort is SerialPort)
{
2018-11-15 16:37:54 +01:00
string send = " #" + start.ToString() + " P" + a.ToString();
2018-11-13 23:09:58 +01:00
start++;
2018-11-15 16:37:54 +01:00
send = send + " #" + start.ToString() + " P" + b.ToString();
2018-11-13 23:09:58 +01:00
start++;
2018-11-15 16:37:54 +01:00
send = send + " #" + start.ToString() + " P" + c.ToString();
2018-11-12 23:57:23 +01:00
2018-11-15 16:37:54 +01:00
send = send + " \r";
2018-11-12 23:57:23 +01:00
2018-11-15 16:37:54 +01:00
serialPort.Write(send);
2018-11-13 23:09:58 +01:00
}
2018-11-12 23:57:23 +01:00
}
}
}