diff --git a/PIC_Simulator/Form1.Designer.cs b/PIC_Simulator/Form1.Designer.cs index 2fb24f4..6c5d513 100644 --- a/PIC_Simulator/Form1.Designer.cs +++ b/PIC_Simulator/Form1.Designer.cs @@ -102,6 +102,10 @@ this.btn_RB_Tris_0 = new System.Windows.Forms.Button(); this.btn_RA_0 = new System.Windows.Forms.Button(); this.btn_RA_Tris_0 = new System.Windows.Forms.Button(); + this.edRS232Log = new System.Windows.Forms.TextBox(); + this.cbxComPorts = new System.Windows.Forms.ComboBox(); + this.btnRSConnect = new System.Windows.Forms.Button(); + this.btnRSDisconnect = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // @@ -758,11 +762,51 @@ this.btn_RA_Tris_0.UseVisualStyleBackColor = true; this.btn_RA_Tris_0.Click += new System.EventHandler(this.btn_RA_Tris_0_Click); // + // edRS232Log + // + this.edRS232Log.Location = new System.Drawing.Point(1129, 494); + this.edRS232Log.Multiline = true; + this.edRS232Log.Name = "edRS232Log"; + this.edRS232Log.Size = new System.Drawing.Size(157, 183); + this.edRS232Log.TabIndex = 54; + // + // cbxComPorts + // + this.cbxComPorts.FormattingEnabled = true; + this.cbxComPorts.Location = new System.Drawing.Point(1129, 438); + this.cbxComPorts.Name = "cbxComPorts"; + this.cbxComPorts.Size = new System.Drawing.Size(157, 21); + this.cbxComPorts.TabIndex = 55; + // + // btnRSConnect + // + this.btnRSConnect.Location = new System.Drawing.Point(1129, 465); + this.btnRSConnect.Name = "btnRSConnect"; + this.btnRSConnect.Size = new System.Drawing.Size(75, 23); + this.btnRSConnect.TabIndex = 56; + this.btnRSConnect.Text = "Connect"; + this.btnRSConnect.UseVisualStyleBackColor = true; + this.btnRSConnect.Click += new System.EventHandler(this.btnRSConnect_Click); + // + // btnRSDisconnect + // + this.btnRSDisconnect.Location = new System.Drawing.Point(1212, 464); + this.btnRSDisconnect.Name = "btnRSDisconnect"; + this.btnRSDisconnect.Size = new System.Drawing.Size(75, 23); + this.btnRSDisconnect.TabIndex = 57; + this.btnRSDisconnect.Text = "Disconnect"; + this.btnRSDisconnect.UseVisualStyleBackColor = true; + this.btnRSDisconnect.Click += new System.EventHandler(this.btnRSDisconnect_Click); + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1298, 692); + this.Controls.Add(this.btnRSDisconnect); + this.Controls.Add(this.btnRSConnect); + this.Controls.Add(this.cbxComPorts); + this.Controls.Add(this.edRS232Log); this.Controls.Add(this.btn_RB_0); this.Controls.Add(this.btn_RB_Tris_0); this.Controls.Add(this.btn_RA_0); @@ -900,6 +944,10 @@ private System.Windows.Forms.Button btn_RB_Tris_0; private System.Windows.Forms.Button btn_RA_0; private System.Windows.Forms.Button btn_RA_Tris_0; + private System.Windows.Forms.TextBox edRS232Log; + private System.Windows.Forms.ComboBox cbxComPorts; + private System.Windows.Forms.Button btnRSConnect; + private System.Windows.Forms.Button btnRSDisconnect; } } diff --git a/PIC_Simulator/Form1.cs b/PIC_Simulator/Form1.cs index 907245c..9c870a3 100644 --- a/PIC_Simulator/Form1.cs +++ b/PIC_Simulator/Form1.cs @@ -13,6 +13,7 @@ namespace PIC_Simulator private PICProgramm programm; private Timer quartztimer; + private RS232Verbindung rs232 = new RS232Verbindung(); public Form1() { @@ -21,6 +22,8 @@ namespace PIC_Simulator quartztimer = new Timer(); quartztimer.Tick += timer1_Tick; + cbxComPorts.Items.AddRange(rs232.GetSerialPorts().ToArray()); + for (int i = 0; i < 16; i++) { var item = new ListViewItem(new[] { string.Format("0x{0:X}0", i), "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00", "00" }); @@ -50,6 +53,7 @@ namespace PIC_Simulator quartztimer.Stop(); quartztimer.Interval = int.Parse(insertTime.Text); quartztimer.Start(); + rs232.Disconnect(); } private void cmd_next_Click(object sender, EventArgs e) @@ -116,6 +120,7 @@ namespace PIC_Simulator programm = new PICProgramm(); programm.Laden(box_CodeView.Text); + rs232.Disconnect(); OberflaecheAktualisieren(); } @@ -146,7 +151,7 @@ namespace PIC_Simulator OberflaecheAktualisieren(); } - private void OberflaecheAktualisieren() + public void OberflaecheAktualisieren() { // Zeile highlighten @@ -468,6 +473,16 @@ namespace PIC_Simulator programm.SetRegisterOhneBank(PICProgramm.ADDR_PORT_B, 0, !programm.GetRegisterOhneBank(PICProgramm.ADDR_PORT_B, 0)); OberflaecheAktualisieren(); } + + private void btnRSConnect_Click(object sender, EventArgs e) + { + rs232.Connect(cbxComPorts.SelectedItem.ToString(), edRS232Log, programm, this); + } + + private void btnRSDisconnect_Click(object sender, EventArgs e) + { + rs232.Disconnect(); + } } } diff --git a/PIC_Simulator/PIC/PICProgramm.cs b/PIC_Simulator/PIC/PICProgramm.cs index 3ebc253..3aca4ee 100644 --- a/PIC_Simulator/PIC/PICProgramm.cs +++ b/PIC_Simulator/PIC/PICProgramm.cs @@ -808,6 +808,11 @@ namespace PIC_Simulator.PIC Register[index] = SetBit(Register[index], bit, wert); } + public void SetRegisterOhneBank(uint index, uint wert) + { + Register[index] = wert; + } + public bool GetRegisterOhneBank(uint index, uint bit) { return GetBit(Register[index], bit); diff --git a/PIC_Simulator/PIC/RS232Verbindung.cs b/PIC_Simulator/PIC/RS232Verbindung.cs new file mode 100644 index 0000000..96d0cba --- /dev/null +++ b/PIC_Simulator/PIC/RS232Verbindung.cs @@ -0,0 +1,210 @@ +using System; +using System.Collections.Generic; +using System.IO.Ports; +using System.Linq; +using System.Windows.Forms; + +namespace PIC_Simulator.PIC +{ + class RS232Verbindung + { + private SerialPort ComPort; + private PICProgramm Programm; + private TextBox TextBox; + private Form1 Fenster; + + private Timer timer; + + public RS232Verbindung() + { + timer = new Timer(); + timer.Tick += TimerOnTick; + timer.Interval = 500; + timer.Enabled = true; + } + + private void TimerOnTick(object sender, EventArgs eventArgs) + { + if (ComPort == null) return; + if (!ComPort.IsOpen) return; + + SendData(); + RecieveData(); + } + + public List GetSerialPorts() + { + string[] ArrayComPortsNames = null; + int index = -1; + string ComPortName = null; + + List result = new List(); + + ArrayComPortsNames = SerialPort.GetPortNames(); + do + { + index += 1; + result.Add(ArrayComPortsNames[index]); + } + while (!((ArrayComPortsNames[index] == ComPortName) || (index == ArrayComPortsNames.GetUpperBound(0)))); + + return result.OrderBy(p => p).ToList(); + } + + public void Connect(string port, TextBox logbox, PICProgramm prog, Form1 f) + { + ComPort = new SerialPort(); + + ComPort.PortName = port; + ComPort.BaudRate = 4800; + ComPort.DataBits = 8; + ComPort.Parity = Parity.None; + ComPort.StopBits = StopBits.One; + + try + { + ComPort.Open(); + } + catch (Exception ex) + { + logbox.Text += ex + "\r\n"; + ComPort = null; + return; + } + + if (ComPort.IsOpen) + { + logbox.Text += "Connected to " + port + "\r\n"; + Programm = prog; + TextBox = logbox; + Fenster = f; + return; + } + + ComPort = null; + } + + public void Disconnect() + { + ComPort.Close(); + ComPort = null; + } + + private void SendData() + { + string p_a = encodeByte(Programm.GetRegisterOhneBank(PICProgramm.ADDR_PORT_A)); + string t_a = encodeByte(Programm.GetRegisterOhneBank(PICProgramm.ADDR_TRIS_A)); + string p_b = encodeByte(Programm.GetRegisterOhneBank(PICProgramm.ADDR_PORT_B)); + string t_b = encodeByte(Programm.GetRegisterOhneBank(PICProgramm.ADDR_TRIS_B)); + + string send = t_a + p_a + t_b + p_b; + + Send_RS232(send); + } + + private void Send_RS232(string txt) + { + if (txt != string.Empty) + { + ComPort.Write(txt + '\r'); + TextBox.Text += "> " + txt + "\r\n"; + } + } + + private void RecieveData() + { + string x = ReadDataSegment(); + + if (x != string.Empty) + { + if (x == null) + { + TextBox.Text += ("< [ERR-R] NULL") + "\r\n"; + } + else if (x.Length == 4) + { + TextBox.Text += ("< " + x) + "\r\n"; + + var v = decodeBytes(x); + + if (v == null) + { + TextBox.Text += ("< [ERR-D] " + x) + "\r\n"; + } + else + { + Programm.SetRegisterOhneBank(PICProgramm.ADDR_PORT_A, v.Item1); + Programm.SetRegisterOhneBank(PICProgramm.ADDR_PORT_B, v.Item2); + Fenster.OberflaecheAktualisieren(); + } + } + else + { + TextBox.Text += ("< [ERR-L] " + x) + "\r\n"; + } + + x = ReadDataSegment(); + } + } + + private string encodeByte(uint b) + { + char c1 = (char)(0x30 + ((b & 0xF0) >> 4)); + char c2 = (char)(0x30 + (b & 0x0F)); + + return "" + c1 + c2; + } + + private Tuple decodeBytes(string s) + { + int i0 = s[0] - 0x30; + int i1 = s[1] - 0x30; + int i2 = s[2] - 0x30; + int i3 = s[3] - 0x30; + + if (i0 >= 0 && i1 >= 0 && i2 >= 0 && i3 >= 0 && i0 <= 0xF && i1 <= 0xF && i2 <= 0xF && i3 <= 0xF) + { + uint a = (((uint)i0 & 0x0F) << 4) | ((uint)i1 & 0x0F); + uint b = (((uint)i2 & 0x0F) << 4) | ((uint)i3 & 0x0F); + + return Tuple.Create(a, b); + } + else + { + return null; + } + } + + private string ReadDataSegment() + { + string s = ""; + + if (ComPort.BytesToRead >= 5) + { + char? c = null; + + int idx = 5; + while (c != '\r' && idx > 0) + { + c = (char)ComPort.ReadByte(); + + s += c; + + idx--; + } + + if (idx <= 0 && c != '\r') + { + return null; + } + } + else + { + return string.Empty; + } + + return s.Trim('\r'); + } + + } +} diff --git a/PIC_Simulator/PIC_Simulator.csproj b/PIC_Simulator/PIC_Simulator.csproj index aa71077..3992113 100644 --- a/PIC_Simulator/PIC_Simulator.csproj +++ b/PIC_Simulator/PIC_Simulator.csproj @@ -52,6 +52,7 @@ Form1.cs + diff --git a/Projektinformationen/Tabelle.txt b/Projektinformationen/Tabelle.txt index e5330ef..b627b2a 100644 --- a/Projektinformationen/Tabelle.txt +++ b/Projektinformationen/Tabelle.txt @@ -11,7 +11,7 @@ [?] Latchfunktion der IO-Register [X] frei wählbare Quarzfrequenz [X] Breakpoints -[ ] Hardwareansteuerung +[X] Hardwareansteuerung [X] Testprogramm 1 (Literalbefehle) [ ] Testprogramm 2 (CALL, GOTO etc) [ ] Testprogramm 3 (Byte Befehle ohne 4).