CometRFSocketServer.cs 22 KB

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