简体   繁体   English

在Linux C ++中没有通过串行端口发送数据

[英]No data sent over serial port in linux c++

TL;DR - I am attempting serial communication with Arduino with code that I found here and nothing gets sent over (Arduino programmed to respond, and I checked that it does with its serial monitor) TL; DR-我正在尝试使用我在这里找到的代码与Arduino进行串行通信,但没有任何东西被发送出去(Arduino已编程为响应,并且我检查了它是否与串行监视器兼容)

Hi there, I was looking for a way to send information over to an Arduino Mega (2560) unit over linux serial port by C++. 嗨,我在寻找一种通过C ++通过Linux串行端口将信息发送到Arduino Mega(2560)单元的方法。

I came across the following solution: Solution I'm using this guy's code for write (I'm able to read data from the arduino) and use the same parameters (they work, as I'm able to receive data from the Ardunio). 我遇到了以下解决方案: 解决方案我正在使用此人的代码进行写操作(我能够从arduino读取数据)并使用相同的参数(它们起作用了,因为我能够从Ardunio接收数据) 。 I programmed my Arduino to send "Hi" over serial whenever it sees at least 1 bit of information, and checked it worked through the Arduino IDE Serial Monitor. 我对Arduino进行编程,以便每当看到至少1位信息时都通过串行发送“ Hi”,然后通过Arduino IDE串行监视器检查其是否工作。

Yet when running the C++ code, the arduino doesn't respond. 但是,在运行C ++代码时,arduino不会响应。 Do anyone might have idea why? 有人知道为什么吗?

Full disclosure - I inserted @Lunatic999's code to a class so I can make an instance of it for my needs of the code. 全面披露-我将@ Lunatic999的代码插入到一个类中,以便我可以根据自己的代码需要为其创建一个实例。

fd = open(portNameC, O_RDWR | O_NOCTTY | O_SYNC); //open port ("opens file")

Serial Parameters: 串行参数:

struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);

/* Error Handling */
if ( tcgetattr ( fd, &tty ) != 0 ) {
   std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
}

/* Save old tty parameters */
tty_old = tty;

/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B19200);
cfsetispeed (&tty, (speed_t)B19200);

/* Setting other Port Stuff */
tty.c_cflag     &=  ~PARENB;            // Make 8n1
tty.c_cflag     &=  ~CSTOPB;
tty.c_cflag     &=  ~CSIZE;
tty.c_cflag     |=  CS8;

tty.c_cflag     &=  ~CRTSCTS;           // no flow control
tty.c_cc[VMIN]   =  1;                  // read doesn't block
tty.c_cc[VTIME]  =  5;                  // 0.5 seconds read timeout
tty.c_cflag     |=  CREAD | CLOCAL;     // turn on READ & ignore ctrl lines

/* Make raw */
cfmakeraw(&tty);

/* Flush Port, then applies attributes */
tcflush( fd, TCIFLUSH );
if ( tcsetattr ( fd, TCSANOW, &tty ) != 0) {
   std::cout << "Error " << errno << " from tcsetattr" << std::endl;
}

Write (this code I put inside a function which I call) 编写 (此代码已放入我调用的函数中)

unsigned char cmd[] = "INIT \r";
int n_written = 0,
    spot = 0;

do {
    n_written = write( fd, &cmd[spot], 1 );
    spot += n_written;
} while (cmd[spot-1] != '\r' && n_written > 0);

Arduino code: Arduino代码:

bool dataRecieved = false;
int ledpin = 13;

void setup() {
pinMode(ledpin, OUTPUT);
digitalWrite(ledpin, HIGH);
Serial.begin(19200);
}

void loop() {  
  while(!dataRecieved)
  {
   digitalWrite(ledpin,HIGH);
   if (Serial.available() > 0) 
   {
     dataRecieved = true;
   }
  }
  digitalWrite(ledpin,LOW);
  delay(1000);
  digitalWrite(ledpin,HIGH);
  delay(1000);
  Serial.println("hi");
}

Turns out it was an arduino problem all along. 原来,这一直是一个难题。 I needed to apply some usleep to let arduino bootload 我需要应用一些usleep来让arduino引导加载

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

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