【问题标题】:Linux read, write Arduino serialLinux 读写 Arduino 串口
【发布时间】:2018-09-23 22:23:14
【问题描述】:

我正在尝试从 c++ 程序写入 Arduino 串行。如何在 C++ 程序中设置波特率?当 arduino 设置为 9600 时,通信似乎有效,但当我更改它时失败,正如预期的那样。(所以 9600 是默认值?)使用命令查看输出: 屏幕 /dev/ttyUSBx 115200 在 Arduino 上使用示例程序来回显所有内容:

* serial_echo.pde
* ----------------- 
* Echoes what is sent back through the serial port.
*
* http://spacetinkerer.blogspot.com
*/

int incomingByte = 0;    // for incoming serial data

void setup() {
  Serial.begin(115200);    // 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();
  // say what you got:
  Serial.print((char)incomingByte);
}

C++ 代码是:

#include <iostream>
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

using namespace std;

string comArduino(string outmsg = " hello"){

  #define comports_size 3

  static int fd_ard;
  static bool init = false;
  //char buffer[256];

  if(!init){
      string comports[] = { "/dev/ttyUSB0","/dev/ttyUSB1","/dev/ttyUSB2" };
      for(int i = 0; i < comports_size; i++){

          fd_ard = open(comports[i].c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
          if (fd_ard != -1) {
              cout<<"connected to "<<comports[i]<<endl;
              init = true;
              break;
          }
          else{
              perror ("open");
          }
          if(i == (comports_size - 1)){
              cout<<"can't connect to arduino [ ER ]\n"<<endl;
              return "";
          }
      }
  }
  int Er = write(fd_ard,outmsg.c_str(),strlen(outmsg.c_str()));
  if(Er < strlen(outmsg.c_str())){
      perror ("write ");
      init = false;
  }
  else
      cout<<"write ok"<<endl;
  return "";
}
int main(){
  while(1){
      comArduino();
      sleep(1);
  }
}

编辑:需要在open() 之后添加以下行以正确配置串行:

    struct termios PortConf;
    // write port configuration, as ' stty -F /dev/ttyUSB0 -a ' returned after opening the port with the arduino IDE.
    tcgetattr(fd_ard, &PortConf);                           // Get the current attributes of the Serial port
    PortConf.c_cflag = 0;                                 //set cflag
    PortConf.c_cflag |= (CS8 | HUPCL | CREAD | CLOCAL);

    PortConf.c_iflag = 0;                                 //set iflag
    //PortConf.c_iflag &= ~(ISIG | ICANON | IEXTEN);

    PortConf.c_oflag = 0;                                 //set oflag
    PortConf.c_oflag |= (ONLCR | CR0 | TAB0 | BS0 | VT0 | FF0); //NR0 is supposed to be set, but won't compile
    //PortConf.c_oflag &= ~(OPOST);

    PortConf.c_lflag = 0;                                 //set lflag
    //PortConf.c_lflag &= ~(ECHO | ECHOE);

    PortConf.c_cc[VMIN]  = 0;
    PortConf.c_cc[VTIME] = 0;

    cfsetispeed(&PortConf,B115200);                       // Set Read  Speed as 115200                       
    cfsetospeed(&PortConf,B115200);                       // Set Write Speed as 115200 

    if((tcsetattr(fd_ard,TCSANOW,&PortConf)) != 0){       // Set the attributes to the termios structure
        printf("Error while setting attributes \n");
        return "";
    }

【问题讨论】:

  • 尝试使用/dev/ttyS0/dev/ttyS1/dev/ttyS2 代替 USB...
  • 检查,它是正确的开发,但波特率不正确。正如我所说,它适用于 9600。
  • 很确定 USB 和传统的串行 com 端口不是一回事。
  • 所以澄清一下,PC 是通过 USB(用于编程的电缆)连接到 arduino nano 板,而不是使用传统的串行电缆。我应该更清楚一点。跨度>
  • 比这更复杂。它是一个通过 USB 连接的串行控制器,一旦这个控制器安装在您的系统中,它就会提供一个串行端口来与 Arduino 微控制器通信。您不能只通过 USB 通道谈论 Serial,它不是这样工作的。安装您刚刚通过 USB 连接的串行控制器,然后您可以通过串行端口与 Arduino 通信。如果 Arduino IDE 可以做到,我猜它已经安装了,只需使用串行端口。

标签: c++ arduino ubuntu-16.04 communication baud-rate


【解决方案1】:

您需要使用 termios 结构设置您正在使用的任何端口的波特率。

使用“man termios”获取有关 termios 结构的更多信息

所以一开始需要添加

#include <termios.h> 

到代码的顶部。

稍后当您打开端口时:

fd_ard = open(comports[i].c_str(), O_RDWR | O_NOCTTY | O_NDELAY);

您需要访问 termios 结构并对其进行修改以满足您的需要

struct termios SerialPortSettings;  // Create the structure                          
tcgetattr(fd_ard, &SerialPortSettings); // Get the current attributes of the Serial port

然后,设置波特率

cfsetispeed(&SerialPortSettings,B115200); // Set Read  Speed as 115200                       
cfsetospeed(&SerialPortSettings,B115200); // Set Write Speed as 115200                       

然后提交更改

if((tcsetattr(fd_ard,TCSANOW,&SerialPortSettings)) != 0) // Set the attributes to the termios structure
  printf("Error while setting attributes \n");

还有很多其他的东西可以设置为流量控制、规范模式等,这可能会影响数据的发送和接收方式。参考手册页,或参考通用终端接口的This description

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 2017-03-13
    • 1970-01-01
    相关资源
    最近更新 更多