树莓派4B开启串口及TTL转485通信功能

xingyun86 2022-8-9 544

树莓派4B开启串口功能

1.查看

ls -l /dev


2.修改系统配置项

sudo vi /boot/config.txt
#尾部添加三行代码(PI4B也是一样)
enable_uart=1
dtoverlay=pi3-miniuart-bt
force_turbo=1

3.修改系统启动

###禁用系统服务
$sudo systemctl stop serial-getty@ttyAMA0.service
$sudo systemctl disable serial-getty@ttyAMA0.service

sudo vi /boot/cmdline.txt
###
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

删掉 console=serial0,115200,剩下的内容(根据实际剩余即可)类似如下:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
【或】(下面办法仅适用于固定波特率)
修改console=serial0,115200->console=ttyAMA0,115200,剩下的内容(根据实际剩余即可)类似如下:
dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait

4.wiringPi库demo测试:(-lwiringPi)

// SerialTest.h : Include file for standard system include files,
// or project specific include files.
#pragma once
#include <iostream>
#include <vector>
#include <string>
// TODO: Reference additional headers your program requires here.
#include "SerialTest.h"
#include <stdio.h>
#include <unistd.h>
#include <wiringPi.h>
#include <wiringSerial.h>
size_t serialWrite(int fd, const void* data, size_t size)
{
    return write(fd, data, size);
}
size_t serialRead(int fd, void* data, size_t size)
{
    return read(fd, data, size);
}
int main()
{
    int i = 0;
    int fd;
    if (wiringPiSetup() < 0) {
        printf("wiringPiSetup error\n");
        return 1;
    }
    //if((fd=serialOpen("/dev/ttyS0",115200))<0) { // gpio 使用mini串口
    if ((fd = serialOpen("/dev/ttyAMA0", 115200)) < 0) { // gpio 使用硬件串口
        printf("serialOpen error\n");
        return 1;
    }
    printf("serial test output ...\n");
    //serialPrintf(fd, "1234567890abcdef\r\n");
    //serialPuts(fd, "Hello World!");
    //serialPuts(fd, "\x80\x08\x80\x50\x00\x00\x01\x58");
    std::vector<uint8_t> vChars = {
        (uint8_t)0xC0,(uint8_t)0x08,(uint8_t)0x80,
        (uint8_t)0x50,(uint8_t)0x00,(uint8_t)0x00,
        (uint8_t)0x58,(uint8_t)0x01 };
    serialWrite(fd, vChars.data(), vChars.size());
    for (;;)
    {
        delay(100);
        int len = serialDataAvail(fd);
        if (len > 0)
        {
            printf("%d\n", len);
            std::vector<uint8_t> vInChars(len, 0x00);
            serialRead(fd, vInChars.data(), len);
            for (auto & it : vInChars)
            {
                printf("%02X ", it);
            }
            printf("\n");
            //for (i = 0; i < vInChars.size(); i++)
            //{
            //    serialPutchar(fd, vInChars[i]);
            //}
            delay(1000);
        }
        else
        {
            //printf("len=%d\n", len);
            serialWrite(fd, vChars.data(), vChars.size());
        }
    }
    serialClose(fd);
    return 0;
}

使用USB转TTL串口模块或者TTL转485.我这里用的是TTL转485,公司内部做的4G控制板留出接口是485:

接线:

上引脚图,虽然型号可能不一样,但是方向是一致的,由于给树莓派4B接了风扇,所以4,6引脚被占用,依次连接:

转接板【VCC】 到 树莓派 2 引脚(实测,这个接了反而无法收数据)

转接板【RXD】 到 树莓派 8 引脚

转接板【TXD】 到 树莓派10 引脚

转接板【GND】 到 树莓派14引脚(实测,这个接了反而无法收数据)

转接板【接大地】  到 4G控制板的485-GND

转接板【B-】        到 4G控制板的485-B

转接板【A+】       到 4G控制板的485-A



×
打赏作者
最新回复 (0)
只看楼主
全部楼主
返回