PC通过RS422与树莓派 Zero进行串口通信

硬件连接

整个连接图

现在要弄个东西需要树莓派Zero进行rs422串口通信,连接图如下
在这里插入图片描述
先使用Serial_Expansion_HAT拓展板,通过I2C扩展两路UART通道,这两路通道都是TTL接口,要进行RS422通信,需在增加TTL转RS422模块,PC端使用USB转串口(三合一)模块。
在这里插入图片描述

TTL转RS422模块的连接

模块4P端子侧:
VCC <<>> +5V / +3.3V (模块规格)
TXD <<>> 本模块的TTL(UART)发送,接单片机的 RXD 接收脚
RXD <<>> 本模块的TTL(UART)接收,接单片机的 TXD 发送脚
GND <<>> 本模块地线(此地线必须与单片机系统的地相连)

模块5P端子侧:
R+/A <<>> 本模块的A脚,需接其他422模块的 T+/Y 脚
R-/B <<>> 本模块的B脚,需接其他422模块的 T-/Z 脚
T-/Z <<>> 本模块的Z脚,需接其他422模块的 R-/B 脚
T+/Y <<>> 本模块的Y脚,需接其他422模块的 R+/A 脚
GND <<>> 本模块地线,部分条件允许的情况下可以引出地线作为信号的共模信号的回流路径,当然此线在条件不允许的情况下可以不使用。

代码编写

树莓派代码

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp8
{
    class Program
    {
        SerialPort serialPort;

        public Program()
        {
            serialPort = new SerialPort(portName: "/dev/ttySC0");
            serialPort.BaudRate = 115200; //波特率
            serialPort.Parity = Parity.None ; //校验位
            serialPort.DataBits = 8; //数据位
            serialPort.StopBits = StopBits.One ; //停止位
            serialPort.Encoding = Encoding.UTF8 ;
            serialPort.ReadTimeout = 500;
            serialPort.WriteTimeout = 500;

            serialPort.Close();
            serialPort.Open();
            Console.WriteLine("串口已打开");
        }

        static void Main(string[] args)
        {
            
            Program program = new Program();

            Thread thread1 = new Thread(program.ReceiveDatas);
            thread1.Start();
            

            Thread.Sleep(Timeout.InfiniteTimeSpan);
        }
     

        void ReceiveDatas()
        {
            Console.WriteLine("串口开始接收");
            if (serialPort.IsOpen)
                Console.WriteLine("ReadThread(), sp Opened.");
            while (true)
            {

                try
                {
					Thread.Sleep(1);//非常重要,没有这句非常耗CPU
                    
                    if (serialPort.BytesToRead > 0)
                    {


                        string str = serialPort.ReadLine();

                        Console.WriteLine(str);
                        serialPort.WriteLine(str);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

            }

        }



    }
}

PC端代码

using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp9
{
    class Program
    {
        SerialPort serialPort;
        static void Main(string[] args)
        {
            Program program = new Program();

            Thread thread1 = new Thread(program.ReceiveDatas);
            thread1.Start();
            while (true)
            {
                string input = Console.ReadLine();
                switch (input)
                {
                    case "a":
                        Thread thread = new Thread(program.SendDatas);
                        thread.Start();
                        break;
                    default:
                        break;

                }
            }

        }

        public Program()
        {
            serialPort = new SerialPort(portName: "COM5");
            serialPort.BaudRate = 115200; //波特率
            serialPort.Parity = Parity.None; //校验位
            serialPort.DataBits = 8; //数据位
            serialPort.StopBits = StopBits.One; //停止位
            serialPort.Encoding = Encoding.UTF8;
            serialPort.ReadTimeout = 500;
            serialPort.WriteTimeout = 500;

            serialPort.Close();
            serialPort.Open();
            Console.WriteLine("串口已打开");
        }

        void SendDatas()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(100);
                string str = $"I Love you,Mum {i}!";
                serialPort.WriteLine(str);
                Console.WriteLine("Send:"+str);
            }
        }

        

        void ReceiveDatas()
        {
            Console.WriteLine("串口开始接收");
            if (serialPort.IsOpen)
                Console.WriteLine("ReadThread(), sp Opened.");
            while (true)
            {

                try
                {
                	Thread.Sleep(1);//非常重要,没有这句非常耗CPU
                    if (serialPort.BytesToRead > 0)
                    {
                        

                        string str = serialPort.ReadLine();

                        Console.WriteLine("Receive:" + str);
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

            }

        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值