简体   繁体   English

C ++ Linux(Ubuntu)正确写入串行(对于Arduino)

[英]C++ Linux (Ubuntu) Writing to Serial Properly (For Arduino)

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. 现在,我正在根据数字输入摆弄LED以给定的数量点亮。 (Arduino code below). (下面的Arduino代码)。 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. while循环很可爱,但是如果允许其在不等待用户的情况下运行,则效率很低。 I suspect the redundant fopen fclose use is stupid. 我怀疑多余的fopen fclose使用是愚蠢的。

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. 计算机将对所有这些值进行“压缩”,并将消息发送回以更改arduino的行为。 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++! 当然,这一切都是为了娱乐,但正如您所看到的,我需要“学习C ++中的串行交互的规则”! Any help or guidance greatly appreciated. 任何帮助或指导,不胜感激。


The arduino code: arduino代码:

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) . 如果希望Serial.read读取单个值,请发送'\\n', fprint (file, "%d\\n", value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM