[英]segmentation fault ( core dumped )
I'm trying to receive 9 float data using UART, every float is sent in the form XXX.XX.我正在尝试使用 UART 接收 9 个浮点数据,每个浮点数都以 XXX.XX 的形式发送。 So I'm going to receive 5*9 =45 informations.所以我将收到 5*9 =45 条信息。
I tried to make the data received in a array of 45 char then I can do what I want with this array, but I get this famous error when i run my application : Segmentation fault (core dumped )我试图在 45 个字符的数组中接收数据,然后我可以用这个数组做我想做的事情,但是当我运行我的应用程序时,我得到了这个著名的错误:分段错误(核心转储)
my code is我的代码是
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <errno.h>
#include <wiringSerial.h>
using namespace std;
int main (){
int fd;
float courant;
float voltage;
float activepower;
float reactivepower;
float apparentpower;
float powerfactor;
float frequency;
float temperature;
float indexIns;
string courantC;
string voltageC;
string activepowerC;
string reactivepowerC;
string apparentpowerC;
string powerfactorC;
string frequencyC;
string temperatureC;
string indexinsC;
char receivedData[45];
int i=0;
if ((fd = serialOpen("/dev/ttyS1",9600)) < 0)
{
fprintf(stderr,"Unable to open serial device %s\n",strerror(errno));
return 1;
}
// Loop , getting and printing characters
for (int j=0; j<45; j++)
{
i = serialGetchar(fd);
receivedData[j] = char(i);
if (j = 44) {
j = 0;
courantC = receivedData[0]+receivedData[1]+receivedData[2]+"." + receivedData[3]+receivedData[4];
voltageC = receivedData[5]+receivedData[6]+receivedData[7]+"." + receivedData[8]+receivedData[9];
activepowerC = receivedData[10]+receivedData[11]+receivedData[12]+"." + receivedData[13]+receivedData[14];
reactivepowerC = receivedData[15]+receivedData[16]+receivedData[17]+"." + receivedData[18]+receivedData[19];
apparentpowerC = receivedData[20]+receivedData[21]+receivedData[22]+"." + receivedData[23]+receivedData[24];
powerfactorC = receivedData[25]+receivedData[26]+receivedData[27]+"." + receivedData[28]+receivedData[29];
frequencyC = receivedData[30]+receivedData[31]+receivedData[32]+"." + receivedData[33]+receivedData[34];
temperatureC = receivedData[35]+receivedData[36]+receivedData[37]+"." + receivedData[38]+receivedData[39];
indexinsC = receivedData[40]+receivedData[41]+receivedData[42]+"." + receivedData[43]+receivedData[44];
}
printf("a=%c ", char(i));
fflush(stdout);
The command used to compile用于编译的命令
gcc receiveData.cpp -o receiveData -lwiringPi -lpthread -lstdc++
This line:这一行:
courantC = receivedData[0]+receivedData[1]+receivedData[2]+"." + receivedData[3]+receivedData[4];
causes undefined behavior (UB) and is probably the cause of the segment fault.导致未定义行为 (UB) 并且可能是段错误的原因。 In this line you have an array of char
, this here "."
在这一行中,您有一个char
数组,这里是"."
, with 2 chars, '.'
, 有 2 个字符, '.'
and '\\0'
.和'\\0'
。 This array is stored somewhere.这个数组存储在某处。 When you use it like this it decays to a pointer that points at the start of the array.当您像这样使用它时,它会衰减为指向数组开头的指针。 The rest are all char
, which are integers in C. They are added together and added to the pointer pointing to the array "."
其余的都是char
,在C中都是整数,它们相加后加到指向数组"."
的指针上"."
. . This pointer then points to some unknown memory region which is accessed by the String
class to be copied to courantC
.这个指针然后指向一些未知的内存区域,该区域由String
类访问以复制到courantC
。 But accessing the region causes UB.但是访问该区域会导致UB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.