树莓派4B 串口通信
- 提前下载安装Glade图形编辑器
- 参考 树莓派4B安装netcore 环境部署、发布、执行操作
- 准备串口设备本文使用串口控制继电器设备 如图
1、发现串口
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(); }
2、发送协议
void SendCommand(string command) { Console.WriteLine(command); var buf = command.ToHex(); if (_SerialPort != null && _SerialPort.IsOpen) _SerialPort.Write(buf, 0, buf.Length); }
3、Glade设计界面
<?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
相关推荐
YukiRain 2020-04-10
internetofthings 2020-01-29
FIllMood 2020-01-28
kidneybeans 2011-12-08
jessieHJ 2019-06-12
背景在工业上,当设备之间不能通过网络联通时,通常设备上有串口可以用于传输数据,该系统是利用设备的串口来达到传输文件的目的.enum {REQ = 1, DATA, ACK, FINISH, ERR};
anqier 2019-06-27
vincent 2011-02-13
liuqipao 2011-07-11
liushi 2011-07-13
zhjn0 2014-11-11
读点儿设计 2017-12-07
软件设计 2017-05-16