Raspberry Pi Serial/rs485 Communication

Source: Internet
Author: User
Tags vmin

Raspberry Pi native Serial port default for console output, if you want to use in your own program need to shut down the system to occupy this serial port.

The method is configured for Rasp-config, or directly modifies the/boot/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

After the modification will be more than a serial port device file/dev/ttyama0, you can use the command stty-f/dev/ttyama0 9600 set the serial port parameters, etc.

Can be installed under the command line debugging software minicom for serial port setup and data transceiver

sudo apt-get install minicom

If the desktop system is installed, more graphical debugging tools such as Cutecom

System comes with the serial port is the TTL level, if you want to communicate with the RS485 device, need to connect a 485 conversion chip, ready-made can be directly inserted on the board for the link


can buy empty plate to buy their own components welding or buy welding ready-made. Board on the chip for ADM2587, chip-level power supply and signal are isolated 485 chip, the cost is not low, but for the motor and other interference large and go outside the security requirements of high-quality recommendations with isolation. To avoid system instability or burn out the motherboard

can also directly with the USB to 485 small module, suitable for debugging and so on is not 24-hour use of the occasion. USB is often unstable, and the module does not signal isolation.


Then do not install the driver, automatically will be more than a serial device file/dev/ttyusb0

Usage and programming as in the previous article

Since the two modules are self-contained transceiver automatic switching, if you do not have to change the control of a di/ri pin, of course, if the baud rate is high, it is best to manually control. Gpio control can use the WRINGPI library, while the serial port program can also use WRINGPI this library to write

Serial communication in various languages:

C language, using the WRINGPI library, a large number of online code calls to the LINUX API are not introduced

#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 Installation pyserial Module


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



echo "123" >/dev/ttyama0


PHP: Installing extensions



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


* 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 is 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 a 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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.