using Edge.Core.Processor.Communicator; using System; using System.Collections.Generic; using System.IO.Ports; using System.Linq; using System.Threading; namespace TestComPortServer { class Program { static void Main(string[] args) { //var list0 = new List() { }; //var list1 = new List() { 1, 2, 3, 4, 5 }; //var except = list0.Except(list1).Union(list1.Except(list0)); //Console.WriteLine("except are:" + // (except.Any() ? (except.Select(s => s.ToString()).Aggregate((acc, n) => acc + ", " + n)) : "")); //Console.ReadKey(); int totalTestMsgCount = int.Parse(args[0]); int reportInterval = int.Parse(args[1]); Console.WriteLine("Start ComToTcpTest..."); ComToTcpTest(reportInterval, totalTestMsgCount, "192.168.0.7", 20001, "COM5", () => { Thread.Sleep(5000); Console.WriteLine("Start TcpToComTest..."); TcpToComTest(reportInterval, totalTestMsgCount, "192.168.0.7", 20001, "COM5", () => { Thread.Sleep(5000); Console.WriteLine("Start ComToTcpTest..."); ComToTcpTest(reportInterval, totalTestMsgCount, "192.168.0.7", 20001, "COM5", null); }); }); while (true) Console.ReadLine(); } static void TcpToComTest(int progress, int totalTestMsgCount, string tcpServerIp, int tcpServerPort, string comPortName, Action callback) { TcpClientCommunicator communicator = new TcpClientCommunicator( new MessageCutter(), new MessageParser(), tcpServerIp, tcpServerPort, null, null); communicator.OnConnected += (_, __) => { SerialPort serialPort = new SerialPort(comPortName, 9600, Parity.Odd, 8, StopBits.One); List comPortReceived = new List(); serialPort.DataReceived += (s, sa) => { while (serialPort.BytesToRead > 0) { comPortReceived.Add((byte)serialPort.ReadByte()); } }; serialPort.Open(); var parser = new MessageParser(); // counters var delayTimes = new List>() { new Tuple(10,0), new Tuple(30,0), new Tuple(50,0), new Tuple(100,0), new Tuple(150,0), new Tuple(200,0), new Tuple(300,0), new Tuple(500,0), new Tuple(800,0), new Tuple(1000,0), new Tuple(2000,0), }; for (int i = 0; i < totalTestMsgCount; i++) { if (i % progress == 0) { Console.WriteLine("(TcpToCom)sending at " + (i + 1) + " message"); } var msg = new Message() { Id = i, Description = "abcde" }; var tcpSend = parser.Serialize(msg); communicator.Write(msg); int hitTime = 0; for (int d = 0; d < delayTimes.Count; d++) { Thread.Sleep(delayTimes[d].Item1); if (!comPortReceived.Any() || comPortReceived.Count != tcpSend.Length || comPortReceived.Except(tcpSend).Union(tcpSend.Except(comPortReceived)).Any() ) { continue; } hitTime = d; break; } var reportItem = delayTimes[hitTime]; delayTimes.RemoveAt(hitTime); delayTimes.Insert(hitTime, new Tuple(reportItem.Item1, reportItem.Item2 + 1)); comPortReceived.Clear(); } communicator.Dispose(); serialPort.Close(); Console.WriteLine("Tcp to Comport Report(total send msg: " + totalTestMsgCount + "):\r\n " + delayTimes.Select(b => b.Item1 + "ms : " + b.Item2 + " times").Aggregate((acc, n) => acc + "\r\n" + n)); callback?.Invoke(); }; communicator.Start(); } static void ComToTcpTest(int progress, int totalTestMsgCount, string tcpServerIp, int tcpServerPort, string comPortName, Action callback) { TcpClientCommunicator communicator = new TcpClientCommunicator( new MessageCutter(), new MessageParser(), tcpServerIp, tcpServerPort, null, null); List tcpReceived = new List(); communicator.OnDataReceived += (a, b) => { tcpReceived = b.Data.ToList(); }; communicator.OnConnected += (_, __) => { SerialPort serialPort = new SerialPort(comPortName, 9600, Parity.Odd, 8, StopBits.One); serialPort.Open(); var parser = new MessageParser(); var delayTimes = new List>() { new Tuple(10,0), new Tuple(30,0), new Tuple(50,0), new Tuple(100,0), new Tuple(150,0), new Tuple(200,0), new Tuple(300,0), new Tuple(500,0), new Tuple(800,0), new Tuple(1000,0), new Tuple(2000,0), }; for (int i = 0; i < totalTestMsgCount; i++) { if (i % progress == 0) { Console.WriteLine("(ComToTcp)sending at " + (i + 1) + " message"); } var msg = new Message() { Id = i, Description = "abcde" }; var comSend = parser.Serialize(msg); serialPort.Write(comSend, 0, comSend.Length); //serialPort.BaseStream.Flush(); int hitTime = 0; for (int d = 0; d < delayTimes.Count; d++) { Thread.Sleep(delayTimes[d].Item1); if (!tcpReceived.Any() || tcpReceived.Count != comSend.Length || tcpReceived.Except(comSend).Union(comSend.Except(tcpReceived)).Any() ) { continue; } hitTime = d; break; } var reportItem = delayTimes[hitTime]; delayTimes.RemoveAt(hitTime); delayTimes.Insert(hitTime, new Tuple(reportItem.Item1, reportItem.Item2 + 1)); tcpReceived.Clear(); } communicator.Dispose(); serialPort.Close(); Console.WriteLine("Com to Tcp Report(total send msg: " + totalTestMsgCount + "):\r\n " + delayTimes.Select(b => b.Item1 + "ms : " + b.Item2 + " times").Aggregate((acc, n) => acc + "\r\n" + n)); callback?.Invoke(); }; communicator.Start(); } } }