using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using arDev.Arduino;
using COMM;
using AR;
namespace arDev
{
public class FakePLC : arDev.Arduino.DIO
{
private System.Threading.Thread thPLC;
public bool Shutdown { get; set; } = false;
public FakePLC()
{
VAR.Init(128);
}
~FakePLC()
{
System.Threading.Tasks.Task.Run(() =>
{
//쓰레드가 살아잇다면 대기한다
if (thPLC != null && thPLC.IsAlive)
{
System.Threading.Thread.Sleep(100);
}
this.Close();
});
}
private object lockobjdata = new object();
public enum eEEPAddress
{
EEP_IOINTERVAL = 0,
EEP_RESETCOUNT,
EEP_DIREVH,
EEP_DIREVL,
EEP_OPTION,
EEP_UPTIME,
};
public enum eCommand : byte
{
LOAD = 0, //EEPROM 불러오기
SAVE, //EEPROM 저장
RESET, //초기화
PINGCHK,
SET_PINMODE, //PINMODE 설정
SET_DOUTPUT, //디지털출력설정(포트번호,값[1,0])
SET_AOUTPUT, //아날로그출력설정(포트GET_SETTING = 50, //셋팅값 요청
SET_FLAG,
SET_EEPROM,
SET_MANUALSPEED,
GET_SETTING = 50,
GUIDE_MOT = 90, //가이드커버(양쪽) 0=멈춤,1=UP,2=DN 아스키코드표 90=Z
SET_EEP_DIREV,
};
//public enum MotorType
//{
// Left = 0,
// Right
//}
//public enum Direction
//{
// CW = 0,
// CCW,
//}
//public enum RunType
//{
// Stop = 0,
// Up,
// Dn
//}
public enum DIName
{
PINI_EMG = 0,
PINI_BTN_1,
PINI_BTN_2,
PINI_BTN_3,
PINI_BTN_4,
PINI_OVERLOADL,
PINI_OVERLOADR,
PINI_EMPTY_07,
PINI_EMPTY_08,
PINI_BTN_ZUP,
PINI_BTN_ZDN,
PINI_LIMIT_LU,
PINI_LIMIT_LD,
PINI_LIMIT_RU,
PINI_LIMIT_RD,
PINI_STOP,
}
public enum DIPin
{
PINI_EMG = 30,
PINI_BTN_1,
PINI_BTN_2,
PINI_BTN_3,
PINI_BTN_4,
PINI_OVERLOADL,
PINI_OVERLOADR,
PINI_EMPTY_07,
PINI_EMPTY_08,
PINI_BTN_ZUP,
PINI_BTN_ZDN,
PINI_LIMIT_LU,
PINI_LIMIT_LD,
PINI_LIMIT_RU,
PINI_LIMIT_RD,
PINI_STOP,
}
public enum DOName
{
PINO_GUIDEMOTOR_LDIR,
PINO_GUIDEMOTOR_LRUN,
PINO_GUIDEMOTOR_RDIR,
PINO_GUIDEMOTOR_RRUN,
PINO_EMPTY_04,
PINO_EMPTY_05,
PINO_EMPTY_06,
PINO_EMPTY_07,
}
public enum DOPin
{
PINO_GUIDEMOTOR_LDIR = 22,
PINO_GUIDEMOTOR_LRUN,
PINO_GUIDEMOTOR_RDIR,
PINO_GUIDEMOTOR_RRUN,
PINO_EMPTY_04,
PINO_EMPTY_05,
PINO_EMPTY_06,
PINO_EMPTY_07,
}
public enum Rundirection : byte
{
Stop = 0,
Forward,
Backward,
Left,
Right,
EStop,
}
public enum ZMotDirection
{
Stop = 0,
Up,
Down
}
private bool Sendcommand(eCommand cmd, char p1, char p2)
{
return Sendcommand(cmd, (byte)p1, (byte)p2);
}
private bool Sendcommand(eCommand cmd, char p1, byte p2 = 0)
{
return Sendcommand(cmd, (byte)p1, p2);
}
private bool Sendcommand(eCommand cmd, byte p1 = 0, byte p2 = 0)
{
if (cmd != eCommand.PINGCHK)
{
if (cmd == eCommand.SET_FLAG)
{
//플래그값은 공용변수로 변경
var idx = (eVarBool)p1;
VAR.BOOL[idx] = p2 != 0;
RaiseMessage(MessageType.Send, string.Format("PLC CMD:{0} [{1}]=>{2}", cmd, (eVarBool)p1, p2));
}
else
RaiseMessage(MessageType.Send, string.Format("PLC CMD:{0}, P1:{1}, P2:{2}", cmd, p1, p2));
}
return Sendcommand((byte)cmd, p1, p2);
}
public Boolean GetValueI(DIName idx)
{
return GetValueI((byte)idx);
}
public Boolean GetValueO(DOName idx)
{
return GetValueO((byte)idx);
}
public Boolean GetFlag(int flag)
{
return VAR.BOOL[flag];
}
public Boolean GetFlag(COMM.eVarBool flag)
{
return VAR.BOOL[(int)flag];
}
public Boolean SetFlag(byte idx, Boolean value)
{
SetFlagTime[(int)idx] = DateTime.Now; //플래그기록을 시도한 시간
if (value == true)
return Sendcommand(eCommand.SET_FLAG, idx, 1);
else
return Sendcommand(eCommand.SET_FLAG, idx, 0);
}
//public Boolean SetOpt_AD4Invert(Boolean value)
//{
// var idx = (byte)PLCFlag.FLAG_ENABLE_AD4INVERT;
// SetFlagTime[idx] = DateTime.Now; //플래그기록을 시도한 시간
// if (value == true)
// return Sendcommand(eCommand.SET_FLAG, idx, 1);
// else
// return Sendcommand(eCommand.SET_FLAG, idx, 0);
//}
//public Boolean SetLog_SpeedControl(Boolean value)
//{
// var idx = (byte)PLCFlag.FLAG_ENABLE_LOG_SPEED;
// SetFlagTime[idx] = DateTime.Now; //플래그기록을 시도한 시간
// if (value == true)
// return Sendcommand(eCommand.SET_FLAG, idx, 1);
// else
// return Sendcommand(eCommand.SET_FLAG, idx, 0);
//}
public Boolean SetFlag(eVarBool flag, Boolean value)
{
VAR.BOOL[flag] = value;
return true;
}
public Boolean SetEEP(eEEPAddress address, byte value)
{
RaiseMessage(MessageType.Normal, string.Format("[M] Set EEP Rom Addr={0},Val={1}", address, value));
return Sendcommand(eCommand.SET_EEPROM, (byte)address, value);
}
public Boolean SetOutput(byte pinNO, Boolean Value)
{
if (Value == true) return Sendcommand(eCommand.SET_DOUTPUT, pinNO, 1);
else return Sendcommand(eCommand.SET_DOUTPUT, pinNO, 0);
}
public byte ApplySpeedH = 0;
public byte ApplySpeedL = 0;
public byte ApplySpeedC = 0;
public byte ApplySpeedZ = 0;
public DateTime SaveTime = DateTime.Parse("1982-11-23");
public Boolean SetPinReverse(byte High, byte Low)
{
RaiseMessage(MessageType.Normal,
string.Format("Pin Reverse H={0},L={1}", High, Low)
);
bool b1 = Sendcommand(eCommand.SET_EEPROM, (byte)eEEPAddress.EEP_DIREVH, High);
bool b2 = Sendcommand(eCommand.SET_EEPROM, (byte)eEEPAddress.EEP_DIREVL, Low);
return b1 && b2;
}
public Boolean SetMotorUpTime(byte value)
{
RaiseMessage(MessageType.Normal,
string.Format("Motor Up Time Value={0}", value)
);
bool b1 = Sendcommand(eCommand.SET_EEPROM, (byte)eEEPAddress.EEP_UPTIME, value);
return b1;
}
public Boolean SaveEEPROM()
{
RaiseMessage(MessageType.Normal,
string.Format("Save EEPROM")
);
bool b1 = Sendcommand(eCommand.SAVE, 0);
return b1;
}
public Boolean LoadEEPROM()
{
RaiseMessage(MessageType.Normal,
string.Format("Load EEPROM")
);
bool b1 = Sendcommand(eCommand.LOAD, 0);
return b1;
}
//public double ManualSpeed = 0.0;
//public bool SetManualSpeed(double Percvalue)
//{
// var Speed = (byte)((Percvalue / 100.0) * 255.0);
// ManualSpeed = Percvalue;
// // datamanager.COMCONTROLLER.Speak($"{Percvalue} 퍼센트 설정");
// RaiseMessage(MessageType.Normal,
// string.Format("Convert Speed Hold {0}%->{1}", Percvalue, Speed),
// false);
// return Sendcommand(eCommand.SET_MANUALSPEED, Speed);
//}
public DateTime LastPingTime = DateTime.Parse("1982-11-23");
public bool SendPing()
{
LastPingTime = DateTime.Now;
return Sendcommand(eCommand.PINGCHK, 0, 0);
}
public bool ZMot(ZMotDirection dir)
{
if (dir == ZMotDirection.Up)
return Sendcommand(eCommand.GUIDE_MOT, 'A', 'P'); //up
else if (dir == ZMotDirection.Down)
return Sendcommand(eCommand.GUIDE_MOT, 'A', 'N'); //down
else if (dir == ZMotDirection.Stop)
return Sendcommand(eCommand.GUIDE_MOT, 'A', 'S'); //stop
else return false;
}
public bool ZMot_Left(ZMotDirection dir)
{
if (dir == ZMotDirection.Up)
return Sendcommand(eCommand.GUIDE_MOT, 'L', 'P'); //up
else if (dir == ZMotDirection.Down)
return Sendcommand(eCommand.GUIDE_MOT, 'L', 'N'); //down
else if (dir == ZMotDirection.Stop)
return Sendcommand(eCommand.GUIDE_MOT, 'L', 'S'); //stop
else return false;
}
public bool ZMot_Right(ZMotDirection dir)
{
if (dir == ZMotDirection.Up)
return Sendcommand(eCommand.GUIDE_MOT, 'R', 'P'); //up
else if (dir == ZMotDirection.Down)
return Sendcommand(eCommand.GUIDE_MOT, 'R', 'N'); //down
else if (dir == ZMotDirection.Stop)
return Sendcommand(eCommand.GUIDE_MOT, 'R', 'S'); //stop
else return false;
}
///
/// 오버로드 또는 L이 활성화되되면 True를 반환함
///
///
public Boolean IsOverLoad()
{
if (GetValueI(DIName.PINI_OVERLOADL) || GetValueI(DIName.PINI_OVERLOADR)) return true;
else return false;
}
public Boolean IsLimitUp()
{
if (GetValueI(DIName.PINI_LIMIT_LU) || GetValueI(DIName.PINI_LIMIT_RU)) return true;
else return false;
}
public Boolean IsLimitDn()
{
if (GetValueI(DIName.PINI_LIMIT_LD) || GetValueI(DIName.PINI_LIMIT_RD)) return true;
else return false;
}
}
}