Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Microsoft.Extensions.Configuration;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Ports;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.Threading;
  10. namespace TcpToComPorts
  11. {
  12. class Program
  13. {
  14. static List<TcpClient> tcpClients = new List<TcpClient>();
  15. static List<SerialPort> serialPorts = new List<SerialPort>();
  16. static void Main(string[] args)
  17. {
  18. var builder = new ConfigurationBuilder()
  19. .SetBasePath(Directory.GetCurrentDirectory())
  20. .AddJsonFile("settings.json", optional: false);
  21. var configuration = builder.Build();
  22. var tcpPortStrings = configuration.GetValue<string>("listeningTcpPorts").Split(',');
  23. var mapToComPortStrings = configuration.GetValue<string>("tcpPortsMapToComPorts").Split(',');
  24. if (tcpPortStrings.Length != mapToComPortStrings.Length)
  25. {
  26. Console.WriteLine("tcp ports count should equals to maptoCompPorts count");
  27. Console.ReadLine();
  28. return;
  29. }
  30. ThreadPool.QueueUserWorkItem(_ =>
  31. {
  32. for (int i = 0; i < mapToComPortStrings.Length; i++)
  33. {
  34. int j = i;
  35. SerialPort serialPort = new SerialPort("COM" + mapToComPortStrings[i],
  36. 9600, Parity.Odd, 8, StopBits.One);
  37. serialPort.Open();
  38. serialPorts.Add(serialPort);
  39. serialPort.DataReceived += (s, a) =>
  40. {
  41. try
  42. {
  43. //if (tcpClients.Count >= (i + 1))
  44. {
  45. var buffer = new List<byte>();
  46. while (serialPort.BytesToRead > 0)
  47. {
  48. buffer.Add((byte)serialPort.ReadByte());
  49. }
  50. Console.WriteLine("Com" + serialPort.PortName + " to Tcp" + (tcpClients[j].Client.RemoteEndPoint as IPEndPoint).Port
  51. + ": 0x" + buffer.Select(b => b.ToString("X").PadLeft(2, '0')).Aggregate((acc, n) => acc + n));
  52. tcpClients[j]?.GetStream().Write(buffer.ToArray());
  53. }
  54. }
  55. catch (Exception exx)
  56. {
  57. }
  58. };
  59. }
  60. for (int i = 0; i < tcpPortStrings.Length; i++)
  61. {
  62. tcpClients.Add(null);
  63. int tcpPort = int.Parse(tcpPortStrings[i]);
  64. TcpListener l = new TcpListener(IPAddress.Any, tcpPort);
  65. l.Start();
  66. Console.WriteLine("Tcp listening port: " + tcpPort);
  67. ReAcceptTcpClient(l, i);
  68. }
  69. });
  70. while (true)
  71. Console.ReadKey();
  72. Console.WriteLine("Hello World!");
  73. }
  74. static void ReAcceptTcpClient(TcpListener tcpListener, int index)
  75. {
  76. var result = tcpListener.BeginAcceptTcpClient((ar) =>
  77. {
  78. var listener = (TcpListener)ar.AsyncState;
  79. var tcpClient = listener.EndAcceptTcpClient(ar);
  80. tcpClients[index] = tcpClient;
  81. ReReadTcpData(tcpClient, index);
  82. ReAcceptTcpClient(tcpListener, index);
  83. }, tcpListener);
  84. }
  85. static void ReReadTcpData(TcpClient tcpClient, int index)
  86. {
  87. var buffer = new byte[2000];
  88. try
  89. {
  90. var r = tcpClient.GetStream().BeginRead(buffer, 0, 2000, (cb =>
  91. {
  92. try
  93. {
  94. var readCount = tcpClient.Client.EndReceive(cb);
  95. if (serialPorts.Count >= (index + 1))
  96. {
  97. Console.WriteLine("Tcp" + (tcpClient.Client.LocalEndPoint as IPEndPoint).Port
  98. + " to Com" + serialPorts[index].PortName +
  99. ": 0x" + buffer.Take(readCount).Select(b => b.ToString("X").PadLeft(2, '0')).Aggregate((acc, n) => acc + n));
  100. serialPorts[index].Write(buffer, 0, readCount);
  101. }
  102. }
  103. catch (Exception exxx)
  104. {
  105. Console.WriteLine("exception in BeginRead()");
  106. return;
  107. }
  108. ReReadTcpData(tcpClient, index);
  109. })
  110. , tcpClient);
  111. }
  112. catch (Exception exxx)
  113. {
  114. Console.WriteLine("exception in ReReadTcpData()");
  115. }
  116. }
  117. }
  118. }