mattraynor 2019-12-24
void GetSerialPort() { Console.WriteLine("发现串口:"); var names = SerialPort.GetPortNames(); int count = 1; foreach (var item in names) { ports.Add(count.ToString(), item); Console.WriteLine($"{count.ToString()}:{item}"); count++; } Console.WriteLine("输入要打开的串口编号:" + ports.Keys.Join(",")); string portName = ports["2"]; _SerialPort = new SerialPort(portName, 9600, Parity.None, 8, StopBits.One); _SerialPort.Open(); }
void SendCommand(string command) { Console.WriteLine(command); var buf = command.ToHex(); if (_SerialPort != null && _SerialPort.IsOpen) _SerialPort.Write(buf, 0, buf.Length); }
<?xml version="1.0" encoding="UTF-8"?> <interface> <requires lib="gtk+" version="2.24"/> <!-- interface-naming-policy project-wide --> <object class="GtkWindow" id="MainWindow"> <property name="can_focus">False</property> <property name="title" translatable="yes">继电器控制</property> <property name="window_position">center</property> <child> <object class="GtkVBox" id="vbox1"> <property name="visible">True</property> <property name="can_focus">False</property> <child> </child> <child> <object class="GtkHBox" id="hbox1"> <property name="visible">True</property> <property name="can_focus">False</property> <child> <object class="GtkToggleButton" id="btnOne"> <property name="label" translatable="yes">继电器1</property> <property name="visible">True</property> <property name="can_focus">False</property> <property name="receives_default">True</property> <property name="tooltip_markup">1</property> <signal name="toggled" handler="on_togglebutton1_toggled" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">0</property> </packing> </child> <child> <object class="GtkToggleButton" id="btnTwo"> <property name="label" translatable="yes">继电器2</property> <property name="visible">True</property> <property name="can_focus">False</property> <property name="receives_default">True</property> <property name="tooltip_markup">2</property> <signal name="toggled" handler="on_togglebutton1_toggled" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> <child> <object class="GtkToggleButton" id="btnThree"> <property name="label" translatable="yes">继电器3</property> <property name="visible">True</property> <property name="can_focus">False</property> <property name="tooltip_markup">3</property> <property name="receives_default">True</property> <signal name="toggled" handler="on_togglebutton1_toggled" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">2</property> </packing> </child> <child> <object class="GtkToggleButton" id="btnFour"> <property name="label" translatable="yes">继电器4</property> <property name="visible">True</property> <property name="can_focus">False</property> <property name="receives_default">True</property> <property name="tooltip_markup">4</property> <signal name="toggled" handler="on_togglebutton1_toggled" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">3</property> </packing> </child> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">1</property> </packing> </child> <child> <object class="GtkToggleButton" id="togglebutton1"> <property name="label" translatable="yes">全部打开</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="tooltip_markup">0</property> <signal name="toggled" handler="on_togglebutton1_toggled" swapped="no"/> </object> <packing> <property name="expand">True</property> <property name="fill">True</property> <property name="position">2</property> </packing> </child> </object> </child> </object> </interface>
源码下载 code.rar
背景在工业上,当设备之间不能通过网络联通时,通常设备上有串口可以用于传输数据,该系统是利用设备的串口来达到传输文件的目的.enum {REQ = 1, DATA, ACK, FINISH, ERR};