Files
Hexapod_Simulation_Unity/Assets/Serial.cs
T

68 lines
1.4 KiB
C#

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;
}
}
public bool enabled = false;
SerialPort serialPort;
public void send(int start, int a, int b, int c)
{
if (serialPort == null)
{
serialPort = new SerialPort(@"\\.\" + "COM10", 115200);
try
{
serialPort.Open();
serialPort.DiscardOutBuffer();
serialPort.DiscardInBuffer();
enabled = true;
}
catch
{
enabled = false;
}
}
if (enabled)
{
if (serialPort is SerialPort)
{
string send = " #" + start.ToString() + " P" + a.ToString();
start++;
send = send + " #" + start.ToString() + " P" + b.ToString();
start++;
send = send + " #" + start.ToString() + " P" + c.ToString();
send = send + " \r";
serialPort.Write(send);
}
}
}
}