树莓派4B 串口通信

Posted lecheng001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树莓派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

以上是关于树莓派4B 串口通信的主要内容,如果未能解决你的问题,请参考以下文章

树莓派串口通信

树莓派串口通信

树莓派4b串口(UART)使用问题的解决方法 - Ubuntu 21.04

36 树莓派串口通信

树莓派使用uart串口与串口设备通信

树莓派3b 串口通信初次尝试