因为Raspberry Zero的串口不什么好用,我采用了微雪的Serial_Expansion_HAT串口拓展板,我使用C#编写用例,基于Mono框架运行在Raspbian上。
Serial_Expansion_HAT的介绍及安装教程
测试用例
1.串口连接我按照官方的测试用例,把TXDA连接至RXDB,RXDA连接至TXDB。
2.测试代码
新建一个.net framework 4.72框架的C#控制台程序 ,项目名为ConsoleApp6,在Program.cs输入代码
using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
namespace ConsoleApp6
{
class Program
{
static void Main(string[] args)
{
Program program = new Program();
program.Test();
}
public void Test()
{
using (SerialPort sendSP = new SerialPort(portName: "/dev/ttySC0"))
{
sendSP.BaudRate = 115200;
sendSP.Encoding = Encoding.UTF8;
sendSP.ReadTimeout = 500;
sendSP.WriteTimeout = 500;
sendSP.Open();
using (SerialPort receiveSP = new SerialPort(portName: "/dev/ttySC1"))
{
receiveSP.BaudRate = 115200;
receiveSP.Encoding = Encoding.UTF8;
receiveSP.ReadTimeout = 500;
receiveSP.WriteTimeout = 500;
receiveSP.Open();
for (int i = 0; i < 10000; i++)
{
Thread.Sleep(100);
sendSP.WriteLine($"I Love you,Mum {i}!");
Console.WriteLine($"receiveSP receive: {receiveSP.ReadLine()}");
}
receiveSP.Close();
}
sendSP.Close();
}
}
}
}
保存,生成,上传树莓派
运行程序
mono ConsoleApp6.exe
大功告成!今天母亲节,祝愿天下母亲身体健康,万事如意!