CometRFSocketServer.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using System;
  2. using System.Text;
  3. using System.Net.Sockets;
  4. using System.Threading;
  5. using System.Net;
  6. using System.Collections.Generic;
  7. namespace EfemDualSimulator.Instances
  8. {
  9. public enum Mode { Read = 0, Write , Unknown }
  10. public enum GeneratorStatus
  11. {
  12. Unknown,
  13. OFF,
  14. ON,
  15. ERROR
  16. }
  17. public class CometRfData
  18. {
  19. public string sRec = "";
  20. public string sSend = "";
  21. public int TransactionNumber = 0;
  22. public int CommandNumber = 0;
  23. public int ArgumentNumber = 0;
  24. public int simForwardPower = 900;
  25. public int simReflectPower = 100 * 1000;
  26. public Mode mode = Mode.Unknown;
  27. public GeneratorStatus Status = GeneratorStatus.Unknown;
  28. }
  29. class CometRFSocketServer
  30. {
  31. //private bool isConnect = false;
  32. private byte Address = 0x0A;
  33. private byte ProtocolIdentifierHighByte = 0x00;
  34. private byte ProtocolIdentifierLowByte = 0x00;
  35. private byte _read = 0x41;
  36. private byte _write = 0x42;
  37. private byte _readInvalid = 0xC1;
  38. private byte _writeInvalid = 0xC2;
  39. //private byte[] sendData;
  40. private byte[] sendMessage;
  41. public int simCTune = 205;
  42. public int simCLoad = 795;
  43. public int simVpp = 5;
  44. public string sRec = "";
  45. public string sSend = "";
  46. //string sendstr = "Server OK";
  47. int bytesLength = 256;
  48. private TcpListener tcpListener;
  49. private Thread listenThread;
  50. public event Action<string> MessageIn;
  51. public event Action<string> MessageOut;
  52. public CometRFSocketServer(int port)
  53. {
  54. this.tcpListener = new TcpListener(IPAddress.Any, port);
  55. this.listenThread = new Thread(new ThreadStart(ListenForClients));
  56. this.listenThread.Start();
  57. Console.WriteLine("Server started at {0} :{1} @ {2}", IPAddress.Any, port, DateTime.Now.ToString());
  58. }
  59. private void ListenForClients()
  60. {
  61. this.tcpListener.Start();
  62. while (true)
  63. {
  64. //blocks until a client has connected to the server
  65. TcpClient client = this.tcpListener.AcceptTcpClient();
  66. //create a thread to handle communication
  67. //with connected client
  68. Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
  69. clientThread.Start(client);
  70. }
  71. }
  72. private void HandleClientComm(object client)
  73. {
  74. CometRfData cometRfData = new CometRfData();
  75. TcpClient tcpClient = (TcpClient)client;
  76. Console.WriteLine("Client @[{0}] connected @{1}", tcpClient.Client.LocalEndPoint, DateTime.Now.ToString());
  77. NetworkStream clientStream = tcpClient.GetStream();
  78. //isConnect = true;
  79. byte[] message = new byte[bytesLength];
  80. int bytesRead = 0;
  81. //bool isRight=false;
  82. while (true)
  83. {
  84. bytesRead = 0;
  85. try
  86. {
  87. //blocks until a client sends a message
  88. bytesRead = clientStream.Read(message, 0, bytesLength);
  89. }
  90. catch
  91. {
  92. //a socket error has occured
  93. Console.WriteLine("Error:receive msg error");
  94. break;
  95. }
  96. if (bytesRead == 0)
  97. {
  98. //the client has disconnected from the server
  99. Console.WriteLine("Client @[{0}] disconnect @{1}", tcpClient.Client.LocalEndPoint, DateTime.Now.ToString());
  100. break;
  101. }
  102. //message has successfully been received
  103. ASCIIEncoding encoder = new ASCIIEncoding();
  104. //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
  105. sRec = BitConverter.ToString(message, 0, 6 + BitConverter.ToInt32(new byte[] { message[5], 0x00, 0x00, 0x00 }, 0));
  106. //string recvstr = encoder.GetString(message, 0, bytesRead);
  107. //Console.WriteLine("Recv:[{1}]:msg:@[{0}] @{2}", sRec, tcpClient.Client.RemoteEndPoint, DateTime.Now.ToString());
  108. if (MessageIn != null)
  109. {
  110. MessageIn(sRec);
  111. }
  112. ParaRecBytes(message, ref cometRfData);
  113. //send msg to client
  114. //byte[] buffer = encoder.GetBytes(sendstr);
  115. sendMessage = BuildMessage(cometRfData.TransactionNumber, cometRfData.mode, cometRfData.CommandNumber, cometRfData.ArgumentNumber);
  116. clientStream.Write(sendMessage, 0, sendMessage.Length);
  117. clientStream.Flush();
  118. if (MessageOut != null)
  119. MessageOut(BitConverter.ToString(sendMessage, 0, sendMessage.Length));
  120. sSend = BitConverter.ToString(message, 0, 6 + BitConverter.ToInt32(new byte[] { message[5], 0x00, 0x00, 0x00 }, 0));
  121. //Console.WriteLine("Sent:[{1}] To:[{2}]:msg:@[{0}] @{3}\r\n", sSend, tcpClient.Client.LocalEndPoint, tcpClient.Client.RemoteEndPoint, DateTime.Now.ToString());
  122. }
  123. tcpClient.Close();
  124. }
  125. public void ParaRecBytes(byte[] rawMessage, ref CometRfData cometRfData)
  126. {
  127. try
  128. {
  129. cometRfData.TransactionNumber = BitConverter.ToInt32(new byte[] { rawMessage[1], rawMessage[0], 0x00, 0x00 }, 0);
  130. rawMessage[6] = Address;
  131. if (rawMessage[7] == _read)
  132. {
  133. cometRfData.mode = Mode.Read;
  134. cometRfData.CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0);
  135. GenerateSendData(cometRfData.CommandNumber, 0, ref cometRfData.Status, ref cometRfData.ArgumentNumber, ref cometRfData.simForwardPower, ref cometRfData.simReflectPower);
  136. }
  137. else if (rawMessage[7] == _write)
  138. {
  139. cometRfData.mode = Mode.Write;
  140. cometRfData.CommandNumber = BitConverter.ToInt32(new byte[] { rawMessage[9], rawMessage[8], 0x00, 0x00 }, 0);
  141. int DataValue = BytesToInt(new byte[] { rawMessage[10], rawMessage[11], rawMessage[12], rawMessage[13] }, 0);
  142. GenerateSendData(cometRfData.CommandNumber, DataValue, ref cometRfData.Status, ref cometRfData.ArgumentNumber, ref cometRfData.simForwardPower, ref cometRfData.simReflectPower);
  143. }
  144. else if (rawMessage[7] == _readInvalid)
  145. {
  146. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  147. }
  148. else if (rawMessage[7] == _writeInvalid)
  149. {
  150. int DataValue = BitConverter.ToInt32(new byte[] { rawMessage[8], 0x00, 0x00, 0x00 }, 0);
  151. }
  152. else
  153. {
  154. //LOG.Error($"RF invalid function code");
  155. }
  156. }
  157. catch (System.Exception )
  158. {
  159. //LOG.Write("RF failed:" + ex.ToString());
  160. }
  161. }
  162. private void GenerateSendData(int cometCommand, int value, ref GeneratorStatus Status,ref int ArgumentNumber, ref int simForwardPower, ref int simReflectPower, string sValue = "")
  163. {
  164. switch (cometCommand)
  165. {
  166. case 8000:
  167. if(Status == GeneratorStatus.OFF)
  168. {
  169. //sendData = new byte[] { 0x01, 0x00, 0x00, 0x00 };
  170. ArgumentNumber = 1;
  171. }
  172. else if (Status == GeneratorStatus.ON)
  173. {
  174. //sendData = new byte[] { 0x02, 0x00, 0x00, 0x00 };
  175. ArgumentNumber = 2;
  176. }
  177. else if (Status == GeneratorStatus.ERROR)
  178. {
  179. //sendData = new byte[] { 0x03, 0x00, 0x00, 0x00 };
  180. ArgumentNumber = 3;
  181. }
  182. else
  183. {
  184. //sendData = new byte[] { 0x00, 0x00, 0x00, 0x00 };
  185. ArgumentNumber = 0;
  186. }
  187. break;
  188. case 8021:
  189. //LOG.Info($"{Module} [{Display} Read] RF Forward power is {value / 1000} W");
  190. if (Status == GeneratorStatus.ON)
  191. {
  192. //sendData = IntToBytes(ForwardPower);
  193. ArgumentNumber = simForwardPower;
  194. simReflectPower = 100 * 1000;
  195. }
  196. else
  197. {
  198. //sendData = IntToBytes(ForwardPower);
  199. simForwardPower = 0;
  200. simReflectPower = 0;
  201. ArgumentNumber = simForwardPower;
  202. }
  203. break;
  204. case 8022:
  205. //LOG.Info($"{Module} [{Display} Read] RF Reflected power is {value / 1000} W");
  206. //sendData = IntToBytes(ForwardPower);
  207. ArgumentNumber = simReflectPower;
  208. break;
  209. case 8100:
  210. //LOG.Info($"{Module} [{Display} Read] RF Number of Errors is {value}");
  211. break;
  212. case 8101:
  213. //LOG.Info($"{Module} [{Display} Read] RF Error 1 is {sValue}");
  214. break;
  215. case 8102:
  216. //LOG.Info($"{Module} [{Display} Read] RF Error 1 state is {value}");
  217. break;
  218. case 8103:
  219. //LOG.Info($"{Module} [{Display} Read] RF Error 2 is {sValue}");
  220. break;
  221. case 8104:
  222. //LOG.Info($"{Module} [{Display} Read] RF Error 2 state is {value}");
  223. break;
  224. case 8105:
  225. //LOG.Info($"{Module} [{Display} Read] RF Error 3 is {sValue}");
  226. break;
  227. case 8106:
  228. //LOG.Info($"{Module} [{Display} Read] RF Error 3 state is {value}");
  229. break;
  230. case 8107:
  231. //LOG.Info($"{Module} [{Display} Read] RF Error 4 is {sValue}");
  232. break;
  233. case 8108:
  234. //LOG.Info($"{Module} [{Display} Read] RF Error 4 state is {value}");
  235. break;
  236. case 8109:
  237. //LOG.Info($"{Module} [{Display} Read] RF Error 5 is {sValue}");
  238. break;
  239. case 8110:
  240. //LOG.Info($"{Module} [{Display} Read] RF Error 5 state is {value}");
  241. break;
  242. case 8111:
  243. //LOG.Info($"{Module} [{Display} Read] RF Error 6 is {sValue}");
  244. break;
  245. case 8112:
  246. //LOG.Info($"{Module} [{Display} Read] RF Error 6 state is {value}");
  247. break;
  248. case 8113:
  249. //LOG.Info($"{Module} [{Display} Read] RF Error 7 is {sValue}");
  250. break;
  251. case 8114:
  252. //LOG.Info($"{Module} [{Display} Read] RF Error 7 state is {value}");
  253. break;
  254. case 8115:
  255. //LOG.Info($"{Module} [{Display} Read] RF Error 8 is {sValue}");
  256. break;
  257. case 8116:
  258. //LOG.Info($"{Module} [{Display} Read] RF Error 8 state is {value}");
  259. break;
  260. case 8150:
  261. //LOG.Info($"{Module} [{Display} Read] RF Number of warnings is {value}");
  262. break;
  263. case 8151:
  264. //LOG.Info($"{Module} [{Display} Read] RF Warning 1 is {sValue}");
  265. break;
  266. case 8152:
  267. //LOG.Info($"{Module} [{Display} Read] RF Warning 2 is {sValue}");
  268. break;
  269. case 8153:
  270. //LOG.Info($"{Module} [{Display} Read] RF Warning 3 is {sValue}");
  271. break;
  272. case 8154:
  273. //LOG.Info($"{Module} [{Display} Read] RF Warning 4 is {sValue}");
  274. break;
  275. case 8155:
  276. //LOG.Info($"{Module} [{Display} Read] RF Warning 5 is {sValue}");
  277. break;
  278. case 8156:
  279. //LOG.Info($"{Module} [{Display} Read] RF Warning 6 is {sValue}");
  280. break;
  281. case 8157:
  282. //LOG.Info($"{Module} [{Display} Read] RF Warning 7 is {sValue}");
  283. break;
  284. case 8158:
  285. //LOG.Info($"{Module} [{Display} Read] RF Warning 8 is {sValue}");
  286. break;
  287. case 8159:
  288. //LOG.Info($"{Module} [{Display} Read] RF Warning 9 is {sValue}");
  289. break;
  290. case 8160:
  291. //LOG.Info($"{Module} [{Display} Read] RF Warning 10 is {sValue}");
  292. break;
  293. case 8161:
  294. //LOG.Info($"{Module} [{Display} Read] RF Warning 11 is {sValue}");
  295. break;
  296. case 8162:
  297. //LOG.Info($"{Module} [{Display} Read] RF Warning 12 is {sValue}");
  298. break;
  299. case 8163:
  300. //LOG.Info($"{Module} [{Display} Read] RF Warning 13 is {sValue}");
  301. break;
  302. case 8164:
  303. //LOG.Info($"{Module} [{Display} Read] RF Warning 14 is {sValue}");
  304. break;
  305. case 8165:
  306. //LOG.Info($"{Module} [{Display} Read] RF Warning 15 is {sValue}");
  307. break;
  308. case 8166:
  309. //LOG.Info($"{Module} [{Display} Read] RF Warning 16 is {sValue}");
  310. break;
  311. case 9201:
  312. ArgumentNumber = 1;
  313. break;
  314. case 9202:
  315. switch (value)
  316. {
  317. case 0:
  318. //LOG.Info($"{Module} [BiasMatch Read] holding position");
  319. break;
  320. case 1:
  321. //LOG.Info($"{Module} [BiasMatch Read] moving");
  322. break;
  323. default:
  324. break;
  325. }
  326. break;
  327. case 9203:
  328. //LOG.Info($"{Module} [BiasMatch Read] C load position is {value} %");
  329. ArgumentNumber = simCLoad;
  330. break;
  331. case 9204:
  332. //LOG.Info($"{Module} [BiasMatch Read] C tune position is {value} %");
  333. ArgumentNumber = simCTune;
  334. break;
  335. case 9205:
  336. //LOG.Info($"{Module} [BiasMatch Read] C load is {value} pF");
  337. break;
  338. case 9206:
  339. //LOG.Info($"{Module} [BiasMatch Read] C tune is {value} pF");
  340. break;
  341. case 9210:
  342. //LOG.Info($"{Module} [BiasMatch Read] C load ref pos is {value} %");
  343. break;
  344. case 9212:
  345. //LOG.Info($"{Module} [BiasMatch Read] C tune ref pos {value} %");
  346. break;
  347. case 9251:
  348. //LOG.Info($"{Module} [BiasMatch Read] Process control Matching {value} V");
  349. ArgumentNumber = simVpp;
  350. break;
  351. case 1001:
  352. switch (value)
  353. {
  354. case 0:
  355. //LOG.Info($"{Module} [{Display} Write] Switch off RF");
  356. Status = GeneratorStatus.OFF;
  357. ArgumentNumber = value;
  358. break;
  359. case 1:
  360. Status = GeneratorStatus.ON;
  361. ArgumentNumber = value;
  362. break;
  363. case 9:
  364. Status = GeneratorStatus.Unknown;
  365. ArgumentNumber = value;
  366. break;
  367. default:
  368. break;
  369. }
  370. break;
  371. case 1201:
  372. switch (value)
  373. {
  374. case 0:
  375. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Forward power");
  376. ArgumentNumber = value;
  377. break;
  378. case 1:
  379. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Load power");
  380. ArgumentNumber = value;
  381. break;
  382. case 2:
  383. //LOG.Info($"{Module} [{Display} Write] RF Control mode is Process control");
  384. ArgumentNumber = value;
  385. break;
  386. default:
  387. break;
  388. }
  389. break;
  390. case 1206:
  391. //LOG.Info($"{Module} [{Display} Write] RF Power Set point is {value / 1000} W");
  392. simForwardPower = value;
  393. ArgumentNumber = simForwardPower;
  394. break;
  395. case 8201:
  396. switch (value)
  397. {
  398. case 1:
  399. //LOG.Info($"{Module} [BiasMatch Write] Manual matching mode");
  400. break;
  401. case 2:
  402. //LOG.Info($"{Module} [BiasMatch Write] Automatic matching mode");
  403. break;
  404. default:
  405. break;
  406. }
  407. break;
  408. case 8202:
  409. switch (value)
  410. {
  411. case 0:
  412. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic hold mode");
  413. ArgumentNumber = value;
  414. break;
  415. case 1:
  416. //LOG.Info($"{Module} [BiasMatch Write] BiasMatch is Automatic preset mode");
  417. ArgumentNumber = value;
  418. break;
  419. default:
  420. break;
  421. }
  422. break;
  423. case 8203:
  424. //LOG.Info($"{Module} [{Display} Write] Set C load ref position is {value / 10} %");
  425. ArgumentNumber = value;
  426. simCLoad = value;
  427. break;
  428. case 8204:
  429. //LOG.Info($"{Module} [{Display} Write] Set C tune ref position is {value / 10} %");
  430. ArgumentNumber = value;
  431. simCTune = value;
  432. break;
  433. default:
  434. break;
  435. }
  436. }
  437. protected byte[] BuildMessage(int transactionNumber, Mode mode, int commandNumber, int argumentNumber)
  438. {
  439. List<byte> buffer = new List<byte>();
  440. buffer.Add(BitConverter.GetBytes(transactionNumber)[1]);
  441. buffer.Add(BitConverter.GetBytes(transactionNumber)[0]);
  442. buffer.Add(ProtocolIdentifierHighByte);
  443. buffer.Add(ProtocolIdentifierLowByte);
  444. if (mode == Mode.Read)
  445. {
  446. buffer.Add(0x00);
  447. buffer.Add(0x07);
  448. }
  449. else if (mode == Mode.Write)
  450. {
  451. buffer.Add(0x00);
  452. buffer.Add(0x08);
  453. }
  454. buffer.Add(Address);
  455. if (mode == Mode.Read)
  456. {
  457. buffer.Add(_read);
  458. }
  459. else if (mode == Mode.Write)
  460. {
  461. buffer.Add(_write);
  462. }
  463. if (mode == Mode.Read)
  464. {
  465. buffer.Add(0x04);
  466. //buffer.Add(sendData[0]);
  467. //buffer.Add(sendData[1]);
  468. //buffer.Add(sendData[2]);
  469. //buffer.Add(sendData[3]);
  470. buffer.Add(BitConverter.GetBytes(argumentNumber)[3]);
  471. buffer.Add(BitConverter.GetBytes(argumentNumber)[2]);
  472. buffer.Add(BitConverter.GetBytes(argumentNumber)[1]);
  473. buffer.Add(BitConverter.GetBytes(argumentNumber)[0]);
  474. }
  475. else if (mode == Mode.Write)
  476. {
  477. buffer.Add(BitConverter.GetBytes(commandNumber)[1]);
  478. buffer.Add(BitConverter.GetBytes(commandNumber)[0]);
  479. buffer.Add(BitConverter.GetBytes(argumentNumber)[3]);
  480. buffer.Add(BitConverter.GetBytes(argumentNumber)[2]);
  481. buffer.Add(BitConverter.GetBytes(argumentNumber)[1]);
  482. buffer.Add(BitConverter.GetBytes(argumentNumber)[0]);
  483. }
  484. //byte checkSum = 0;
  485. //for (int i = 0; i < buffer.Count; i++)
  486. //{
  487. // checkSum += buffer[i];
  488. //}
  489. //buffer.Add(checkSum);
  490. return buffer.ToArray();
  491. }
  492. public int BytesToInt(byte[] src, int offset)
  493. {
  494. int value;
  495. value = (int)((src[offset + 3] & 0xFF)
  496. | ((src[offset + 2] & 0xFF) << 8)
  497. | ((src[offset + 1] & 0xFF) << 16)
  498. | ((src[offset] & 0xFF) << 24));
  499. return value;
  500. }
  501. public byte[] IntToBytes(int value)
  502. {
  503. byte[] src = new byte[4];
  504. src[0] = (byte)((value >> 24) & 0xFF);
  505. src[1] = (byte)((value >> 16) & 0xFF);
  506. src[2] = (byte)((value >> 8) & 0xFF);
  507. src[3] = (byte)(value & 0xFF);
  508. return src;
  509. }
  510. }
  511. }