写入后从串行端口读取 [英] Reading from a serial port after writing on it

查看:25
本文介绍了写入后从串行端口读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开展一个项目,该项目让我的计算机与 arduino 板进行通信,该板读取传感器输出并仅在收到t"时将其放在串行端口上.如下所示的 arduino 代码正在运行.

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);
    }
  }
}

当我试图读取 arduino 在串行端口上放置的内容时,我的问题出在 C 代码中.我的 C 代码是:

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 总是=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

一个大问题是计算机"上运行的C程序不完整.

One big problem is that the C program running on the "computer" is incomplete.

Arduino 的程序会进行至少波特率的串行端口设置(以及默认情况下可能执行的任何其他操作).
但是计算机的"C 程序从未正确配置串行端口.串行端口将使用之前配置的任何属性(波特率、数据长度、奇偶校验设置、规范与原始模式),这将导致不可预测的读取和写入.(环回测试可能会产生假阳性结果.)

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.)

使用 POSIX 串行端口指南此答案 示例代码.

Use the POSIX Serial Port guide or this answer for sample code.

对于规范模式,您可能需要添加如下代码(假设为 8N1):

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 */
    }

您可能应该删除该行

fcntl(fd, F_SETFL, FNDELAY);  

以及 open() 调用中的 O_NONBLOCK 选项.

as well as the O_NONBLOCK option in the open() call.

这篇关于写入后从串行端口读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆