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

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

问题描述

我想知道是否有一种标准的方式可以有效地与串行设备进行通信.我应该使用标准库吗?如果有,是哪一个?

现在我正在摆弄让 LED 根据数字输入以给定数量点亮.(下面的Arduino代码).只是练习东西.

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

}

while 循环很可爱,但如果允许在不等待用户的情况下运行,则效率很低.我怀疑多余的 fopen fclose 使用是愚蠢的.

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.

微控制器将检测设备的状态并向计算机发送信号.计算机将对所有这些值进行处理",并将消息发送回以改变 arduino 的行为.除了需要人工键盘输入之外,基本上繁重的思考都被委托给了计算机.

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.

当然这只是为了好玩,但正如你所看到的,我需要学习 C++ 中串行交互的规则"!非常感谢任何帮助或指导.

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.

arduino 代码:

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

推荐答案

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

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天全站免登陆