1 系統(tǒng)安裝
這里是安裝Ubuntu mate 16.04霍骄,下載地址:
https://ubuntu-mate.org/download/
可以在Windows下使用Win32DiskImager將鏡像燒寫到sd卡中沉桌,工具下載地址:
https://sourceforge.net/projects/win32diskimager/files/latest/download
燒寫完成后,插入樹莓派中上電,完成初次安裝的一些配置逛尚。
2 配置串口
在命令行中運(yùn)行:sudo raspi-config
選擇 Interfacing Options搪花,然后選擇Serial,關(guān)閉串口shell終端薯嗤,使能串口顽爹。
完成后退出重啟樹莓派。完成重啟后cmdline.txt中的參數(shù)如下:
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
查看config.txt.最后為:enable_uart=1
3 程序簡例
這里使用wiringPi庫骆姐,在Ubuntu mate中該庫已經(jīng)默認(rèn)安裝镜粤。
#include <wiringPi.h>
#include <wiringSerial.h>
#include <stdio.h>
#include <unistd.h>
#define COMPORT "/dev/ttyS0"
#define COUNT 10
int main(void)
{
int fd;
if(wiringPiSetupSys() < 0)
{
printf("init sys fail\n");
return 1;
}
if((fd = serialOpen(COMPORT,9600)) < 0 )
{
printf("open serial fail\n");
return 1;
}
int count = 0;
while( count < COUNT)
{
serialPuts(fd,"123");
sleep(1);
count ++;
}
serialClose(fd);
}
以上程序是一直發(fā)送字符串“123”,需要電平轉(zhuǎn)換模塊轉(zhuǎn)為TTL電平后再連接到PC串口玻褪。在PC端通過串口調(diào)試助手肉渴,設(shè)置如下:
image.png
打開串口后,運(yùn)行樹莓派端測試程序带射,即可看到字符串信息同规。
wiriingPi 相關(guān)庫函數(shù)接口介紹可參考:
http://www.cnblogs.com/lulipro/p/5992172.html