树莓派4B 串口通信
- 提前下载安装Glade图形编辑器
- 参考 树莓派4B安装netcore 环境部署、发布、执行操作
- 准备串口设备本文使用串口控制继电器设备 如图
.jpg)
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串口测试与开发
参考文档: https://shumeipai.nxez.com/2021/08/09/raspberry-pi-4-activating-additional-uart-ports.html 树莓派 ...
- 树莓派3b 串口通信初次尝试
准备工作: 1. 安装wiringPi 2. 修改config.txt sudo nano /boot/config.txt 在文件的末尾添加: dtoverlay=pi3-miniuart-bt 3 ...
- 树莓派和STM32通过USB和串口通信记录
不管怎样,为了简便开发,通信选择串口通信. 推荐文章:https://blog.csdn.net/magnetoooo/article/details/53564797 推荐测试工具:https:// ...
- Python 串口通信 GUI 开发
在项目中遇到树莓派串口通信问题.由于本人一直从事.net 开发,希望将树莓派系统换成Win10 IOT版.但是在测试过程中出现无法找到串口的问题.最终也没有解决.最终按照领导要求,linux (了解不 ...
- linux与开发板串口通信
研究了一天的linux串口,结果改了树莓派的系统配置文件config.txt给改了导致系统崩溃....其实我感觉网上的大多数方法都是不符合新版本树莓派的,网上的方法是通过修改系统配置文件后安装mini ...
- win10上使用php与python实现与arduino串口通信
注意: php 需要php7,安装及开启php_dio.dll com口按照实际的进行设置,如果不知道可以打开arduino编辑器进行查看 可以与用户实现命令行交互,但是效率过慢,不清楚如何优化,使用 ...
- 树莓派4B遇到的坑
由于大创需要用到机器学习这些东西,入手了一个树莓派4B(新手没弄过,直接上手最新版果然是有坑的),大佬勿喷
- Linux学习 - 树莓派4b的U-Boot的初识
Linux学习 - 树莓派4b的U-Boot的初识 初识U-Boot 学习书籍:<[正点原子]I.MX6U嵌入式Linux驱动开发指南V1.5.1> 章节:第三十章 学习内容: 书中介绍u ...
- .NET 串口通信
这段时间做了一个和硬件设备通信的小项目,涉及到扫描头.输送线.称重机.贴标机等硬件.和各设备之间通信使用的是串口或网络(Socket)的方式.扫描头和贴标机使用的网络通信,输送线和称重机使用的是串口通 ...
随机推荐
- 题解 最长上升子序列 LIS
最长上升子序列 LIS Description 给出一个 1 ∼ n (n ≤ 10^5) 的排列 P 求其最长上升子序列长度 Input 第一行一个正整数n,表示序列中整数个数: 第二行是空格隔开的 ...
- linux运维、架构之路-K8s数据管理
一.Volume介绍 容器和Pod是短暂的,它们的生命周期可能很短,会被频繁的销毁和创建,存在容器中的数据会被清除,为了持久化保存容器的数据,k8s提供了Volume.Volume的生命周期独立于容器 ...
- [Python之路] 实现简单Web服务器(TCP3次握手4次挥手解释)
一.如何使用Python实现一个返回固定页面的Web Server 1.使用socket创建一个TCP Server 2.接受来自浏览器的TCP链接,并接收HTTP请求 3.返回固定响应数据给浏览器 ...
- [Python]token的生成及验证
hmac模块(仅在python3中可以使用) 简介: HMAC是密钥相关的哈希运算消息认证码,HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出. 典型应用: HMAC的一 ...
- .net大文件上传断点续传解决方案
HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx. ...
- confirm 弹出信息
if(confirm("真的要删除吗?")){ alert("点击了确认按钮"); } else{ alert("点击了取消按钮"); } ...
- D. Make a Permutation!(思维)
D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 对象中 new this
构造函数 new后面调用函数,我们称为构造函数(首字母大写) Object() 我们把他视为一个构造函数,构造函数的本质就是一个函数,只不过构造函数的目的是为了创建新对象,为新对象进行初始化(设置对象 ...
- Hive使用与安装步骤
1.Hive安装与配置 Hive官网:https://hive.apache.org/ 1. 安装文件下载 从Apache官网下载安装文件 http://mirror.bit.edu.cn/apach ...
- 在ubuntu16.04-32bits 下编译vlc和vlc-qt开源项目
软件版本: Ubuntu14.04 32位 Qt5.4.0 32位 开源项目: vlc2.2.4: wget http://download.videolan.org/pub/v ...