樹莓派串口通信簡記

1 系統(tǒng)安裝

這里是安裝Ubuntu mate 16.04,下載地址:
https://ubuntu-mate.org/download/
可以在Windows下使用Win32DiskImager將鏡像燒寫到sd卡中,工具下載地址:
https://sourceforge.net/projects/win32diskimager/files/latest/download
燒寫完成后,插入樹莓派中上電,完成初次安裝的一些配置。

2 配置串口

在命令行中運行: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)默認安裝。

#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

打開串口后,運行樹莓派端測試程序,即可看到字符串信息。
wiriingPi 相關(guān)庫函數(shù)接口介紹可參考:
http://www.cnblogs.com/lulipro/p/5992172.html

如果對您有所幫助請給在下點個贊

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容