准備工作:
1. 安裝wiringPi
2. 修改config.txt
sudo nano /boot/config.txt
在文件的末尾添加:
dtoverlay=pi3-miniuart-bt
3. 修改cmdline.txt
sudo nano /boot/cmdline.txt 刪除其中紅色部分: dwc_otg.lpm_enable=0 console=serial10,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4
elevator=deadline fsck.repair=yes rootwait
編寫程序的注意事項:
1. char* 、const char*、字符串之間的轉換導致編譯錯誤
解決方案:
char * str = (char*) "/dev/ttyUSB0";
2. C++編譯報錯
undefined reference to `std::basic_ostream<char, std::char_traits<char>
解決方案:
編譯時添加 -lstdc++
編譯程序:
gcc -o test test.cpp -lwiringPi -lstdc++