123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- using MECF.Framework.Common.Net;
- using MECF.Framework.Simulator.Core.Driver;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace CyberX8_Simulator.Devices
- {
- public class WagoSocketSimulator : SocketDeviceSimulator
- {
- private const short WRITE_DO_STARTADDRESS = 0x0200;
- private const short WRITE_AO_STARTADDRESS = 0x0200;
-
- private Dictionary<string, int> DONameIndexDic = new Dictionary<string, int>
- {{"DO0",0 },{"c_LoaderA_LS_Vacuum",1} };
- private Dictionary<string, int> DINameIndexDic = new Dictionary<string, int>
- {{"r_DRIP_TRAY_FLUID_DETECTION",19 },{"DI1",1} };
- private Dictionary<string, int> AINameIndexDic = new Dictionary<string, int>
- {{"r_LoaderA_LS_Vacuum_anlg",0 },{"AI1",1} };
- private Dictionary<string, int> AONameIndexDic = new Dictionary<string, int>
- {{"AO0",0 },{"AO1",1} };
- private IByteTransform byteTransform = new BigEndianByteTransformBase();
- private byte[] DOBytes = new byte[100];
- private short[] AOShorts = new short[50];
- private byte[] DIBytes = new byte[100];
- private short[] AIShorts = new short[50];
-
-
-
- private object _writeDOLocker = new object();
-
-
-
- private object _writeAOLocker = new object();
- public WagoSocketSimulator(int port):base(port)
- {
- InitializeData();
- }
-
-
-
-
- private void InitializeData()
- {
- AIShorts[0] = 0x1388;
- }
- #region 公共方法
- public void UpdataDOBytes(string name,int value)
- {
- if (DONameIndexDic.ContainsKey(name))
- {
- if (DONameIndexDic[name] < DOBytes.Length)
- {
- DOBytes[DONameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
- }
- }
- }
- public void UpdataDIBytes(string name, int value)
- {
- if (DINameIndexDic.ContainsKey(name))
- {
- if (DINameIndexDic[name] < DIBytes.Length)
- {
- DIBytes[DINameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
- }
- }
- }
- public void UpdataAOShorts(string name, int value)
- {
- if (AONameIndexDic.ContainsKey(name))
- {
- string hexValue = value.ToString("X2");
- try
- {
- short result = Convert.ToInt16(hexValue, 16);
- if (AONameIndexDic[name] < AOShorts.Length)
- {
- AOShorts[AONameIndexDic[name]] = result;
- }
- }
- catch (FormatException)
- {
- }
- }
- }
- public void UpdataAIShorts(string name, int value)
- {
- if (AINameIndexDic.ContainsKey(name))
- {
- string hexValue = value.ToString("X2");
- try
- {
- short result = Convert.ToInt16(hexValue, 16);
- if(AINameIndexDic[name] < AIShorts.Length)
- {
- AIShorts[AINameIndexDic[name]] = result;
- }
- }
- catch (FormatException)
- {
-
- }
- }
- }
- #endregion
- #region 功能方法
-
-
-
-
-
- public byte ConvertByteArrayToHex(byte[] byteArray)
- {
- byte result = 0;
-
- int binaryValue = 0;
- for (int i = 0; i < 8; i++)
- {
- binaryValue |= (byteArray[i] << (7 - i));
- }
-
- int reversedValue = 0;
- for (int i = 0; i < 8; i++)
- {
- reversedValue |= ((binaryValue >> i) & 1) << (7 - i);
- }
-
- if (byte.TryParse(reversedValue.ToString("X2"), System.Globalization.NumberStyles.HexNumber, null, out result))
- {
- return result;
- }
- return 0;
- }
-
-
-
-
-
- private byte[] ConvertShortArrayToByteArray(short[] shortArray)
- {
- byte[] byteArray = new byte[shortArray.Length * 2];
- for (int i = 0; i < shortArray.Length; i++)
- {
- byte[] tempBytes = BitConverter.GetBytes(shortArray[i]);
- Array.Copy(tempBytes, 0, byteArray, i * 2, 2);
- }
- return byteArray;
- }
- #endregion
-
- protected override void ProcessUnsplitMessage(byte[] data)
- {
- byte command = data[7];
- if (command == 0x01)
- {
- short flag = byteTransform.TransInt16(data, 0);
- byte channel = data[6];
- short startAddress = byteTransform.TransInt16(data, 8);
- short bitCount = byteTransform.TransInt16(data, 10);
- byte byteCount = (byte)(bitCount / 8 + 1);
- byte[] bytes = new byte[byteCount];
- for(int i = 0; i < byteCount;i++)
- {
- byte[] tempbytes = new byte[8];
- Array.Copy(DOBytes,8 * i, tempbytes, 0, 8);
- bytes[i] = ConvertByteArrayToHex(tempbytes);
- }
- OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
- return;
- }
- else if(command == 0x03)
- {
- short flag = byteTransform.TransInt16(data, 0);
- byte channel = data[6];
- short startAddress = byteTransform.TransInt16(data, 8);
- short registerCount = byteTransform.TransInt16(data, 10);
- short[] shorts = new short[registerCount];
- Array.Copy(AOShorts, 0, shorts, 0, registerCount);
- byte[] bytes = new byte[registerCount * 2];
- bytes = ConvertShortArrayToByteArray(shorts);
- OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
- return;
- }
- else if (command == 0x02)
- {
- short flag = byteTransform.TransInt16(data, 0);
- byte channel = data[6];
- short startAddress = byteTransform.TransInt16(data, 8);
- short bitCount = byteTransform.TransInt16(data, 10);
- byte byteCount = (byte)(bitCount / 8 + 1);
- byte[] bytes = new byte[byteCount];
- for (int i = 0; i < byteCount; i++)
- {
- byte[] tempbytes = new byte[8];
- Array.Copy(DIBytes, 8 * i, tempbytes, 0, 8);
- bytes[i] = ConvertByteArrayToHex(tempbytes);
- }
- OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
- return;
- }
- else if (command == 0x04)
- {
- short flag = byteTransform.TransInt16(data, 0);
- byte channel = data[6];
- short startAddress = byteTransform.TransInt16(data, 8);
- short registerCount = byteTransform.TransInt16(data, 10);
- short[] shorts = new short[registerCount];
- Array.Copy(AIShorts, 0, shorts, 0, registerCount);
- byte[] bytes = new byte[registerCount * 2];
- bytes = ConvertShortArrayToByteArray(shorts);
- OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
- return;
- }
- else if (command == 0x05)
- {
- short startAddress = byteTransform.TransInt16(data, 8);
- if (startAddress > 0x03FF || startAddress < WRITE_DO_STARTADDRESS)
- {
- short flag = byteTransform.TransInt16(data, 0);
- byte channel = data[6];
- OnWriteMessage(CreateError(flag, channel, command, 0x02));
- return;
- }
- int position = startAddress - WRITE_DO_STARTADDRESS;
- bool status = data[10] == 0xFF ? true : false;
- lock (_writeDOLocker)
- {
- DOBytes[position] = status ? (byte)1 : (byte)0;
- }
- OnWriteMessage(data);
- return;
- }
- else if (command == 0x06)
- {
- short startAddress = byteTransform.TransInt16(data, 8);
- if(startAddress > 0x02FF || startAddress < WRITE_AO_STARTADDRESS)
- {
- short flag = byteTransform.TransInt16(data, 0);
- byte channel = data[6];
- OnWriteMessage(CreateError(flag, channel, command, 0x02));
- return;
- }
- int position = startAddress - WRITE_AO_STARTADDRESS;
- short value = byteTransform.TransInt16(data, 10);
- lock (_writeAOLocker)
- {
- AOShorts[position] = value;
- }
- OnWriteMessage(data);
- return;
- }
- else
- {
- short flag = byteTransform.TransInt16(data, 0);
- byte channel = data[6];
- OnWriteMessage(CreateError(flag, channel, command, 0x01));
- return;
- }
- }
-
-
-
-
-
-
-
-
-
- private byte[] CreateReadDigitalResponse(short flag, byte channel, byte command, byte byteCount, byte[] values)
- {
- byte[] bytes = new byte[7 + 2 + values.Length];
- Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
- bytes[2] = 0x00;
- bytes[3] = 0x00;
- short dataLength = (short)(3 + values.Length);
- Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
- bytes[6] = channel;
- bytes[7] = command;
- bytes[8] = byteCount;
- Array.Copy(values, 0, bytes, 9, values.Length);
- return bytes;
- }
-
-
-
-
-
-
-
-
-
- private byte[] CreateReadAnalogyResponse(short flag, byte channel, byte command, byte registerCount, byte[] values)
- {
- byte[] bytes = new byte[7 + 2 + 2 * registerCount];
- Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
- bytes[2] = 0x00;
- bytes[3] = 0x00;
- short dataLength = (short)(3 + 2 * registerCount);
- Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
- bytes[6] = channel;
- bytes[7] = command;
- bytes[8] = (byte)(2 * registerCount);
- Array.Copy(values, 0, bytes, 9, values.Length);
- return bytes;
- }
-
-
-
-
-
-
-
-
- private byte[] CreateError(short flag, byte channel, byte command, byte error)
- {
- byte[] bytes = new byte[9];
- Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
- bytes[2] = 0x00;
- bytes[3] = 0x00;
- bytes[4] = 0x00;
- bytes[5] = 0x03;
- bytes[6] = channel;
- bytes[7] = (byte)(command | 0x80);
- bytes[8] = error;
- return bytes;
- }
- }
- }
|