139 lines
2.2 KiB
C#
139 lines
2.2 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.IO.Ports;
|
|
|
|
namespace DMX2
|
|
{
|
|
public class DriverBoitierV1 : DriverDMX
|
|
{
|
|
// tampons Entrée/Sortie
|
|
byte[] inputbuffer = new byte[1];
|
|
byte[] outputbuffer = new byte[260];
|
|
|
|
//Thread de boucle
|
|
Thread loopthread=null;
|
|
volatile bool running=true;
|
|
|
|
UniversDMX patch=null;
|
|
|
|
string portname = "/dev/ttyUSB0";
|
|
SerialPort serial = null;
|
|
|
|
public DriverBoitierV1 ()
|
|
{
|
|
patch = Conduite.Courante.Patches[0];
|
|
Start();
|
|
outputbuffer[0]=27;
|
|
outputbuffer[1]=68;
|
|
outputbuffer[4]=255;
|
|
}
|
|
|
|
|
|
void Start ()
|
|
{
|
|
OpenPort();
|
|
if (loopthread == null) {
|
|
loopthread = new Thread(new ThreadStart(MainLoop));
|
|
loopthread.Start();
|
|
}
|
|
}
|
|
|
|
void OpenPort ()
|
|
{
|
|
if (serial != null) {
|
|
serial.Close();
|
|
}
|
|
serial = new SerialPort(portname, 460800,Parity.None,8,StopBits.One);
|
|
serial.DtrEnable = false;
|
|
serial.ReadTimeout = 15;
|
|
serial.WriteTimeout = 200;
|
|
serial.Open();
|
|
}
|
|
|
|
bool CheckPortStatus ()
|
|
{
|
|
if(serial.IsOpen) return true;
|
|
|
|
OpenPort();
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
void MainLoop ()
|
|
{
|
|
DateTime prochainEnvoi= DateTime.Now;
|
|
TimeSpan sleeptime;
|
|
while (running) {
|
|
lock(Conduite.Courante)
|
|
{
|
|
patch.CalculUnivers(outputbuffer,5,255);
|
|
}
|
|
|
|
if(!CheckPortStatus())
|
|
{
|
|
Thread.Sleep(1000);
|
|
continue;
|
|
}
|
|
|
|
sleeptime = prochainEnvoi - DateTime.Now;
|
|
if(sleeptime.TotalMilliseconds>1)
|
|
Thread.Sleep(sleeptime);
|
|
|
|
prochainEnvoi = DateTime.Now.AddMilliseconds(22);
|
|
Envoi();
|
|
Reception();
|
|
|
|
}
|
|
}
|
|
|
|
void Envoi ()
|
|
{
|
|
try {
|
|
|
|
if(!serial.IsOpen) return;
|
|
serial.Write(outputbuffer,0,outputbuffer.Length);
|
|
|
|
} catch (TimeoutException ex) {
|
|
serial.Close();
|
|
}
|
|
}
|
|
|
|
void Reception ()
|
|
{
|
|
try {
|
|
|
|
if(!serial.IsOpen) return;
|
|
|
|
serial.Read(inputbuffer,0,inputbuffer.Length);
|
|
Console.WriteLine(inputbuffer[0]);
|
|
|
|
} catch (TimeoutException ex) {
|
|
serial.Close();
|
|
}
|
|
}
|
|
|
|
|
|
#region implemented abstract members of DMX2.DriverDMX
|
|
public override Gtk.Window GetDialog ()
|
|
{
|
|
return null;
|
|
}
|
|
#endregion
|
|
|
|
public override void Dispose ()
|
|
{
|
|
disposed = true;
|
|
running = false;
|
|
loopthread.Join();
|
|
loopthread=null;
|
|
|
|
//TODO : Close Port
|
|
if(serial != null)
|
|
serial.Close();
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|