[英]Linux read, write Arduino serial
我正在嘗試從 C++ 程序寫入 Arduino 串行。 如何在 C++ 程序中設置波特率? 當 arduino 設置為 9600 時,通信似乎有效,但是當我按預期更改它時失敗。(所以 9600 是默認值?)使用命令查看輸出: screen /dev/ttyUSBx 115200 Using an example program to在 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 "";
}
您需要使用 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");
還有很多其他的東西可以設置為流量控制、規范模式等,它們可能會影響數據的發送和接收方式。 參考手冊頁,或者參考通用終端接口的 這個描述
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.