树莓派串口/RS485通信

    本文地址:http://www.tongxinmao.com/Article/Detail/id/330

    树莓派原生串口默认用于控制台输出,如果想在自己程序里使用需要先关闭系统占用此串口。

    方法为rasp-config 里配置,或直接修改cmdline.txt

    #dwc_otg.lpm_enable=0 console=tty1 console=serial0,115200 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwai

    dwc_otg.lpm_enable=0 console=tty1  root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwai


    修改后就会多出一个串口设备文件/dev/ttyAMA0, 可以用命令stty -F /dev/ttyAMA0   921600 设置串口参数等

    可以安装命令行下调试软件minicom进行串口设置及数据收发

    sudo apt-get install minicom


    系统自带的串口是TTL电平,如果要和RS485设备通信,需要接一块485转换芯片,现成的可以直接插在板子上用的见链接

    https://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-951511554.15.23a61349h5N5xG&id=563656341277


    可以买空板自己买元件焊接或买焊好现成的。板子上的芯片为ADM2587 ,芯片级电源和信号都隔离的485芯片,成本不低,但对于电机等干扰大及走室外的对安全要求高的建议用隔离的。以免系统不稳定或烧坏主板


    也可以直接用USB转485小模块,适合于调试等不是24小时用的场合。USB经常不稳定,同时模块也没有做信号隔离

    https://item.taobao.com/item.htm?spm=a1z10.3-c.w4002-951511554.15.23a61349h5N5xG&id=563656341277

    接上去不用装驱动,自动会多出一个串口设备文件 /dev/ttyUSB0

    用法和编程与前文一样


    由于最两款模块都自带收发控制自动切换,如果不用改程序控制一个DI/RI引脚,当然如果波特率很高,最好还是手动控制。GPIO的控制可以使用wringpi 库,同时串口程序也可以使用wringpi 这个库来写



    各种语言的串口通信:


    C语言,使用wringpi库,调用 LINUX api的网上大量代码就不介绍了

    #include <wiringSerial.h>

    int main(void)

    {

        int fd;

        if((fd = serialOpen ("/dev/ttyAMA0",9600))<0)

        {

            printf("serial err\n");

        }

        while(1)

        {

            UartBuff[0]=serialGetchar(fd);

            if(UartBuff[0]=='a')

            {

             serialPutchar(fd,UartBuff[0]);

            }

            sleep(0.5);

        }

        return EXIT_SUCCESS;

     }



    PYTHON 安装pyserial 模块

    http://www.cnblogs.com/starspace/archive/2009/02/20/1394603.html

    >>> ser = serial.Serial('/dev/ttyS1', 19200, timeout=1)
    >>> x = ser.read()          # read one byte
    >>> s = ser.read(10)        # read up to ten bytes (timeout)
    >>> line = ser.readline()   # read a '\n' terminated line
    >>> ser.close()


    shell:

    echo "123" > /dev/ttyAMA0

    cat /dev/ttyAMA0


    PHP: 安装扩展

    https://github.com/oasynnoum/phpmake_serialport

     <?php

        use PHPMake\SerialPort;

        // $device = 'COM4'; // on Windows

        $device = '/dev/ttyUSB0'; // on Linux 


        /*

         * create new instance

         */

        $port = new SerialPort();


        try {

            /* 

             * open the port

             */

            $port->open($device);

        

            /*

             * configure baud rate

             *

             * you can specify baud rate as integer, 

             * or other class constants like SerialPort::BAUD_RATE_*

             */

            $port->setBaudRate(SerialPort::BAUD_RATE_9600);

        

            /*

             * configure flow control

             * 

             * any other options are below.

             * SerialPort::FLOW_CONTROL_SOFT is software flow control.

             * SerialPort::FLOW_CONTROL_HARD is hardware flow control.

             */

            $port->setFlowControl(SerialPort::FLOW_CONTROL_NONE);

        

            /*

             * configure canonical mode

             * 

             * canonical mode is for text-based communication.

             * non-canonical mode is binary-safe.

             * more detail information about VMIN and VTIME, 

             * see http://www.unixwiz.net/techtips/termios-vmin-vtime.html

             */

            $port->setCanonical(false)

                    ->setVTime(1)->setVMin(0);

        

            /*

             * read data from port.

             * you can get size of actual read data with strlen($data) .

             */

            $data = $port->read(256);

        

            /*

             * send data.

             */

            $port->write($data);

        

        } catch (Exception $e) {

            print $e->getMessage() . PHP_EOL;

        }


        if ($port->isOpen()) $port->close();



    上一篇:商业设备清单
    下一篇:5号停机坪 XZ-W680C usb描述符