在树莓派编译运行
树莓派已经通过网络连接通过串口通信在串口调试小助手打印C
gcc xxx.c -lwiringPi
源码
#include <wiringSerial.h> #include <wiringPi.h> int main () { int fd; wiringPiSetup(); fd = serialOpen("/dev/ttyAMA0",9600); while (1) { serialPutchar(fd,'c'); delayMicrosecondes(1000000); } return 0; }
接线
网络异常,图片无法展示
|
打开串口调试小助手
电脑操作,在串口上打印liuzhihao 234
#include <wiringSerial.h> #include <wiringPi.h> #include <stdio.h> int main () { int fd; int cmd; wiringPiSetup(); fd = serialOpen("/dev/ttyAMA0",9600); while (1) { while(serialDataAvail(fd) != -1) { cmd = serialGetchar(fd); if(cmd == '2') { serialPuts(fd,"liuzhihao2\r\n"); } if(cmd == '3') { serialPuts(fd,"liuzhihao3\r\n"); } if(cmd == '4') { serialPuts(fd,"liuzhihao4\r\n"); } } } return 0; }
串口打印与输出
串口输入树莓派输出
#include <wiringSerial.h> #include <wiringPi.h> #include <stdio.h> int main () { int fd; int cmd; wiringPiSetup(); fd = serialOpen("/dev/ttyAMA0",9600); while (1) { while(serialDataAvail(fd) != -1) { cmd = serialGetchar(fd); printf("get data: cmd =%d\n",cmd); if(cmd == '2') { serialPuts(fd,"liuzhihao2\r\n"); } if(cmd == '3') { serialPuts(fd,"liuzhihao3\r\n"); } if(cmd == '4') { serialPuts(fd,"liuzhihao4\r\n"); } } } return 0; }