ln.ethercat/ln.ethercat/controller/remote/StupidSerialRemote.cs

171 lines
6.0 KiB
C#

using System;
using System.IO.Ports;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using ln.logging;
using ln.type;
namespace ln.ethercat.controller.remote
{
/*
#define LED_ERROR 0x00
#define LED_RUN 0x01
#define LED_LOAD25 0x02
#define LED_LOAD50 0x03
#define LED_LOAD75 0x04
#define LED_LOAD100 0x05
#define LED_SERVICE 0x06
#define LED_AUX 0x07
*/
[Flags]
public enum StupidLEDs : int
{
NONE = 0,
ERROR = (1<<15),
RUN = (1<<1),
LOAD25 = (1<<2),
LOAD50 = (1<<3),
LOAD75 = (1<<4),
LOAD100 = (1<<15),
ALL = -1
}
public class StupidSerialRemote : ControllerRemote
{
public string SerialPortName { get; }
SerialPort serialPort;
bool stopReceiverThread;
Thread threadReceiver;
public StupidSerialRemote(Controller controller)
:this(controller, SerialPort.GetPortNames()[0]){}
public StupidSerialRemote(Controller controller, string serialDevice)
:base(controller)
{
SerialPortName = serialDevice;
CycleFrequency = 10.0;
}
protected override void Cycle()
{
byte cycleDisplayStep = (byte)(CycleCounter & 0x0F);
switch (Controller.ControllerState)
{
case ControllerStates.NOTREADY:
SetLEDs(((cycleDisplayStep & 0x07) < 0x04) ? StupidLEDs.ALL : StupidLEDs.NONE);
break;
case ControllerStates.FAULT:
SetLEDs(((cycleDisplayStep & 0x07) < 0x04) ? StupidLEDs.ERROR : StupidLEDs.NONE);
break;
case ControllerStates.READY:
SetLEDs((cycleDisplayStep < 0x08) ? StupidLEDs.RUN : StupidLEDs.NONE);
break;
case ControllerStates.OPERATIONAL:
StupidLEDs leds = StupidLEDs.RUN;
if (Controller.ECMaster.DriveControllers[0].ActualLoad >= 0.25)
leds |= StupidLEDs.LOAD25;
if (Controller.ECMaster.DriveControllers[0].ActualLoad >= 0.5)
leds |= StupidLEDs.LOAD50;
if (Controller.ECMaster.DriveControllers[0].ActualLoad >= 0.75)
leds |= StupidLEDs.LOAD75;
if (Controller.ECMaster.DriveControllers[0].ActualLoad >= 1)
leds |= StupidLEDs.LOAD100;
SetLEDs(leds);
break;
case ControllerStates.ENABLING:
break;
case ControllerStates.DISABLING:
break;
}
}
void Receiver()
{
while (!stopReceiverThread)
{
string rxLine = serialPort.ReadLine();
//Logging.Log(LogLevel.DEBUGDETAIL, rxLine);
if (rxLine.Length >= 6)
{
ushort av = ushort.Parse(rxLine.Substring(2,4), System.Globalization.NumberStyles.HexNumber);
switch (rxLine[0])
{
case 'A':
int drive = rxLine[1] - '0';
if (drive == 0) drive = 1;
else if (drive == 1) drive = 2;
else if (drive == 2) drive = 0;
double rel = (double)av / 65535;
if ((drive >= 0) && (drive < Controller.ECMaster.DriveControllers.Length))
Controller.RemoteUpdateTarget(drive, rel);
break;
case 'B':
switch (rxLine[1])
{
case 'P':
switch (av)
{
case 0:
if (Controller.ControllerState == ControllerStates.FAULT)
Controller.RemoteAction(CRActions.CLEARFAULT);
else if (Controller.ControllerState == ControllerStates.READY)
Controller.RemoteAction(CRActions.ENABLE);
break;
case 1:
Controller.RemoteAction(CRActions.DISABLE);
break;
case 3:
// ToDo: Feeder left
break;
case 4:
// ToDo: Feeder right
break;
case 14:
// ToDo: cycle drilling
break;
}
break;
}
break;
}
}
}
}
void SetLEDs(StupidLEDs leds)
{
serialPort.Write(string.Format("LS{0:X4}\r\n", (ushort)leds));
}
protected override void Initialize()
{
stopReceiverThread = false;
serialPort = new SerialPort(SerialPortName);
serialPort.BaudRate = 57600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Open();
if (!(threadReceiver?.IsAlive ?? false))
{
threadReceiver = new Thread(Receiver);
threadReceiver.Start();
}
}
protected override void Shutdown()
{
stopReceiverThread = true;
serialPort.Close();
}
}
}