using UnityEngine;
using System.IO.Ports;
public class ControlServo : MonoBehaviour {
SerialPort sp;
float next_time;
//0 ile 180 arası gelen değer.
int servo_degree;
void Start () {
string the_com="";
next_time = Time.time;
foreach (string mysps in SerialPort.GetPortNames())
{
print(mysps);
if (mysps != "COM1") { the_com = mysps; break; }
}
sp = new SerialPort("\\\\.\\" + the_com, 9600);
if (!sp.IsOpen)
{
print(the_com + ", 9600 bps" + " bağlanılıyor");
sp.Open();
sp.ReadTimeout = 100;
sp.Handshake = Handshake.None;
if (sp.IsOpen) { print("Serial Port açık"); }
}
}
// Update is called once per frame
void Update() {
if (Time.time > next_time) {
if (!sp.IsOpen)
{
sp.Open();
print("Serial Port açık");
}
if (sp.IsOpen)
{
print("Gönderiliyor: " + servo_degree);
sp.Write(servo_degree.ToString());
}
next_time = Time.time + 0.5;
}
}
}