分段违规 wiringpi
Segmentation violation wiringpi
我对以下代码有疑问。我认为这是一个指针问题,但我不知道如何解决。
我想从 main 调用 SIM7070_Start 函数。此函数调用 SIM7070_SendCommand 然后出现错误。
与WiringPi的'serial'变量有关?
请帮忙
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
const int pwr = 4;
const int dtr = 25;
int SIM7070_PowerOn()
{
printf("SIM7070_PowerOn\n");
pinMode(pwr, OUTPUT);
digitalWrite(pwr, HIGH);
delay(2000);
digitalWrite(pwr, LOW);
delay(2000);
pinMode(dtr, OUTPUT);
digitalWrite(dtr, LOW);
delay(1000);
}
int SIM7070_powerDown(void)
{
printf("SIM7070_PowerDown\n");
digitalWrite(pwr, HIGH);
delay(1000);
digitalWrite(pwr, LOW);
delay(2000);
}
int SIM7070_SendCommand(int serial, char * command, char * expectedResponse)
{
char data;
data = (char *) malloc(sizeof(char));
printf("\nSEND COMMAND IS: %s\n", command);
serialPrintf(serial, command);
delay(200);
while (serialDataAvail(serial)) {
data = serialGetchar(serial);
printf("%s", data);
}
if (strlen(expectedResponse) != 0) {
if (strcmp(data, expectedResponse) <= 0) {
printf("\nSIM7070_SendCommand - The expected response has been found\n");
return 1;
} else {
printf("\nSIM7070_SendCommand - The expected response hasn't been found\n");
return 0;
}
}
}
void SIM7070_Start(int serial)
{
SIM7070_SendCommand(serial, "AT\r\n", "OK");
}
int main()
{
int serial;
char data;
if (wiringPiSetup() < 0)
return 1;
if ((serial = serialOpen("/dev/ttyS0", 57600)) < 0)
return 1;
printf("Serial start ...\n");
SIM7070_PowerOn();
delay(2000);
SIM7070_Start(serial);
delay(5000);
serialClose(serial);
SIM7070_powerDown();
return 0;
}
如果主要是:
int main()
{
int serial;
char data;
if (wiringPiSetup() < 0)
return 1;
if ((serial = serialOpen("/dev/ttyS0", 57600)) < 0)
return 1;
printf("Serial start ...\n");
SIM7070_PowerOn();
delay(2000);
SIM7070_SendCommand(serial, "AT\r\n", "OK");
delay(5000);
serialClose(serial);
SIM7070_powerDown();
return 0;
}
有效。
另请参阅我的评论。你可能想要一个像这样的循环:
char data[256], *pData= data;
while(serialDataAvail(serial)){
*pData++ = serialGetchar(serial);
}
*pData= '[=10=]';
printf ("%s", data);
这使用一个字符数组来放置接收到的数据。指针变量 pData
然后指向此缓冲区并随着每个接收到的字符递增。最后,它用一个终止空字符终止(字符串)缓冲区。
我对以下代码有疑问。我认为这是一个指针问题,但我不知道如何解决。
我想从 main 调用 SIM7070_Start 函数。此函数调用 SIM7070_SendCommand 然后出现错误。 与WiringPi的'serial'变量有关?
请帮忙
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <wiringPi.h>
#include <wiringSerial.h>
const int pwr = 4;
const int dtr = 25;
int SIM7070_PowerOn()
{
printf("SIM7070_PowerOn\n");
pinMode(pwr, OUTPUT);
digitalWrite(pwr, HIGH);
delay(2000);
digitalWrite(pwr, LOW);
delay(2000);
pinMode(dtr, OUTPUT);
digitalWrite(dtr, LOW);
delay(1000);
}
int SIM7070_powerDown(void)
{
printf("SIM7070_PowerDown\n");
digitalWrite(pwr, HIGH);
delay(1000);
digitalWrite(pwr, LOW);
delay(2000);
}
int SIM7070_SendCommand(int serial, char * command, char * expectedResponse)
{
char data;
data = (char *) malloc(sizeof(char));
printf("\nSEND COMMAND IS: %s\n", command);
serialPrintf(serial, command);
delay(200);
while (serialDataAvail(serial)) {
data = serialGetchar(serial);
printf("%s", data);
}
if (strlen(expectedResponse) != 0) {
if (strcmp(data, expectedResponse) <= 0) {
printf("\nSIM7070_SendCommand - The expected response has been found\n");
return 1;
} else {
printf("\nSIM7070_SendCommand - The expected response hasn't been found\n");
return 0;
}
}
}
void SIM7070_Start(int serial)
{
SIM7070_SendCommand(serial, "AT\r\n", "OK");
}
int main()
{
int serial;
char data;
if (wiringPiSetup() < 0)
return 1;
if ((serial = serialOpen("/dev/ttyS0", 57600)) < 0)
return 1;
printf("Serial start ...\n");
SIM7070_PowerOn();
delay(2000);
SIM7070_Start(serial);
delay(5000);
serialClose(serial);
SIM7070_powerDown();
return 0;
}
如果主要是:
int main()
{
int serial;
char data;
if (wiringPiSetup() < 0)
return 1;
if ((serial = serialOpen("/dev/ttyS0", 57600)) < 0)
return 1;
printf("Serial start ...\n");
SIM7070_PowerOn();
delay(2000);
SIM7070_SendCommand(serial, "AT\r\n", "OK");
delay(5000);
serialClose(serial);
SIM7070_powerDown();
return 0;
}
有效。
另请参阅我的评论。你可能想要一个像这样的循环:
char data[256], *pData= data;
while(serialDataAvail(serial)){
*pData++ = serialGetchar(serial);
}
*pData= '[=10=]';
printf ("%s", data);
这使用一个字符数组来放置接收到的数据。指针变量 pData
然后指向此缓冲区并随着每个接收到的字符递增。最后,它用一个终止空字符终止(字符串)缓冲区。