完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近在测超声波传感器(HC-SR04),已经通过BBB的PRU测试过。现在手上有个arduino UNO板,想利用arduino测传感器,BBB通过UART4实现串口通信获取数据,两边的程序都编译成功并能实现通信,但获取的数据根本不对,还出现乱码,希望有高手指点。以下是我编程的程序和结果:BBB上的程序:
#include #include #include #include #include int main() { int file, count; if ((file = open("/dev/ttyO4", O_RDWR | O_NOCTTY | O_NDELAY))<0) { perror("UART: Failed to open the file. n"); return -1; } struct termios options; // the termios structure is vital tcgetattr(file, &options); // sets the parameters associated with file /*Set up the communications options: 9600 baud, 8-bit, enable receiver, no modem control lines */ options.c_cflag = B9600 | CS8 | CREAD | CLOCAL; options.c_iflag = IGNPAR | ICRNL; // ignore partity errors, CR -> newline tcflush(file, TCIFLUSH); // discard file information not transmitted tcsetattr(file, TCSANOW, &options); // changes occur immmediately unsigned char transmit[18] = "Hello BeagleBone!"; // the string to send if ((count = write(file, &transmit,18))<0) { perror("Failed to write to the outputn"); return -1; } usleep(100000); // give the Arduino a chance to respond unsigned char receive[100]; int i=0; //float c; while(i++ < 20){ if ((count = read(file, (void*)receive, 100))<0) // receive the data { perror("Failed to read from the inputn"); return -1; } if (count==0) printf("There was no data available to read! n"); else { printf("The distance is: %s cmn",receive); } //c=atof(receive); // printf("c= %fn",c); sleep(1); } close(file); return 0; } arduino上的程序: #define TrigPin 2 //引脚Tring 连接 IO D2 #define EchoPin 3 //引脚Echo 连接 IO D3 float cm; void setup() { Serial.begin(9600); pinMode(TrigPin, OUTPUT); pinMode(EchoPin, INPUT); } void loop() { //发一个10ms的高脉冲去触发TrigPin digitalWrite(TrigPin, LOW); delayMicroseconds(2); digitalWrite(TrigPin, HIGH); delayMicroseconds(10); digitalWrite(TrigPin, LOW); cm = pulseIn(EchoPin, HIGH) / 58.0; //算成厘米 cm = (int(cm * 100.0)) / 100.0; //保留两位小数 Serial.print(cm); //Serial.print("cm"); Serial.println(); delay(100); }
|
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
165个成员聚集在这个小组
加入小组BBB通过sd卡启动ubuntu16.04后不能升级下载联网的问题
4406 浏览 2 评论
3176 浏览 0 评论
7492 浏览 5 评论
330浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-18 17:30 , Processed in 0.772501 second(s), Total 75, Slave 57 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号