398 lines
12 KiB
C#
398 lines
12 KiB
C#
using Aurora.Api;
|
|
using Aurora.Api.Devices;
|
|
using Hermes;
|
|
using System;
|
|
using System.Text;
|
|
|
|
|
|
namespace AuroraCAS
|
|
{
|
|
public class CAS : SerialPortDevice
|
|
{
|
|
public const byte BYTE_STX = 0x02; // 02
|
|
public const byte BYTE_ETX = 0x03; // 03
|
|
public const byte BYTE_ACK = 0x06; // 06
|
|
public const byte BYTE_NAK = 0x15; // 21
|
|
public const byte BYTE_PLUS_SIGN = 0x2B; // +
|
|
public const byte BYTE_MINUS_SIGN = 0x2D; // -
|
|
|
|
public const byte BYTE_0 = 0x30; // 48
|
|
public const byte BYTE_1 = 0x31; // 49
|
|
public const byte BYTE_5 = 0x35; // 53
|
|
|
|
|
|
public const byte BYTE_A = 0x41;
|
|
public const byte BYTE_C = 0x43;
|
|
public const byte BYTE_E = 0x45;
|
|
public const byte BYTE_H = 0x48;
|
|
public const byte BYTE_N = 0x4E;
|
|
public const byte BYTE_O = 0x4F;
|
|
public const byte BYTE_P = 0x50;
|
|
public const byte BYTE_S = 0x53;
|
|
public const byte BYTE_T = 0x54;
|
|
|
|
public const byte BYTE_CR = 0x0D;
|
|
public const byte BYTE_LF = 0x0A;
|
|
|
|
|
|
public const string CMD_SNO = "SNO";
|
|
public const string CMD_CNO = "CNO";
|
|
public const string CMD_PNO = "PNO";
|
|
public const string CMD_TAR = "TAR";
|
|
public const string CMD_CWT = "CWT";
|
|
public const string CMD_TRS = "TRS";
|
|
public const string CMD_ZER = "ZER";
|
|
public const string CMD_HOL = "HOL";
|
|
public const string CMD_HRS = "HRS";
|
|
|
|
public const string CMD_SP1 = "SP1";
|
|
public const string CMD_SP2 = "SP2";
|
|
public const string CMD_STR = "STR";
|
|
|
|
public const byte BYTE_REQ_READ = 0x52; // 82
|
|
public const byte BYTE_REQ_WRITE = 0x57; // 87
|
|
|
|
public const byte LENGTH_DEFAULT_MSG = 8;
|
|
public const byte LENGTH_WRITE_REQUEST_MSG = 14;
|
|
public const byte LENGTH_WRITE_RESPONSE_MSG = 9;
|
|
public const byte LENGTH_SAVE_REQUEST_MSG = 8;
|
|
|
|
public const byte LENGTH_RESPONSE_PNO_MSG = 10;
|
|
public const byte LENGTH_RESPONSE_DEFAULT_MSG = 14;
|
|
public const byte LENGTH_RESPONSE_CWT_MSG = 22;
|
|
|
|
public const byte LENGTH_SLAVE_ADDRESS = 2;
|
|
public const byte LENGTH_CMD = 3;
|
|
public const byte LENGTH_DATA_INTEGER = 5;
|
|
public const byte LENGTH_DATA_DECIMAL_FRACTION = 1;
|
|
public const byte LENGTH_DATA = 6;
|
|
public const byte LENGTH_DATA_CWT = 7;
|
|
|
|
public const byte LENGTH_READ_REGISTER_MSG = 17;
|
|
public const byte LENGTH_RESPONSE_REGISTER_MSG = 255;
|
|
|
|
protected bool _isSimulBySetting;
|
|
CASConfig _CASConfig;
|
|
|
|
public CAS(string deviceId, bool isSimul) : base(deviceId, isSimul)
|
|
{
|
|
DeviceId = deviceId;
|
|
CommType = CommunicationType.Serial; // 추후 필요 시 DoActivate 할때 IConnConfig 에서 처리하게끔 추가 필요.
|
|
Type = DeviceType.Etc;
|
|
_isSimulBySetting = isSimul;
|
|
}
|
|
|
|
protected override int DoActivate(IDeviceConfig config)
|
|
{
|
|
_CASConfig = config as CASConfig;
|
|
_isSimulBySetting = _CASConfig.IsSimul || _isSimulBySetting;
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
} else {
|
|
var result = Open(_CASConfig.ConnConfig as SerialConnConfig);
|
|
if (result != E1.OK) {
|
|
|
|
return result;
|
|
}
|
|
}
|
|
//SetSP1((int)_CASConfig.LowLimit *10);
|
|
//SetSP2((int)_CASConfig.HighLimit *10);
|
|
return E1.OK;
|
|
}
|
|
|
|
protected override int DoDeactivate()
|
|
{
|
|
if (IsSimul) {
|
|
return E1.OK;
|
|
} else {
|
|
return Close();
|
|
}
|
|
}
|
|
|
|
public double testCWT()
|
|
{
|
|
double cwt = 0.0f;
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_CWT_MSG];
|
|
responseMsg[0] = BYTE_STX;
|
|
responseMsg[1] = BYTE_0;
|
|
responseMsg[2] = BYTE_1;
|
|
responseMsg[3] = BYTE_REQ_READ;
|
|
responseMsg[4] = BYTE_C;
|
|
responseMsg[5] = BYTE_REQ_WRITE;
|
|
responseMsg[6] = BYTE_T;
|
|
responseMsg[7] = BYTE_S;
|
|
responseMsg[8] = BYTE_T;
|
|
responseMsg[9] = BYTE_N;
|
|
responseMsg[10] = BYTE_T;
|
|
responseMsg[11] = BYTE_MINUS_SIGN;
|
|
responseMsg[12] = BYTE_0;
|
|
responseMsg[13] = BYTE_0;
|
|
responseMsg[14] = BYTE_0;
|
|
responseMsg[15] = 0x32;
|
|
responseMsg[16] = 0x37;
|
|
responseMsg[17] = 0x2E;
|
|
responseMsg[18] = 0x36;
|
|
responseMsg[19] = 0x6B;
|
|
responseMsg[20] = 0x67;
|
|
responseMsg[21] = BYTE_ETX;
|
|
|
|
byte sign;
|
|
cwt = ExtractDoubleData(responseMsg, CMD_CWT, out sign);
|
|
if (sign == BYTE_PLUS_SIGN)
|
|
cwt = 1 * cwt;
|
|
else if (sign == BYTE_MINUS_SIGN)
|
|
cwt = -1 * cwt;
|
|
return cwt;
|
|
|
|
}
|
|
public double GetCurrentWeight()
|
|
{
|
|
double weight = 0.0;
|
|
if (GetCWT(out weight) == E1.OK) {
|
|
return weight;
|
|
} else return 0.0;
|
|
}
|
|
|
|
public int GetCWT(out double cwt)
|
|
{
|
|
if (IsSimul) {
|
|
cwt = 0.0;
|
|
return E1.OK;
|
|
}
|
|
|
|
return ReadDoubleData(CMD_CWT, out cwt);
|
|
}
|
|
|
|
public int GetLowLimit()
|
|
{
|
|
int lowLimit = 0;
|
|
if (GetSP1(out lowLimit) == E1.OK) {
|
|
return lowLimit;
|
|
} else return 0;
|
|
|
|
}
|
|
public int GetHighLimit()
|
|
{
|
|
int HighLimit = 0;
|
|
if (GetSP2(out HighLimit) == E1.OK) {
|
|
return HighLimit;
|
|
} else return 0;
|
|
|
|
}
|
|
public int GetSP1(out int Sp1)
|
|
{
|
|
if (IsSimul) {
|
|
Sp1 = 0;
|
|
return E1.OK;
|
|
}
|
|
|
|
return ReadIntData(CMD_SP1, out Sp1);
|
|
}
|
|
|
|
public int GetSP2(out int Sp2)
|
|
{
|
|
if (IsSimul) {
|
|
Sp2 = 10;
|
|
return E1.OK;
|
|
}
|
|
|
|
return ReadIntData(CMD_SP2, out Sp2);
|
|
}
|
|
|
|
public int SetSP1(int value)
|
|
{
|
|
string WriteData = ConvertDataIntToString(value);
|
|
if (!IsPortOpen()) {
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis) {
|
|
byte[] requestMsg = CreateWriteRequestMsg(CMD_SP1, WriteData);
|
|
|
|
if (Write(requestMsg)) {
|
|
byte[] responseMsg = new byte[LENGTH_WRITE_RESPONSE_MSG];
|
|
if (ReadMessage(responseMsg, BYTE_STX, BYTE_ETX) <= 0) {
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
}
|
|
}
|
|
int ResultValue = 0;
|
|
GetSP1(out ResultValue);
|
|
if (value == ResultValue)
|
|
return E1.OK;
|
|
else
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public int SetSP2(int value)
|
|
{
|
|
string WriteData = ConvertDataIntToString(value);
|
|
if (!IsPortOpen()) {
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis) {
|
|
byte[] requestMsg = CreateWriteRequestMsg(CMD_SP2, WriteData);
|
|
|
|
if (Write(requestMsg)) {
|
|
byte[] responseMsg = new byte[LENGTH_WRITE_RESPONSE_MSG];
|
|
if (ReadMessage(responseMsg, BYTE_STX, BYTE_ETX) <= 0) {
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
}
|
|
}
|
|
int ResultValue = 0;
|
|
GetSP2(out ResultValue);
|
|
if (value == ResultValue)
|
|
return E1.OK;
|
|
else
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public int GetSNO(out int SNO)
|
|
{
|
|
if (IsSimul) {
|
|
SNO = 0;
|
|
return E1.OK;
|
|
}
|
|
|
|
return ReadIntData(CMD_SNO, out SNO);
|
|
}
|
|
|
|
private int ReadDoubleData(string cmd, out double readData)
|
|
{
|
|
readData = 0;
|
|
|
|
if (!IsPortOpen()) {
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis) {
|
|
byte[] requestMsg = CreateReadRequestMsg(cmd);
|
|
if (Write(requestMsg)) {
|
|
byte[] responseMsg = new byte[LENGTH_RESPONSE_CWT_MSG];
|
|
|
|
if (ReadMessage(responseMsg, BYTE_STX, BYTE_ETX) <= 0) {
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
byte sign = 0x00;
|
|
readData = ExtractDoubleData(responseMsg, cmd, out sign);
|
|
if (sign == BYTE_MINUS_SIGN)
|
|
readData = (-1) * readData;
|
|
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private int ReadIntData(string cmd, out int readData)
|
|
{
|
|
readData = 0;
|
|
|
|
if (!IsPortOpen()) {
|
|
Log.E("Serial Port is NOT Open!");
|
|
return E1.NETWORK;
|
|
}
|
|
|
|
lock (LockThis) {
|
|
byte[] requestMsg = CreateReadRequestMsg(cmd);
|
|
if (Write(requestMsg)) {
|
|
|
|
byte[] responseMsg;
|
|
if (cmd == CMD_PNO)
|
|
responseMsg = new byte[LENGTH_RESPONSE_PNO_MSG];
|
|
else
|
|
responseMsg = new byte[LENGTH_RESPONSE_DEFAULT_MSG];
|
|
|
|
if (ReadMessage(responseMsg, BYTE_STX, BYTE_ETX) <= 0) {
|
|
Log.E("ReadError");
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
readData = ExtractIntData(responseMsg, cmd);
|
|
}
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private double ExtractDoubleData(byte[] msg, string cmd, out byte sign)
|
|
{
|
|
sign = msg[11];
|
|
string msgStr = Encoding.Default.GetString(msg);
|
|
string dataStr = msgStr.Substring(12, LENGTH_DATA_CWT);
|
|
|
|
return ConvertDataStringToDouble(dataStr);
|
|
}
|
|
private int ExtractIntData(byte[] msg, string cmd)
|
|
{
|
|
string msgStr = Encoding.Default.GetString(msg);
|
|
string dataStr = msgStr.Substring(7, LENGTH_DATA);
|
|
|
|
return ConvertDataStringToInt(dataStr);
|
|
}
|
|
|
|
private static int ConvertDataStringToInt(string valueStr)
|
|
{
|
|
return Convert.ToInt32(valueStr);
|
|
}
|
|
|
|
private static double ConvertDataStringToDouble(string valueStr)
|
|
{
|
|
string intStr = valueStr.Substring(0, LENGTH_DATA_INTEGER);
|
|
string dfStr = valueStr.Substring(0 + LENGTH_DATA_INTEGER + 1, LENGTH_DATA_DECIMAL_FRACTION);
|
|
|
|
return Convert.ToDouble(intStr) + (Convert.ToDouble(dfStr) * 0.1);
|
|
}
|
|
|
|
private byte[] CreateReadRequestMsg(string cmd)
|
|
{
|
|
byte[] requestMsg = new byte[LENGTH_DEFAULT_MSG];
|
|
byte[] cmdByte = Encoding.Default.GetBytes(cmd);
|
|
|
|
requestMsg[0] = BYTE_STX;
|
|
requestMsg[1] = BYTE_0;
|
|
requestMsg[2] = BYTE_1;
|
|
requestMsg[3] = BYTE_REQ_READ;
|
|
|
|
Array.Copy(cmdByte, 0, requestMsg, 4, LENGTH_CMD);
|
|
requestMsg[requestMsg.Length - 1] = BYTE_ETX;
|
|
|
|
return requestMsg;
|
|
}
|
|
|
|
|
|
private byte[] CreateWriteRequestMsg(string cmd, string writeData)
|
|
{
|
|
byte[] WriteMsg = new byte[LENGTH_WRITE_REQUEST_MSG];
|
|
byte[] cmdByte = Encoding.Default.GetBytes(cmd);
|
|
byte[] writeDataByte = Encoding.Default.GetBytes(writeData);
|
|
|
|
WriteMsg[0] = BYTE_STX;
|
|
WriteMsg[1] = BYTE_0;
|
|
WriteMsg[2] = BYTE_1;
|
|
WriteMsg[3] = BYTE_REQ_WRITE;
|
|
|
|
Array.Copy(cmdByte, 0, WriteMsg, 4, LENGTH_CMD);
|
|
|
|
Array.Copy(writeDataByte, 0, WriteMsg, 7, LENGTH_DATA);
|
|
WriteMsg[WriteMsg.Length - 1] = BYTE_ETX;
|
|
|
|
return WriteMsg;
|
|
|
|
}
|
|
|
|
private static string ConvertDataIntToString(int value)
|
|
{
|
|
return string.Format("{0:000000}", value);
|
|
}
|
|
}
|
|
}
|