diff --git a/ErgometerApplication/.vs/ErgometerApplication/v14/.suo b/ErgometerApplication/.vs/ErgometerApplication/v14/.suo index 65dfc85..c31ed23 100644 Binary files a/ErgometerApplication/.vs/ErgometerApplication/v14/.suo and b/ErgometerApplication/.vs/ErgometerApplication/v14/.suo differ diff --git a/ErgometerApplication/ErgometerApplication/Ergometer.Designer.cs b/ErgometerApplication/ErgometerApplication/Ergometer.Designer.cs index 0c2343a..f1dc963 100644 --- a/ErgometerApplication/ErgometerApplication/Ergometer.Designer.cs +++ b/ErgometerApplication/ErgometerApplication/Ergometer.Designer.cs @@ -73,9 +73,7 @@ namespace ErgometerApplication // ComPortBox // this.ComPortBox.FormattingEnabled = true; - this.ComPortBox.Items.AddRange(new object[] { - "COM8", - "COM9"}); + this.ComPortBox.Items.AddRange(SerialPort.GetPortNames()); this.ComPortBox.Location = new System.Drawing.Point(12, 290); this.ComPortBox.Margin = new System.Windows.Forms.Padding(3, 2, 3, 2); this.ComPortBox.Name = "ComPortBox"; diff --git a/ErgometerApplication/ErgometerApplication/Ergometer.cs b/ErgometerApplication/ErgometerApplication/Ergometer.cs index 40abad6..ce10b12 100644 --- a/ErgometerApplication/ErgometerApplication/Ergometer.cs +++ b/ErgometerApplication/ErgometerApplication/Ergometer.cs @@ -13,6 +13,10 @@ using Newtonsoft.Json; using System.IO; using ErgometerLibrary; using System.Net.Sockets; +using System.Net; +using System.Timers; +using ErgometerLibrary; +using Timer = System.Timers.Timer; namespace ErgometerApplication { @@ -21,14 +25,15 @@ namespace ErgometerApplication private ComPort comPort; private List _data; private int i = 0; - int sessionID; NetCommand command; + private ServerCommunicator communicator = new ServerCommunicator(); List readFile = new List(); public Ergometer() { InitializeComponent(); comPort = new ComPort(); _data = new List(); + communicator.data = _data; } private void connectButton_Click(object sender, EventArgs e) @@ -61,7 +66,7 @@ namespace ErgometerApplication Meting m = Meting.Parse(response); SaveData(m); - richTextBox1.Text = m.ToString(); + // richTextBox1.Text = m.ToString(); } } else @@ -85,7 +90,16 @@ namespace ErgometerApplication } } - sessionID = int.Parse(ReadServer()); + IPAddress ipAddress; //= IPAddress.Parse("127.0.0.1"); + + bool ipIsOk = IPAddress.TryParse("127.0.0.1", out ipAddress); //GetIp() + if (!ipIsOk) { Console.WriteLine("ip adres kan niet geparsed worden."); Environment.Exit(1); } + communicator.client = new TcpClient(); + communicator.client.Connect("127.0.0.1", 8888); + communicator.reader = new StreamReader(communicator.client.GetStream(), Encoding.Unicode); + communicator.writer = new StreamWriter(communicator.client.GetStream(), Encoding.Unicode); + Thread thread = new Thread(ServerCommunication); + thread.Start(communicator); } private void statusButton_Click(object sender, EventArgs e) @@ -93,14 +107,37 @@ namespace ErgometerApplication comPort.Write("ST"); string response = comPort.Read(); Console.WriteLine(response); - Meting m = Meting.Parse(response); + Meting m = Meting.Parse(response, '\t'); + Console.WriteLine(m); SaveData(m); - command = new NetCommand(m, sessionID); - WriteServer(command.ToString()); + communicator.data.Add(m); + command = new NetCommand(m, communicator.sessionId); + communicator.writer.WriteLine(command.ToString()); richTextBox1.Text = m.ToString(); WriteFile(); } + static void ServerCommunication(object obj) + { + ServerCommunicator communicator = obj as ServerCommunicator; + communicator.reader = new StreamReader(communicator.client.GetStream(), Encoding.Unicode); + communicator.writer = new StreamWriter(communicator.client.GetStream(), Encoding.Unicode); + int sessionId = 0; + communicator.writer.WriteLine("5»ses?"); + communicator.writer.Flush(); + string session = communicator.reader.ReadLine(); + if (session != null) + { + session = session.Remove(0, 5); + sessionId = int.Parse(session); + communicator.sessionId = sessionId; + } + NetCommand command = new NetCommand("name", false, sessionId); + communicator.writer.WriteLine(command.ToString()); + communicator.writer.Flush(); + communicator.reader.ReadLine(); + } + private void resetButton_Click(object sender, EventArgs e) { comPort.Write("RS"); @@ -119,9 +156,13 @@ namespace ErgometerApplication Console.WriteLine(response); Meting m = Meting.Parse(response); SaveData(m); - command = new NetCommand(m, sessionID); - WriteServer(command.ToString()); + communicator.data.Add(m); + command = new NetCommand(m, communicator.sessionId); + communicator.writer.WriteLine(command.ToString()); richTextBox1.Text = m.ToString(); + command = new NetCommand(communicator.data[0], communicator.sessionId); + communicator.writer.WriteLine(command.ToString()); + communicator.writer.Flush(); } } @@ -183,8 +224,6 @@ namespace ErgometerApplication } } - - private void readButton_Click(object sender, EventArgs e) { ReadFile(); @@ -257,20 +296,5 @@ namespace ErgometerApplication readFile = null; } } - System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient(); - private String ReadServer() - { - - StreamReader reader = new StreamReader(client.GetStream(), Encoding.ASCII); - String b = reader.ReadLine(); - return b; - } - private void WriteServer(String Send) - { - - StreamWriter stream = new StreamWriter(client.GetStream(), Encoding.ASCII); - stream.WriteLine(Send); - - } } } \ No newline at end of file diff --git a/ErgometerApplication/ErgometerApplication/Ergometer.resx b/ErgometerApplication/ErgometerApplication/Ergometer.resx index e7c8a0a..5223d06 100644 --- a/ErgometerApplication/ErgometerApplication/Ergometer.resx +++ b/ErgometerApplication/ErgometerApplication/Ergometer.resx @@ -123,4 +123,10 @@ 147, 19 + + 276, 19 + + + 377, 19 + \ No newline at end of file diff --git a/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj b/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj index 59cc704..fbd48ff 100644 --- a/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj +++ b/ErgometerApplication/ErgometerApplication/ErgometerApplication.csproj @@ -53,6 +53,7 @@ + Form diff --git a/ErgometerApplication/ErgometerApplication/ServerCommunicator.cs b/ErgometerApplication/ErgometerApplication/ServerCommunicator.cs new file mode 100644 index 0000000..d8428fc --- /dev/null +++ b/ErgometerApplication/ErgometerApplication/ServerCommunicator.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; +using ErgometerLibrary; + +namespace ErgometerApplication +{ + class ServerCommunicator + { + public TcpClient client { get; set; } + public StreamWriter writer { get; set; } + public StreamReader reader { get; set; } + public int sessionId { get; set; } + public List data { get; set; } + } +}