82 lines
3.7 KiB
C#
82 lines
3.7 KiB
C#
using Hermes;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using WeldingCommon.Data;
|
|
|
|
namespace WeldingCommon.Devices
|
|
{
|
|
public partial class PLCController
|
|
{
|
|
public (int err, bool data) ReadBit(eCommonBitInput address) => ReadBit((ushort)address, ePLCDevice.M);
|
|
public (int err, bool data) ReadOutBit(eCommonBitOutput address) => ReadBit((ushort)address, ePLCDevice.M, isInput: false);
|
|
public int WriteBit(eCommonBitOutput addrOffset, bool onOff, ePLCDevice device = ePLCDevice.M, bool isOutput = true) => base.WriteBit((int)addrOffset, onOff, ePLCDevice.M, isOutput);
|
|
public (int err, short data) ReadWord(eCommonWordInput address) => base.ReadWord((ushort)address, ePLCDevice.D);
|
|
public int WriteWord(eCommonWordOutput addrOffset, short data, ePLCDevice device = ePLCDevice.D, bool isOutput = true) => base.WriteWord((int)addrOffset, data, ePLCDevice.D, isOutput);
|
|
|
|
public (int err, int data) ReadDWord(eCommonWordInput address) => base.ReadDWord((ushort)address, ePLCDevice.D);
|
|
public int WriteDWord(eCommonWordOutput addrOffset, int data, ePLCDevice device = ePLCDevice.D, bool isOutput = true) => base.WriteDWord((int)addrOffset, data, ePLCDevice.D, isOutput);
|
|
|
|
public int UpdateReportDatas(List<PLCReportData> list)
|
|
{
|
|
foreach (var data in list) {
|
|
if (!data.IsUpdate)
|
|
continue;
|
|
|
|
if (data.IsDoubleWord) {
|
|
var readValue = ReadDWord(data.Address, ePLCDevice.D);
|
|
if (readValue.err != E1.OK){
|
|
Log.E("RMS Parameter Read Fail");
|
|
return E2.CANCELLED;
|
|
}
|
|
|
|
data.Value = readValue.data;
|
|
|
|
if (data.IsThrow && WriteDWord(data.ThrowAddress == 0 ? data.Address : data.ThrowAddress, (int)data.Value, ePLCDevice.D) != E1.OK) {
|
|
Log.E($"RMS ThrowValue Write Fail (DWord): {data.DataName}");
|
|
return E2.CANCELLED;
|
|
}
|
|
} else {
|
|
var readValue = ReadWord(data.Address, ePLCDevice.D);
|
|
if (readValue.err != E1.OK) {
|
|
Log.E("RMS Parameter Read Fail");
|
|
return E2.CANCELLED;
|
|
}
|
|
|
|
data.Value = readValue.data;
|
|
|
|
if (data.IsThrow && WriteWord(data.ThrowAddress == 0 ? data.Address : data.ThrowAddress, (short)data.Value, ePLCDevice.D) != E1.OK) {
|
|
Log.E($"RMS ThrowValue Write Fail (Word): {data.DataName}");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
}
|
|
Log.FuncI($"Update PLC Read Data (count : {list.Where(t => t.IsUpdate == true).ToList().Count})");
|
|
return E1.OK;
|
|
}
|
|
public int UploadReportDatas(List<PLCReportData> list)
|
|
{
|
|
foreach (var data in list) {
|
|
if (!data.IsUpdate)
|
|
continue;
|
|
|
|
if (data.IsDoubleWord) {
|
|
if (WriteDWord(data.Address, data.getValue(), ePLCDevice.D) != E1.OK) {
|
|
Log.E("Upload PLC Report Data Write Fail");
|
|
return E2.CANCELLED;
|
|
}
|
|
} else {
|
|
if (WriteWord(data.Address, (short)data.getValue(), ePLCDevice.D) != E1.OK) {
|
|
Log.E("Upload PLC Report Data Write Fail");
|
|
return E2.CANCELLED;
|
|
}
|
|
}
|
|
}
|
|
Log.FuncI($"Upload PLC Report Data (count : {list.Where(t => t.IsUpdate == true).ToList().Count})");
|
|
return E1.OK;
|
|
}
|
|
}
|
|
}
|