构建代码时出错
Error when I building my code
我在构建代码时在覆盆子上的C 代码有一些问题。该代码是从传感器中获取并从Arduino发送数据的。
因此,我认为问题不能来自Arduino,因为Arduino的代码与程序合作,但是由于某些原因,我需要使用C ,我想知道如何解决我的问题,我有点按压力时间是因为这是一个大学项目。
所以这是我的代码:
#include "SerialPort.h" //My library
SerialPort::SerialPort() //Constructor
{
this->numCon = open("/dev/ttyACM0", O_RDWR| O_NOCTTY ); //ttyACM0 arduino port
this->connected = false;
struct termios tty;
struct termios tty_old;
memset (&tty, 0, sizeof tty);
/* Error Handling */
if ( tcgetattr ( this->numCon, &tty ) != 0 ) {
std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
return;
}
/* Save old tty parameters */
tty_old = tty;
/* Set Baud Rate */
cfsetospeed (&tty, (speed_t)B9600);
cfsetispeed (&tty, (speed_t)B9600);
/* 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(this->numCon, TCIFLUSH );
if ( tcsetattr (this->numCon, TCSANOW, &tty ) != 0) {
std::cout << "Error " << errno << " from tcsetattr" << std::endl;
return;
}
this->connected = true;
}
SerialPort::~SerialPort() //Destructor
{
if (this->connected) {
this->connected = false;
}
}
char* SerialPort::readSerialPort()
{
int n = 0, spot = 0;
char buf = ' ';
/* Whole response*/
char response[1024];
memset(response, ' ', sizeof response);
do {
n = read(this->numCon, &buf, 1 );
sprintf( &response[spot], "%c", buf );
spot += n;
} while( buf != 'r' && n > 0);
if (n < 0) {
std::cout << "Error reading: " << strerror(errno) << std::endl;
}
else if (n == 0) {
std::cout << "Read nothing!" << std::endl;
}
else {
std::cout << "Response: " << response << std::endl;
}
return response;
}
void SerialPort::writeSerialPort(unsigned char cmd[])
{
int n_written = 0, spot = 0;
do {
n_written = write(this->numCon, &cmd[spot], 1 );
spot += n_written;
} while (cmd[spot-1] != 'r' && n_written > 0);
}
bool SerialPort::isConnected()
{
return this->connected;
}
这是库(serialport.h(:
#ifndef SERIALPORT_H
#define SERIALPORT_H
#include <stdio.h> // standard input / output functions
#include <stdlib.h>
#include <string.h> // string function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
#include <iostream>
class SerialPort
{
private:
int numCon;
bool connected;
public:
SerialPort();
~SerialPort();
char* readSerialPort();
void writeSerialPort(unsigned char cmd[]);
bool isConnected();
};
#endif // SERIALPORT_H
错误:
pi@Lux:~/PROJET $ g++ SerialPort.cpp -o SerialPort.out
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned
[-Wreturn-local-addr]
char response[1024];
^~~~~~~~
/usr/lib/gcc/arm-linux-gnueabihf/6/../../../arm-linux-gnueabihf/crt1.o : Dans
la fonction « _start » :
(.text+0x34) : référence indéfinie vers « main »
collect2: error: ld returned 1 exit status
pi@Lux:~/PROJET $ ./SerialPort.cpp
./SerialPort.cpp: ligne 2: $'r' : commande introuvable
./SerialPort.cpp: ligne 3: erreur de syntaxe près du symbole inattendu « $'r' »
'/SerialPort.cpp: ligne 3: `SerialPort::SerialPort()
让我们采取第一个错误,这不是真正的错误,而是"只是"警告说您做一些不好的事情:
SerialPort.cpp: In member function ‘char* SerialPort::readSerialPort()’:
SerialPort.cpp:63:7: warning: address of local variable ‘response’ returned
在SerialPort::readSerialPort
函数中,变量response
是局部变量。它的寿命和范围是功能调用的范围。当功能结束时,变量的寿命也是如此。它将在某种程度上不再存在。将指针返回该变量将立即使指针无效。
如果要将读取的数据视为字符串,请使用字符串数据类型。像标准C 中的std::string
一样,而Arduino中的String
。
第二个错误是因为您的代码没有main
函数。全局main
功能是所有标准C 程序的入口点。
相关文章:
- 在Linux for Windows上编译C++代码时出错
- 尝试将另一个子句添加到代码中时出错
- 将代码从 Python 转换为 C++ 时出错
- <limits> 使用 VSC2019 编译旧代码时出错
- 使用 Visual Studio 代码读取 C++ 中的输入流时出错
- 在OpenGL中使用程序集代码渲染视频时出错
- 编译以下代码时出错
- Boost FFT示例-编译时出错,这段代码在做什么
- 编译在 Python 代码内部调用的 C++ 代码时出错,使用 Boost Python
- C++:为什么这段代码会出错?多元线性回归
- 混合开放MP/MPI代码中的多线程发送/接收时出错
- 将节点 js 代码集成到 cpp 库时出错
- 构建代码时出错
- 在字符串流中使用来自 github 的代码时出错
- 在主代码中调用 .hpp 函数时出错
- 将 find_if() 与数组一起使用的代码段中出错
- 我在字数C++的代码中哪里出错了
- 尝试从 Github 编译 OpenCV QT 代码时出错
- 编译创建 Eigen::ThreadPoolDevice 对象的代码时出错
- 编译 .cu 与 .cpp:即使没有任何 CUDA 代码,编译器也会出错