using Rs.Framework; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static System.Net.Mime.MediaTypeNames; namespace Rs.MotionPlat.SysConfig { public partial class LightConfig : Form { SerialPort sp; public LightConfig() { InitializeComponent(); sp = new SerialPort(); sp.BaudRate = 19200; sp.DataBits = 8; sp.Parity = Parity.None; sp.StopBits = StopBits.One; sp.PortName = "COM2"; sp.DataReceived += Sp_DataReceived; } List reciveData = new List(); string reciveMsg = string.Empty; private void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] readBuffer = new byte[1024]; int len = sp.BytesToRead; if (len > 0) { sp.Read(readBuffer, 0, len); reciveData.AddRange(readBuffer.Take(len)); string buf = Encoding.ASCII.GetString(readBuffer, 0, len); LogHelper.Debug(" >>> " + buf); if (buf == "+OK") { reciveMsg = Encoding.ASCII.GetString(reciveData.ToArray()); Invoke(new Action(() => { txtRunResult.AppendText(Encoding.ASCII.GetString(reciveData.ToArray()) + Environment.NewLine); })); LogHelper.Debug(Encoding.ASCII.GetString(reciveData.ToArray())); reciveData.Clear(); } else if (buf.IndexOf("#") > 0) { reciveMsg = Encoding.ASCII.GetString(reciveData.ToArray()); Invoke(new Action(() => { txtRunResult.AppendText(Encoding.ASCII.GetString(reciveData.ToArray()) + Environment.NewLine); })); LogHelper.Debug(Encoding.ASCII.GetString(reciveData.ToArray())); reciveData.Clear(); } } } private void btnOpen_Click(object sender, EventArgs e) { try { if (sp != null && sp.IsOpen) { sp.Close(); } sp.Open(); txtRunResult.AppendText("Open serial success!" + Environment.NewLine); } catch (Exception) { txtRunResult.AppendText("Open serial fail!!!" + Environment.NewLine); } } private void btnLoadParam_Click(object sender, EventArgs e) { Task.Run(() => { if (sp.IsOpen) { string msg = "$RD=9999#"; sp.Write(msg); LogHelper.Debug(" <<< " + msg); reciveMsg = ""; Dictionary kv = new Dictionary(); while (true) { if (!string.IsNullOrEmpty(reciveMsg)) { reciveMsg = reciveMsg.Replace("$", "").Replace("#", ""); string[] items = reciveMsg.Split(','); foreach (string item in items) { string[] keyvalue = item.Split('='); if (!kv.ContainsKey(keyvalue[0])) { kv.Add(keyvalue[0], keyvalue[1]); } } string val = kv[$"TR"]; Control[] ctls = this.Controls.Find($"rbtnTR{val}", true); if (ctls != null && ctls.Length > 0) { Invoke(new Action(() => { ((RadioButton)ctls[0]).Checked = true; })); } val = kv[$"GR"]; ctls = this.Controls.Find($"rbtnGR{val}", true); if (ctls != null && ctls.Length > 0) { Invoke(new Action(() => { ((RadioButton)ctls[0]).Checked = true; })); } for (int i = 0; i < 4; i++) { Invoke(new Action(() => { val = kv[$"F{i}"]; ctls = this.Controls.Find($"cboxF{i}", true); if (ctls != null && ctls.Length > 0) { ((CheckBox)ctls[0]).Checked = val == "1" ? true : false; } val = kv[$"L{i}"]; ctls = this.Controls.Find($"txtL{i}", true); if (ctls != null && ctls.Length > 0) { ((TextBox)ctls[0]).Text = val; } val = kv[$"T{i}"]; ctls = this.Controls.Find($"txtT{i}", true); if (ctls != null && ctls.Length > 0) { ((TextBox)ctls[0]).Text = val; } val = kv[$"P{i}"]; ctls = this.Controls.Find($"txtP{i}", true); if (ctls != null && ctls.Length > 0) { ((TextBox)ctls[0]).Text = val; } })); } break; } } } }); } private void btnSendParam_Click(object sender, EventArgs e) { if (sp.IsOpen) { string msg = "$SA=1#"; byte[] datas = Encoding.ASCII.GetBytes(msg); sp.Write(datas, 0, datas.Length); LogHelper.Debug(" <<< " + msg); } } private void enable_CheckedChanged(object sender, EventArgs e) { Task.Run(() => { string msg = ""; CheckBox cbox = (CheckBox)sender; int val = cbox.Checked ? 1 : 0; msg = $"${cbox.Name.Replace("cbox", "")}={val}#"; if (sp.IsOpen) { byte[] datas = Encoding.ASCII.GetBytes(msg); sp.Write(datas, 0, datas.Length); } }); } private void txtParam_KeyUp(object sender, KeyEventArgs e) { Task.Run(() => { string msg = ""; TextBox txt = (TextBox)sender; msg = $"${txt.Name.Replace("txt", "")}={txt.Text}#"; if (sp.IsOpen) { byte[] datas = Encoding.ASCII.GetBytes(msg); sp.Write(datas, 0, datas.Length); LogHelper.Debug(" <<< " + msg); } }); } private void triggerMode_CheckedChanged(object sender, EventArgs e) { Task.Run(() => { string msg = ""; RadioButton rbtn = (RadioButton)sender; if (rbtn.Checked) { msg = $"$TR={rbtn.Name.GetLastChar()}#"; if (sp.IsOpen) { byte[] datas = Encoding.ASCII.GetBytes(msg); sp.Write(datas, 0, datas.Length); } } }); } private void outLevel_CheckedChanged(object sender, EventArgs e) { Task.Run(() => { string msg = ""; RadioButton rbtn = (RadioButton)sender; if (rbtn.Checked) { msg = $"$GR={rbtn.Name.GetLastChar()}#"; if (sp.IsOpen) { byte[] datas = Encoding.ASCII.GetBytes(msg); sp.Write(datas, 0, datas.Length); } } }); } private void btnClose_Click(object sender, EventArgs e) { try { if (sp != null && sp.IsOpen) { sp.Close(); txtRunResult.AppendText("serial closed!" + Environment.NewLine); } } catch (Exception) { } } protected override void OnClosed(EventArgs e) { if (sp != null && sp.IsOpen) { sp.Close(); txtRunResult.AppendText("serial closed!" + Environment.NewLine); } base.OnClosed(e); } private void LightConfig_Load(object sender, EventArgs e) { string[] ionames = System.IO.Ports.SerialPort.GetPortNames(); if (ionames != null && ionames.Length > 0) { foreach (var item in ionames) { comboBox1.Items.Add(item); } comboBox1.SelectedIndex = 0; } } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (sp != null && sp.IsOpen) { sp.Close(); } sp.PortName = comboBox1.SelectedItem.ToString(); } private void btnClose_Click_1(object sender, EventArgs e) { if (sp != null && sp.IsOpen) { sp.Close(); } } private void LightConfig_FormClosing(object sender, FormClosingEventArgs e) { if (sp != null && sp.IsOpen) { sp.Close(); } } } }