C ++的Linux(Ubuntu的)写入串行正确(对于Arduino的) [英] C++ Linux (Ubuntu) Writing to Serial Properly (For Arduino)

查看:433
本文介绍了C ++的Linux(Ubuntu的)写入串行正确(对于Arduino的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有与高效串行设备进行通信的标准方式。我应该使用标准库?如果是这样,哪一个?

现在我正在摆弄周围得到一个LED在基于号码输入一定量点亮。 (Arduino的code以下)。只是练习的东西。

请参阅我的过于简单,低效的测试:

 的#include<&iostream的GT;
#包括LT&;&stdio.h中GT;
使用命名空间std;INT
主要()
{
  FILE *文件;
  //打开设备文件  INT getnum;  而(真)
    {
      文件= FOPEN(的/ dev / ttyACM5,W);
      COUT<< >>中<< ENDL;
      CIN>> getnum;
      fprintf中(文件%D,getnum); //写入文件
      FCLOSE(文件);
    }}

如果允许无需等待用户运行while循环很可爱,但几乎没有效率。我怀疑冗余的fopen FCLOSE 的使用是愚蠢的。

微控制器将是设备的检测状态和计算机发送信号。计算机将做啃所有这些值,并发送消息回改变的Arduino的行为。基本上重思想被委托给计算机,除了需要人为键盘输入。

当然,这一切都是为了好玩,但你可以看到我需要学习规则连环互动的C ++! 任何帮助或指导,大大AP preciated。


Arduino的code:

 字符incomingByte = 0; //传入的串行数据
INT领导= 11;
INT亮;
空虚
建立()
{
  Serial.begin(9600); //打开串口,设置数据传输速率为9600 bps}空虚
循环()
{  //发送数据,只有当您收到数据:
  如果(Serial.available()大于0)
    {
      //读取传入字节:
      incomingByte = Serial.read();      开关(incomingByte)
        {
      情况1':
        明亮= 10;
        打破;
      案2:
        明亮= 50;
        打破;
      案3:
        明亮= 255;
        打破;      默认:
        明亮= 0;
        打破;
        }      analogWrite(LED,亮);      Serial.println(incomingByte);
    }
}


解决方案

我不知道为什么没有人回答了这么久这个问题。你为什么说这是一种低效的方式?如果你指的是,这不是在文件系统中创建一个物理文件。这实际上是做了正确的方式,就是不要打开和关闭循环内的文件描述符。如果你想Serial.read读取单个值发送的'\\ n',fprint(文件%d个\\ N,值)

I'd like to know if there is a standard way to communicate with the serial device that is efficient. Should I be using a standard library? If so, which one?

Right now I'm fiddling around getting an LED to light up at a given amount based on number input. (Arduino code below). Just practice stuff.

See my overly simple and inefficient test:

#include <iostream>
#include <stdio.h>
using namespace std;

int
main()
{
  FILE *file;
  //Opening device file

  int getnum;

  while (true)
    {
      file = fopen("/dev/ttyACM5", "w");
      cout << ">>" << endl;
      cin >> getnum;
      fprintf(file, "%d", getnum); //Writing to the file
      fclose(file);
    }

}

The while loop is cute, but hardly efficient if allowed to run without waiting for the user. I suspect the redundant fopen fclose use is stupid.

The microcontroller is going to be sensing states of a device and sending signals to the computer. The computer will do the "crunching" of all of these values, and sending messages back to alter the arduino's behavior. Basically the heavy thinking is being delegated to the computer, besides requiring human keyboard input.

Of course this is all for fun, but as you can see I need to "learn the rules" of serial interaction in C++! Any help or guidance greatly appreciated.


The arduino code:

char incomingByte = 0;   // for incoming serial data
int led = 11;
int bright;
void
setup()
{
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps

}

void
loop()
{

  // send data only when you receive data:
  if (Serial.available() > 0)
    {
      // read the incoming byte:
      incomingByte = Serial.read();

      switch (incomingByte)
        {
      case '1':
        bright = 10;
        break;
      case '2':
        bright = 50;
        break;
      case '3':
        bright = 255;
        break;

      default:
        bright = 0;
        break;
        }

      analogWrite(led, bright);

      Serial.println(incomingByte);
    }
}

解决方案

I wonder why nobody answered this question for so long. Why are you saying this is an inefficient way? It isn't creating a physical file in the filesystem if you're referring to that. This is actually the right way to do it, just don't open and close the file descriptor inside the loop. If you want Serial.read to read a single value send '\n', fprint (file, "%d\n", value).

这篇关于C ++的Linux(Ubuntu的)写入串行正确(对于Arduino的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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