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 MessageIn; public event Action 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 buffer = new List(); 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; } } }