从Linux C ++ ifstream的Arduino的到 [英] C++ ifstream from linux to arduino

查看:280
本文介绍了从Linux C ++ ifstream的Arduino的到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原来的code

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
    ofstream arduino_output("/dev/ttyACM0");
    ifstream arduino_input("/dev/ttyACM0");

    int value;
    string txt;

    while(cin >> value){
        arduino_output << value << endl;
        arduino_input >> txt;//I never recieve the "OK" (Which I should get)
        cout << txt;
    }

    arduino_input.close();
    arduino_output.close();
    return(0);
} 

下面的问题是:

        cin >> value;
        arduino_output << value << endl;
        arduino_input >> txt;//I never recieve the "OK" (Which I should get)
        cout << txt;

但如果我这样做,而不是它的工作原理:

but if I do this instead it works:

        cin >> value;
        arduino_output << value << endl;

        for(int i=0;i<10000;++i)
        for(int j=0;j<10000;++j){ //Waste a lot of time
           ++value;
           --value;
        }

        arduino_input >> txt; //I always recieve the "OK"
        cout << txt; //I can see the "OK"

那么,如何让我的快速的电脑能够读取来自Arduino的输出慢? (不使用for循环浪费时间)

So how do I make my fast computer able to read the slow output from the arduino? (Without using for-loops to waste time)

这说,大约回调 http://www.cplusplus.com/一些事情参考/ IOS /的ios_bas​​e / register_callback / 但我永远无法得到它的工作。它说,它支持3个事件,其中没有一个是:如果输入缓冲区不是空的,调用这个函数。

Here it says some things about callback http://www.cplusplus.com/reference/ios/ios_base/register_callback/ but I could never get it to work. It says it supports 3 events, and none of them are: "If input buffer is not empty, call this function".

由于最终的解决办法是,只要输入缓冲区不是空的回调函数。

Because the ultimate solution would be a callback function for whenever the input buffer is not empty.

这是可接受的解决方案将是Arduino的版本Serial.available()C ++的等价版本。

An acceptable solution would be a c++ equivalent version of the arduino version "Serial.available()".

另一种可以接受的解决办法是什么,迫使我不依赖于两个for循环。 3 for循环是不能接受的,如果这是你在想什么。

Another acceptable solution would be anything that forces me to not rely on two for-loops. 3 for-loops is not acceptable if that's what you're thinking.

EDIT1:表现为原来的code结果
EDIT2:我使用的linux(lubuntu)结果
EDIT3:有人糊涂了,其中code编写的。奇怪的。

Showed the original code
I am using linux(lubuntu)
Someone got confused where the code was written. Strange.

推荐答案

您的问题是怪异。在一般情况下,问题是,慢方无法读取速度更快方发送什么。所以,看来你在这里有一个更根本的问题。

Your problem is weird. In general, the problem is that the slower party can't read what the faster party sends. So, it seems you have a more fundamental problem here.

如果 arduino_output 是一个串口(UART)的重新presentation,我建议使用访问它的特定于平台的方式。在Windows中,有UART功能,并且在Linux上有的termios (可能是大多数其他POSIX似的,太)。这会给你一个方法来控制通信的参数,并获得有关事件(包括奇偶校验/帧错误)信息和/或通知。

If arduino_output is a representation of a serial port (UART), I suggest to use a platform specific way of accessing it. On Windows, there are UART functions, and on Linux there's termios (probably on most other POSIX-like, too). This will give you a way to control the parameters of communication, and get information and/or notification about events (including parity/framing errors).

这篇关于从Linux C ++ ifstream的Arduino的到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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