Raspberry Pi 3 ~ C language Control serial

Source: Internet
Author: User

Annotated Source: http://www.cnblogs.com/einstein-2014731/p/5551846.html

The use of the C language Control Raspberry Pi 3B serial port, to achieve the purpose of using the serial port to send and receive data. Previously thought this serial port is the default to be formulated into a serial control Raspberry Pi mode,

In fact, after the installation of the latest noobs is the default serial0, and we use ttyAMA0, and some say this and Pi3 on-board Bluetooth conflict, the two can not be used at the same time,

So far, we haven't tried both. If both cannot be used at the same time is not very pit father.

"1" First look at the/boot/overlays/directory there is no PI3-MINIUART-BT-OVERLAY.DTB this

File, if any, then the hardware serial port has been reassigned to the Gpio.

Then look at the next/boot/cmdline.txt this file, my content is as follows:


Otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.re Pair=yes rootwait

You can see that console=serial0 is using Serial0 control mode, not ttyAMA0. And don't change it here.

"2" Installation Wiringpi, previously mentioned, check the Gpio pin assignment after installation. The PIN diagram is as follows:

"3" according to the figure line, TXD is 8 feet connected to the MAX232 Rx Rxd on the TX, the TTL level to RS232 level so that the computer can recognize.

Writing code in "4" Eclipse

#include <wiringSerial.h>intMainvoid){    intFD; 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); }    returnexit_success;

First receives the data from the serial port, then if receives is the character a, then sends back the received a.

WIRINGPI about the serial function as follows, can be used according to requirements.

extern intSerialopen (Const Char*device,Const intbaud);extern voidSerialclose (Const intFD);extern voidSerialflush (Const intFD);extern voidSerialputchar (Const intFdConstUnsignedCharc);extern voidSerialputs (Const intFdConst Char*s);extern voidserialprintf (Const intFdConst Char*message, ...) ;extern intSerialdataavail (Const intFD);extern intSerialgetchar (Const intFD);

Raspberry Pi 3 ~ C language Control serial

Related Article

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.