它写入后从串口读取 [英] Reading from a serial port after writing on it
问题描述
我的工作有我的电脑项目,一个Arduino板,读取传感器输出通信,并把它只有一个T是received.the的Arduino code串行端口如下图所示工作
const int的inputPin = 0;
无效设置(){
Serial.begin(9600);
pinMode(13,输出);}空隙环(){
如果(Serial.available()大于0){
焦C = Serial.read();
如果(C =='T'){
int值= analogRead(inputPin);
浮摄氏度=(5.0 *值* 100.0)/1024.0;
Serial.println(摄氏);
}
}
}
我的问题是在C code当我试着读什么把Arduino的串行端口上。我的C code是:
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&errno.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&fcntl.h GT;诠释主(){
INT STATE_OK = 0;
INT STATE_WARNING = 1;
INT STATE_CRITICAL = 2;
烧焦tempbuf [10];
INT FD =打开(/开发/ ttyACM0,O_RDWR | O_NOCTTY | O_NONBLOCK);
如果(FD == -1){
的printf(无法打开/ dev / ttyACM0 \\ n);
返回STATE_WARNING;
}其他{
使用fcntl(fd,F_SETFL,FNDELAY);
INT W =写(FD,T,1);
输出(写入到串行端口的字节数为%d \\ n,w)的;
fprintf中(标准错误,FD =%d个\\ N,FD);
睡眠(10);
INT N =读(FD,tempbuf,5);
的printf(%D,%S \\ N,N,字符串错误(错误));
如果(N 0){
浮TEMP = ATOF(tempbuf);
的printf(温度为:%F摄氏\\ n,温度);
如果(临时> 27){
返回STATE_CRITICAL;
}其他{
输出(温度%F摄氏度,并检查10秒前\\ n,温度);
返回STATE_OK;
}
}
}
关闭(FD);
返回0;
}
n是始终= 0,我想不出有什么问题。
先谢谢了。
我也弄不清是什么问题。
块引用>的一个大问题是,电脑报上运行的C程序是不完整的。
Arduino的的程序做至少波特率的串行端口设置(以及其他任何可能被默认执行)。结果
但电脑的C程序的从不正确配置串口。串行端口将使用任何属性(波特率,数据长度,奇偶校验设置,规范与原始模式)previously配置,这将导致未predictable读取和写入。 (环回测试,可能会产生假阳性结果。)使用 POSIX串口指南或<一个href=\"http://stackoverflow.com/questions/12437593/how-to-read-a-binary-data-over-serial-terminal-in-c-program/12457195#12457195\">this回答样品code。
有关标准模式下,你可能需要增加code所示(假设8N1):
RC = tcgetattr(FD,&安培; TTY);
如果(RC℃,){
/ *处理错误* /
}
savetty = TTY; / * preserve为恢复原始设置* / SPD = B9600;
cfsetospeed来设置(安培; TTY(speed_t)SPD);
之后,cfsetispeed(安培; TTY(speed_t)SPD); tty.c_cflag&安培; =〜PARENB
tty.c_cflag&安培; =〜CSTOPB
tty.c_cflag&安培; =〜CSIZE;
tty.c_cflag | = CS8; tty.c_cflag&安培; =〜CRTSCTS; / *没有硬件流控制? * /
tty.c_cflag | = CLOCAL | CREAD; tty.c_iflag | = IGNPAR | IGNCR;
tty.c_iflag&安培; =〜(IXON | IXOFF | IXANY);
tty.c_lflag | = ICANON;
tty.c_oflag&安培; =〜OPOST; RC = tcsetattr(FD,TCSANOW,&安培; TTY);
如果(RC℃,){
/ *处理错误* /
}您或许应该删除行
的fcntl(FD,F_SETFL,FNDELAY);
,以及在
的open()
通话O_NONBLOCK
选项。I am working on a project that has my computer communicating with an arduino board that reads the sensor output and put it on the serial port only if a "t" was received.the arduino code as shown below is working.
const int inputPin = 0; void setup(){ Serial.begin(9600); pinMode(13, OUTPUT);} void loop(){ if (Serial.available() > 0){ char c=Serial.read(); if(c=='t'){ int value = analogRead(inputPin); float celsius = (5.0 * value * 100.0)/1024.0; Serial.println(celsius); } } }
My problem is in the C code when Im trying to read what arduino puts on the serial port. My C code is:
#include<string.h> #include<stdlib.h> #include<stdio.h> #include<errno.h> #include<unistd.h> #include<fcntl.h> int main(){ int STATE_OK=0; int STATE_WARNING=1; int STATE_CRITICAL=2; char tempbuf[10]; int fd=open("/dev/ttyACM0",O_RDWR | O_NOCTTY | O_NONBLOCK); if(fd == -1){ printf("Unable to open /dev/ttyACM0\n"); return STATE_WARNING; } else { fcntl(fd, F_SETFL, FNDELAY); int w=write(fd, "t", 1); printf("The number of bytes written to the serial port is %d \n",w); fprintf(stderr, "fd = %d.\n", fd); sleep(10); int n=read(fd,tempbuf,5); printf("%d,%s \n",n,strerror(errno)); if(n>0){ float temp=atof(tempbuf); printf("Temperature is: %f Celsius\n", temp); if (temp>27){ return STATE_CRITICAL; }else{ printf("The temperature is %f Celsius and checked 10 seconds ago\n",temp); return STATE_OK; } } } close(fd); return 0; }
n is always=0 and i can't figure out what is the problem. Thanks in advance.
解决方案i can't figure out what is the problem
One big problem is that the C program running on the "computer" is incomplete.
The Arduino's program does a serial port setup of at least the baud rate (and whatever else might be performed by default).
But the "computer's" C program never properly configures the serial port. The serial port will use whatever attributes (baud rate, data length, parity setting, canonical versus raw mode) previously configured, which will cause unpredictable reads and writes. (A loopback test would probably produce a false positive result.)Use the POSIX Serial Port guide or this answer for sample code.
For canonical mode you probably need to add code like (assuming 8N1):
rc = tcgetattr(fd, &tty); if (rc < 0) { /* handle error */ } savetty = tty; /* preserve original settings for restoration */ spd = B9600; cfsetospeed(&tty, (speed_t)spd); cfsetispeed(&tty, (speed_t)spd); tty.c_cflag &= ~PARENB tty.c_cflag &= ~CSTOPB tty.c_cflag &= ~CSIZE; tty.c_cflag |= CS8; tty.c_cflag &= ~CRTSCTS; /* no HW flow control? */ tty.c_cflag |= CLOCAL | CREAD; tty.c_iflag |= IGNPAR | IGNCR; tty.c_iflag &= ~(IXON | IXOFF | IXANY); tty.c_lflag |= ICANON; tty.c_oflag &= ~OPOST; rc = tcsetattr(fd, TCSANOW, &tty); if (rc < 0) { /* handle error */ }
You probably should delete the line
fcntl(fd, F_SETFL, FNDELAY);
as well as the
O_NONBLOCK
option in theopen()
call.这篇关于它写入后从串口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!