Linux读写Arduino系列 我试图从C++程序中写入一个ARDUINO系列。如何在C++程序中设置波特率?当arduino设置为9600时,通信似乎正常,但当我更改它时,通信失败,正如预期的那样。那么9600是默认值?使用命令查看输出: screen/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); } }

Linux读写Arduino系列 我试图从C++程序中写入一个ARDUINO系列。如何在C++程序中设置波特率?当arduino设置为9600时,通信似乎正常,但当我更改它时,通信失败,正如预期的那样。那么9600是默认值?使用命令查看输出: screen/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); } },c++,arduino,ubuntu-16.04,communication,baud-rate,C++,Arduino,Ubuntu 16.04,Communication,Baud Rate,您需要使用termios结构设置正在使用的任何端口的波特率 使用man termios获取有关termios结构的更多信息 因此,首先需要添加 #include <termios.h> 您需要访问termios结构并修改它以满足您的需要 struct termios SerialPortSettings; // Create the structure tcgetattr(fd_ard, &SerialPortSett

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

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

因此,首先需要添加

#include <termios.h> 
您需要访问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");

可以为流量控制、cannonical模式等设置许多其他功能,这些功能可能会影响数据的发送和接收方式。参考手册页,或参考通用终端接口的

尝试使用/dev/ttyS0、/dev/ttyS1、/dev/ttyS2代替USB…检查,这是正确的开发,但波特率不正确。正如我所说,它适用于9600。非常确定USB和传统串行com端口不是一回事。因此,为了澄清,电脑是通过USB连接到arduino纳米板的,USB是用于编程的电缆,而不是传统的串行电缆。我应该更清楚这一点。它比这更复杂。它是一个通过USB连接的串行控制器,一旦该控制器安装在您的系统中,它就可以使用串行端口与Arduino微控制器进行通信。你不能只是通过USB通道进行串行通信,这不是它的工作原理。安装刚刚通过USB连接的串行控制器,然后可以通过串行端口与Arduino通话。如果Arduino IDE可以做到这一点,我猜它已经安装好了,只需使用串行端口即可。
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");