722 lines
19 KiB
C#
722 lines
19 KiB
C#
using Aurora.Api;
|
|
using Aurora.Api.Devices;
|
|
using AuroraMassFlowMeter.MFC;
|
|
using Hermes;
|
|
using System.IO.Ports;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
|
|
namespace AuroraMassFlowMeter
|
|
{
|
|
public interface IMassFlowMeter
|
|
{
|
|
int Connect(SerialConnConfig config);
|
|
int Disconnect();
|
|
bool IsConnected();
|
|
int Reconnect();
|
|
bool WriteSetPoint(double FlowData);
|
|
bool ReadSetPoint(ref double Point);
|
|
bool ReadFlow(ref double ReadFlow);
|
|
bool ReadReadingError(out double ReadReadingError);
|
|
bool WriteReadingError(double WriteReadingError);
|
|
bool ReadMFCStatus(out byte ReadMFCStatus);
|
|
bool WriteDeviceId(int DeviceID);
|
|
bool ReadDeviceId(out int DeviceID);
|
|
bool ReadOperationMode(out eOperationMode OperationMode);
|
|
bool WriteOperationMode(eOperationMode WriteOperationMode);
|
|
bool ReadModelName(out string ReadModelName);
|
|
bool ReadFirmwareRevision(out string ReadFirmwareRevision);
|
|
bool ReadHardwareRevision(out string ReadHardwareRevision);
|
|
bool ReadSerial(out string ReadSerial);
|
|
bool ReadBaudRate(out eBaudRate ReadBaudRate);
|
|
bool WriteBaudRate(eBaudRate WriteBaudRate);
|
|
bool ReadGasName(out string ReadGasName);
|
|
bool ReadFullScale(out double ReadFullScale);
|
|
bool ReadFlowUnit(out eFlowUnit ReadFlowUnit);
|
|
bool ReadIntegration(out double ReadIntegration);
|
|
bool ResetIntegration(out double ResetIntegration);
|
|
bool ReadIntegrationOn(out eIntegrationOn ReadIntegrationOn);
|
|
bool WriteIntegrationOn(eIntegrationOn WriteIntegrationOn);
|
|
bool WriteRampSetPoint(double Time, double SetPoint);
|
|
bool ZeroOffset();
|
|
void SetDeviceID(int NewDeviceID);
|
|
}
|
|
|
|
public partial class MassFlowMeter : Device
|
|
{
|
|
Logger Log = new Logger("MassFlowMeter");
|
|
|
|
public double MeasureResult { get; private set; }
|
|
|
|
private object Locker = new object();
|
|
|
|
public IMassFlowMeter objMassFlowMeter {
|
|
get {
|
|
lock (Locker) {
|
|
return _objMassFlowMeter;
|
|
}
|
|
}
|
|
}
|
|
private IMassFlowMeter _objMassFlowMeter;
|
|
|
|
|
|
protected bool _isSimulBySetting; // 시스템 세팅의 simul flag 저장. Activate 시, Config 의 시뮬 flag 와 조합하여 최종 결정
|
|
|
|
#region 생성자
|
|
public MassFlowMeter(eMassFlowMeter type, bool isSimul)
|
|
{
|
|
CommType = CommunicationType.Serial;
|
|
Type = DeviceType.PowerMeter;
|
|
_isSimulBySetting = isSimul;
|
|
|
|
switch (type) {
|
|
case eMassFlowMeter.MFC:
|
|
_objMassFlowMeter = new MassFlowMeterMKPrecision(DeviceId, isSimul);
|
|
break;
|
|
//case ePowerMeter.Gentec:
|
|
// _MassFlowMeter = new PowerMeterGT(deviceId, isSimul);
|
|
// break;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
protected override int DoActivate(IDeviceConfig config)
|
|
{
|
|
var l_config = config as MassFlowMeterConfig;
|
|
IsSimul = l_config.IsSimul || _isSimulBySetting;
|
|
var l_connConfig = l_config.ConnConfig as SerialConnConfig;
|
|
SetDevicedID(l_config.DeviceId);
|
|
|
|
var ret = IsSimul ? E1.OK : objMassFlowMeter.Connect(l_connConfig);
|
|
SetMonitoringService(Update, l_config.ScanIntervalMS);
|
|
StartMonitoringService();
|
|
//RunMonitor(l_config.ScanIntervalMS);
|
|
|
|
return ret;
|
|
}
|
|
|
|
protected override int DoDeactivate()
|
|
{
|
|
if (IsSimul == false){
|
|
StopMonitoringService();
|
|
return objMassFlowMeter.Disconnect();
|
|
}
|
|
else{
|
|
StopMonitoringService();
|
|
return E1.OK;
|
|
}
|
|
}
|
|
|
|
//public void RunMonitor(int scanIntervalMS)
|
|
//{
|
|
// Task.Run(() => {
|
|
// Thread.Sleep(1000);
|
|
// while (IsActivated && !IsCommError) {
|
|
// Update();
|
|
// Thread.Sleep(scanIntervalMS);
|
|
// }
|
|
// });
|
|
//}
|
|
|
|
public void Update()
|
|
{
|
|
if (!objMassFlowMeter.ReadSetPoint(ref _SetPercent))
|
|
{
|
|
#warning 데이터 모니터링을 할때, 데이터 버퍼에 있는 정보를 불러서 사용하도록 해야해서 살림. >> 한번만 데이터를 취득 못해도 Error로 알람을 발생해도 되나?
|
|
//IsCommError = true;
|
|
//return;
|
|
}
|
|
|
|
Thread.Sleep(500);
|
|
|
|
if (!objMassFlowMeter.ReadFlow(ref _CurrentFlow))
|
|
{
|
|
//IsCommError = true;
|
|
//return;
|
|
}
|
|
Thread.Sleep(100);
|
|
}
|
|
|
|
public void SetDevicedID(int NewDeviceID)
|
|
{
|
|
objMassFlowMeter.SetDeviceID(NewDeviceID);
|
|
}
|
|
|
|
public int WriteSetPoint(double WriteSetPoint)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.WriteSetPoint(WriteSetPoint)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
}
|
|
public int ReadSetPoint(out double ReadSetPoint)
|
|
{
|
|
ReadSetPoint = 0;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadSetPoint = 100.0;
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadSetPoint(ref ReadSetPoint)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadFlow(out double ReadFlow)
|
|
{
|
|
ReadFlow = 0;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadFlow(ref ReadFlow)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
public int ReadReadingError(out double ReadReadingError)
|
|
{
|
|
ReadReadingError = double.NaN;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadReadingError(out ReadReadingError)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
public int WriteReadingError(double WriteReadingError)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.WriteReadingError(WriteReadingError)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadMFCStatus(out byte ReadMFCStatus)
|
|
{
|
|
ReadMFCStatus = 0;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadMFCStatus(out ReadMFCStatus)) {
|
|
//0b00000001 = Power Over
|
|
//0b00000010 = Sensor Fail
|
|
//0b00000100 = Heater Power Low
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
|
|
}
|
|
public int WriteDeviceId(int DeviceID)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.WriteDeviceId(DeviceID)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadDeviceId(out int DeviceID)
|
|
{
|
|
DeviceID = 0;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadDeviceId(out DeviceID)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadOperationMode(out eOperationMode eOperationMode)
|
|
{
|
|
eOperationMode = eOperationMode.Analog;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadOperationMode(out eOperationMode)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
}
|
|
public int WriteOperationMode(eOperationMode WriteOperationMode)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.WriteOperationMode(WriteOperationMode)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadModelName(out string ReadModelName)
|
|
{
|
|
ReadModelName = "";
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadModelName(out ReadModelName)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadFirmwareRevision(out string ReadFirmwareRevision)
|
|
{
|
|
ReadFirmwareRevision = "";
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadFirmwareRevision = "V13.07";
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadFirmwareRevision(out ReadFirmwareRevision)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
|
|
}
|
|
public int ReadHardwareRevision(out string ReadHardwareRevision)
|
|
{
|
|
ReadHardwareRevision = "";
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadHardwareRevision = "V13.03";
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadFirmwareRevision(out ReadHardwareRevision)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
public int ReadSerial(out string ReadSerial)
|
|
{
|
|
ReadSerial = "";
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadSerial = "SERIAL";
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadSerial(out ReadSerial)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadBaudRate(out eBaudRate ReadBaudRate)
|
|
{
|
|
ReadBaudRate = eBaudRate.bps_9600;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadBaudRate(out ReadBaudRate)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int WriteBaudRate(eBaudRate WriteBaudRate)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.WriteBaudRate(WriteBaudRate)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
|
|
}
|
|
public int ReadGasName(out string ReadGasName)
|
|
{
|
|
ReadGasName = "";
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadGasName = "N2";
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadGasName(out ReadGasName)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadFullScale(out double ReadFullScale)
|
|
{
|
|
ReadFullScale = 0;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadFullScale = 100.0;
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadFullScale(out ReadFullScale)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadFlowUnit(out eFlowUnit ReadFlowUnit)
|
|
{
|
|
ReadFlowUnit = eFlowUnit.LPM;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadFlowUnit(out ReadFlowUnit)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadIntegration(out double ReadIntegration)
|
|
{
|
|
ReadIntegration = 0;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadIntegration = 16.33614;
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadIntegration(out ReadIntegration)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ResetIntegration(out double ResetIntegration)
|
|
{
|
|
ResetIntegration = 0;
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ResetIntegration = 0.000;
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ResetIntegration(out ResetIntegration)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
}
|
|
public int ReadIntegrationOn(out eIntegrationOn ReadIntegrationOn)
|
|
{
|
|
ReadIntegrationOn = eIntegrationOn.NOT_USE;
|
|
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
ReadIntegrationOn = eIntegrationOn.USE;
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ReadIntegrationOn(out ReadIntegrationOn)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
|
|
return E1.OK;
|
|
|
|
|
|
}
|
|
public int WriteIntegrationOn(eIntegrationOn WriteIntegrationOn)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.WriteIntegrationOn(WriteIntegrationOn)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
}
|
|
public int WriteRampSetPoint(double Time, double SetPoint)
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.WriteRampSetPoint(Time, SetPoint)) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
|
|
}
|
|
public int ZeroOffset()
|
|
{
|
|
if (!IsActivated) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
}
|
|
|
|
IsCommError = !objMassFlowMeter.IsConnected();
|
|
if (IsCommError) {
|
|
return E2.NOT_CONNECTED;
|
|
}
|
|
|
|
if (!objMassFlowMeter.ZeroOffset()) {
|
|
return E2.INTERNAL_ERROR;
|
|
}
|
|
return E1.OK;
|
|
|
|
}
|
|
}
|
|
}
|