123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
- using System;
- using System.Text;
- using System.Net.Sockets;
- using System.Threading;
- using System.Net;
- using System.Collections.Generic;
- namespace EfemDualSimulator.Instances
- {
- public enum Mode { Read = 0, Write , Unknown }
- public enum GeneratorStatus
- {
- Unknown,
- OFF,
- ON,
- ERROR
- }
- public class CometRfData
- {
- public string sRec = "";
- public string sSend = "";
- public int TransactionNumber = 0;
- public int CommandNumber = 0;
- public int ArgumentNumber = 0;
- public int simForwardPower = 900;
- public int simReflectPower = 100 * 1000;
- public Mode mode = Mode.Unknown;
- public GeneratorStatus Status = GeneratorStatus.Unknown;
- }
- class CometRFSocketServer
- {
- //private bool isConnect = false;
- private byte Address = 0x0A;
- private byte ProtocolIdentifierHighByte = 0x00;
- private byte ProtocolIdentifierLowByte = 0x00;
- private byte _read = 0x41;
- private byte _write = 0x42;
- private byte _readInvalid = 0xC1;
- private byte _writeInvalid = 0xC2;
- //private byte[] sendData;
- private byte[] sendMessage;
- public int simCTune = 205;
- public int simCLoad = 795;
- public int simVpp = 5;
- public string sRec = "";
- public string sSend = "";
- //string sendstr = "Server OK";
- int bytesLength = 256;
- private TcpListener tcpListener;
- private Thread listenThread;
- public event Action<string> MessageIn;
- public event Action<string> MessageOut;
- public CometRFSocketServer(int port)
- {
- this.tcpListener = new TcpListener(IPAddress.Any, port);
- this.listenThread = new Thread(new ThreadStart(ListenForClients));
- this.listenThread.Start();
- Console.WriteLine("Server started at {0} :{1} @ {2}", IPAddress.Any, port, DateTime.Now.ToString());
- }
- private void ListenForClients()
- {
- this.tcpListener.Start();
- while (true)
- {
- //blocks until a client has connected to the server
- TcpClient client = this.tcpListener.AcceptTcpClient();
- //create a thread to handle communication
- //with connected client
- Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
- clientThread.Start(client);
- }
- }
- private void HandleClientComm(object client)
- {
- CometRfData cometRfData = new CometRfData();
- TcpClient tcpClient = (TcpClient)client;
- Console.WriteLine("Client @[{0}] connected @{1}", tcpClient.Client.LocalEndPoint, DateTime.Now.ToString());
- NetworkStream clientStream = tcpClient.GetStream();
- //isConnect = true;
- byte[] message = new byte[bytesLength];
- int bytesRead = 0;
- //bool isRight=false;
- while (true)
- {
- bytesRead = 0;
- try
- {
- //blocks until a client sends a message
- bytesRead = clientStream.Read(message, 0, bytesLength);
- }
- catch
- {
- //a socket error has occured
- Console.WriteLine("Error:receive msg error");
- break;
- }
- if (bytesRead == 0)
- {
- //the client has disconnected from the server
- Console.WriteLine("Client @[{0}] disconnect @{1}", tcpClient.Client.LocalEndPoint, DateTime.Now.ToString());
- break;
- }
- //message has successfully been received
- ASCIIEncoding encoder = new ASCIIEncoding();
- //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
- sRec = BitConverter.ToString(message, 0, 6 + BitConverter.ToInt32(new byte[] { message[5], 0x00, 0x00, 0x00 }, 0));
- //string recvstr = encoder.GetString(message, 0, bytesRead);
- //Console.WriteLine("Recv:[{1}]:msg:@[{0}] @{2}", sRec, tcpClient.Client.RemoteEndPoint, DateTime.Now.ToString());
- if (MessageIn != null)
- {
- MessageIn(sRec);
- }
- ParaRecBytes(message, ref cometRfData);
- //send msg to client
- //byte[] buffer = encoder.GetBytes(sendstr);
- sendMessage = BuildMessage(cometRfData.TransactionNumber, cometRfData.mode, cometRfData.CommandNumber, cometRfData.ArgumentNumber);
- clientStream.Write(sendMessage, 0, sendMessage.Length);
- clientStream.Flush();
- if (MessageOut != null)
- MessageOut(BitConverter.ToString(sendMessage, 0, sendMessage.Length));
- sSend = BitConverter.ToString(message, 0, 6 + BitConverter.ToInt32(new byte[] { message[5], 0x00, 0x00, 0x00 }, 0));
- //Console.WriteLine("Sent:[{1}] To:[{2}]:msg:@[{0}] @{3}\r\n", sSend, tcpClient.Client.LocalEndPoint, tcpClient.Client.RemoteEndPoint, DateTime.Now.ToString());
- }
- tcpClient.Close();
- }
- public void ParaRecBytes(byte[] rawMessage, ref CometRfData cometRfData)
- {
- try
- {
- cometRfData.TransactionNumber = BitConverter.ToInt32(new byte[] { rawMessage[1], rawMessage[0], 0x00, 0x00 }, 0);
- rawMessage[6] = Address;
- if (rawMessage[7] == _read)
- {
- cometRfData.mode = Mode.Read;
- cometRfData.CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0);
- GenerateSendData(cometRfData.CommandNumber, 0, ref cometRfData.Status, ref cometRfData.ArgumentNumber, ref cometRfData.simForwardPower, ref cometRfData.simReflectPower);
- }
- else if (rawMessage[7] == _write)
- {
- cometRfData.mode = Mode.Write;
- cometRfData.CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0);
- int DataValue = BytesToInt(new byte[] { rawMessage[10], rawMessage[11], rawMessage[12], rawMessage[13] }, 0);
- GenerateSendData(cometRfData.CommandNumber, DataValue, ref cometRfData.Status, ref cometRfData.ArgumentNumber, ref cometRfData.simForwardPower, ref cometRfData.simReflectPower);
- }
- else if (rawMessage[7] == _readInvalid)
- {
- int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
- }
- else if (rawMessage[7] == _writeInvalid)
- {
- int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
- }
- else
- {
- //LOG.Error($"RF invalid function code");
- }
- }
- catch (System.Exception )
- {
- //LOG.Write("RF failed:" + ex.ToString());
- }
- }
- private void GenerateSendData(int cometCommand, int value, ref GeneratorStatus Status,ref int ArgumentNumber, ref int simForwardPower, ref int simReflectPower, string sValue = "")
- {
- switch (cometCommand)
- {
- case 8000:
- if(Status == GeneratorStatus.OFF)
- {
- //sendData = new byte[] { 0x01, 0x00, 0x00, 0x00 };
- ArgumentNumber = 1;
- }
- else if (Status == GeneratorStatus.ON)
- {
- //sendData = new byte[] { 0x02, 0x00, 0x00, 0x00 };
- ArgumentNumber = 2;
- }
- else if (Status == GeneratorStatus.ERROR)
- {
- //sendData = new byte[] { 0x03, 0x00, 0x00, 0x00 };
- ArgumentNumber = 3;
- }
- else
- {
- //sendData = new byte[] { 0x00, 0x00, 0x00, 0x00 };
- ArgumentNumber = 0;
- }
- break;
- case 8021:
- //LOG.Info($"{Module} [{Display} Read] RF Forward power is {value / 1000} W");
- if (Status == GeneratorStatus.ON)
- {
- //sendData = IntToBytes(ForwardPower);
- ArgumentNumber = simForwardPower;
- simReflectPower = 100 * 1000;
- }
- else
- {
- //sendData = IntToBytes(ForwardPower);
- simForwardPower = 0;
- simReflectPower = 0;
- ArgumentNumber = simForwardPower;
- }
- break;
- case 8022:
- //LOG.Info($"{Module} [{Display} Read] RF Reflected power is {value / 1000} W");
- //sendData = IntToBytes(ForwardPower);
- ArgumentNumber = simReflectPower;
- break;
- case 8100:
- //LOG.Info($"{Module} [{Display} Read] RF Number of Errors is {value}");
- break;
- case 8101:
- //LOG.Info($"{Module} [{Display} Read] RF Error 1 is {sValue}");
- break;
- case 8102:
- //LOG.Info($"{Module} [{Display} Read] RF Error 1 state is {value}");
- break;
- case 8103:
- //LOG.Info($"{Module} [{Display} Read] RF Error 2 is {sValue}");
- break;
- case 8104:
- //LOG.Info($"{Module} [{Display} Read] RF Error 2 state is {value}");
- break;
- case 8105:
- //LOG.Info($"{Module} [{Display} Read] RF Error 3 is {sValue}");
- break;
- case 8106:
- //LOG.Info($"{Module} [{Display} Read] RF Error 3 state is {value}");
- break;
- case 8107:
- //LOG.Info($"{Module} [{Display} Read] RF Error 4 is {sValue}");
- break;
- case 8108:
- //LOG.Info($"{Module} [{Display} Read] RF Error 4 state is {value}");
- break;
- case 8109:
- //LOG.Info($"{Module} [{Display} Read] RF Error 5 is {sValue}");
- break;
- case 8110:
- //LOG.Info($"{Module} [{Display} Read] RF Error 5 state is {value}");
- break;
- case 8111:
- //LOG.Info($"{Module} [{Display} Read] RF Error 6 is {sValue}");
- break;
- case 8112:
- //LOG.Info($"{Module} [{Display} Read] RF Error 6 state is {value}");
- break;
- case 8113:
- //LOG.Info($"{Module} [{Display} Read] RF Error 7 is {sValue}");
- break;
- case 8114:
- //LOG.Info($"{Module} [{Display} Read] RF Error 7 state is {value}");
- break;
- case 8115:
- //LOG.Info($"{Module} [{Display} Read] RF Error 8 is {sValue}");
- break;
- case 8116:
- //LOG.Info($"{Module} [{Display} Read] RF Error 8 state is {value}");
- break;
- case 8150:
- //LOG.Info($"{Module} [{Display} Read] RF Number of warnings is {value}");
- break;
- case 8151:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 1 is {sValue}");
- break;
- case 8152:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 2 is {sValue}");
- break;
- case 8153:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 3 is {sValue}");
- break;
- case 8154:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 4 is {sValue}");
- break;
- case 8155:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 5 is {sValue}");
- break;
- case 8156:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 6 is {sValue}");
- break;
- case 8157:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 7 is {sValue}");
- break;
- case 8158:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 8 is {sValue}");
- break;
- case 8159:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 9 is {sValue}");
- break;
- case 8160:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 10 is {sValue}");
- break;
- case 8161:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 11 is {sValue}");
- break;
- case 8162:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 12 is {sValue}");
- break;
- case 8163:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 13 is {sValue}");
- break;
- case 8164:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 14 is {sValue}");
- break;
- case 8165:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 15 is {sValue}");
- break;
- case 8166:
- //LOG.Info($"{Module} [{Display} Read] RF Warning 16 is {sValue}");
- break;
- case 9201:
- ArgumentNumber = 1;
- break;
- case 9202:
- switch (value)
- {
- case 0:
- //LOG.Info($"{Module} [BiasMatch Read] holding position");
- break;
- case 1:
- //LOG.Info($"{Module} [BiasMatch Read] moving");
- break;
- default:
- break;
- }
- break;
- case 9203:
- //LOG.Info($"{Module} [BiasMatch Read] C load position is {value} %");
- ArgumentNumber = simCLoad;
- break;
- case 9204:
- //LOG.Info($"{Module} [BiasMatch Read] C tune position is {value} %");
- ArgumentNumber = simCTune;
- break;
- case 9205:
- //LOG.Info($"{Module} [BiasMatch Read] C load is {value} pF");
- break;
- case 9206:
- //LOG.Info($"{Module} [BiasMatch Read] C tune is {value} pF");
- break;
- case 9210:
- //LOG.Info($"{Module} [BiasMatch Read] C load ref pos is {value} %");
- break;
- case 9212:
- //LOG.Info($"{Module} [BiasMatch Read] C tune ref pos {value} %");
- break;
- case 9251:
- //LOG.Info($"{Module} [BiasMatch Read] Process control Matching {value} V");
- ArgumentNumber = simVpp;
- break;
- case 1001:
- switch (value)
- {
- case 0:
- //LOG.Info($"{Module} [{Display} Write] Switch off RF");
- Status = GeneratorStatus.OFF;
- ArgumentNumber = value;
- break;
- case 1:
- Status = GeneratorStatus.ON;
- ArgumentNumber = value;
- break;
- case 9:
- Status = GeneratorStatus.Unknown;
- ArgumentNumber = value;
- break;
- default:
- break;
- }
- break;
- case 1201:
- switch (value)
- {
- case 0:
- //LOG.Info($"{Module} [{Display} Write] RF Control mode is Forward power");
- ArgumentNumber = value;
- break;
- case 1:
- //LOG.Info($"{Module} [{Display} Write] RF Control mode is Load power");
- ArgumentNumber = value;
- break;
- case 2:
- //LOG.Info($"{Module} [{Display} Write] RF Control mode is Process control");
- ArgumentNumber = value;
- break;
- default:
- break;
- }
- break;
- case 1206:
- //LOG.Info($"{Module} [{Display} Write] RF Power Set point is {value / 1000} W");
- simForwardPower = value;
- ArgumentNumber = simForwardPower;
- break;
- case 8201:
- switch (value)
- {
- case 1:
- //LOG.Info($"{Module} [BiasMatch Write] Manual matching mode");
- break;
- case 2:
- //LOG.Info($"{Module} [BiasMatch Write] Automatic matching mode");
- break;
- default:
- break;
- }
- break;
- case 8202:
- switch (value)
- {
- case 0:
- //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic hold mode");
- ArgumentNumber = value;
- break;
- case 1:
- //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic preset mode");
- ArgumentNumber = value;
- break;
- default:
- break;
- }
- break;
- case 8203:
- //LOG.Info($"{Module} [{Display} Write] Set C load ref position is {value / 10} %");
- ArgumentNumber = value;
- simCLoad = value;
- break;
- case 8204:
- //LOG.Info($"{Module} [{Display} Write] Set C tune ref position is {value / 10} %");
- ArgumentNumber = value;
- simCTune = value;
- break;
- default:
- break;
- }
- }
- protected byte[] BuildMessage(int transactionNumber, Mode mode, int commandNumber, int argumentNumber)
- {
- List<byte> buffer = new List<byte>();
- buffer.Add(BitConverter.GetBytes(transactionNumber)[1]);
- buffer.Add(BitConverter.GetBytes(transactionNumber)[0]);
- buffer.Add(ProtocolIdentifierHighByte);
- buffer.Add(ProtocolIdentifierLowByte);
- if (mode == Mode.Read)
- {
- buffer.Add(0x00);
- buffer.Add(0x07);
- }
- else if (mode == Mode.Write)
- {
- buffer.Add(0x00);
- buffer.Add(0x08);
- }
- buffer.Add(Address);
- if (mode == Mode.Read)
- {
- buffer.Add(_read);
- }
- else if (mode == Mode.Write)
- {
- buffer.Add(_write);
- }
- if (mode == Mode.Read)
- {
- buffer.Add(0x04);
- //buffer.Add(sendData[0]);
- //buffer.Add(sendData[1]);
- //buffer.Add(sendData[2]);
- //buffer.Add(sendData[3]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[3]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[2]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[1]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[0]);
- }
- else if (mode == Mode.Write)
- {
- buffer.Add(BitConverter.GetBytes(commandNumber)[1]);
- buffer.Add(BitConverter.GetBytes(commandNumber)[0]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[3]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[2]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[1]);
- buffer.Add(BitConverter.GetBytes(argumentNumber)[0]);
- }
- //byte checkSum = 0;
- //for (int i = 0; i < buffer.Count; i++)
- //{
- // checkSum += buffer[i];
- //}
- //buffer.Add(checkSum);
- return buffer.ToArray();
- }
- public int BytesToInt(byte[] src, int offset)
- {
- int value;
- value = (int)((src[offset + 3] & 0xFF)
- | ((src[offset + 2] & 0xFF) << 8)
- | ((src[offset + 1] & 0xFF) << 16)
- | ((src[offset] & 0xFF) << 24));
- return value;
- }
- public byte[] IntToBytes(int value)
- {
- byte[] src = new byte[4];
- src[0] = (byte)((value >> 24) & 0xFF);
- src[1] = (byte)((value >> 16) & 0xFF);
- src[2] = (byte)((value >> 8) & 0xFF);
- src[3] = (byte)(value & 0xFF);
- return src;
- }
- }
- }
|