0

I have a problem with the following code. I think it's a pointer issue but I don't know how to solve it.

I want to call the SIM7070_Start function from main. This function calls SIM7070_SendCommand and then the error appears. It is related to the 'serial' variable of WiringPi?

please help


#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;
}

if main is:

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;
}

it works.

Jack Lilhammers
  • 1,207
  • 7
  • 19
Nuria
  • 15
  • 3
  • `*data = serialGetchar(serial);` but `char *data` has not been assigned a value. – Paul Ogilvie Feb 11 '21 at 14:30
  • Hello Paul,I do not understand. What should I do? – Nuria Feb 11 '21 at 14:35
  • `data` must point to valid memory in function `SIM7070_SendCommand` – Paul Ogilvie Feb 11 '21 at 14:37
  • why `char *data` anyway and not `char data`? – Paul Ogilvie Feb 11 '21 at 14:38
  • I want to compare data with expectedResponse. I edit the post to show it. – Nuria Feb 11 '21 at 14:46
  • Sorry. I am a beginner – Nuria Feb 11 '21 at 14:47
  • `data = serialGetchar(serial);` gets a single character, so you cannot `printf ("%s", data);` which prints a string. Turn warnings of your compiler on to get feedback. – Paul Ogilvie Feb 11 '21 at 14:48
  • Not being familiar with `wiringpi` I'd like just a bit more explanation before you jump into: `SIM7070_Start`. Just a brief description of what you're doing and on what platform. It may not be 100% necessary, but it would make me feel less lost. – ebyrob Feb 11 '21 at 15:32
  • Also, at the bottom of your question I'd like to see a detailed description of error / symptoms. I know you name the type of error in the question's title, but in general every error type question should have a detailed error description in the body of the question. – ebyrob Feb 11 '21 at 15:34

1 Answers1

1

See also my comments. You may want a loop like:

char data[256], *pData= data;
while(serialDataAvail(serial)){ 
   *pData++ = serialGetchar(serial);     
}
*pData= '\0';
printf ("%s", data);

This uses an array of chars to place the data received into. Pointer variable pData then points to this buffer and increments with each character received. At the end it terminates the (string) buffer with a terminating null character.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41