码迷,mamicode.com
首页 > 其他好文 > 详细

树莓派4B 串口通信

时间:2019-12-24 11:59:23      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:toggle   too   sign   label   tran   obj   发送   ring   top   

  • 提前下载安装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

树莓派4B 串口通信

标签:toggle   too   sign   label   tran   obj   发送   ring   top   

原文地址:https://www.cnblogs.com/lecheng001/p/12090269.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!