0

I would like to connect GPS Module by using PIGPIO. but I can't. My code that show in below are occuring "Segment Violation",but I have no idea. Please help me.

Here is GPS spec.

GPS Receiver chip->MT3339

receiver frequency->1575.42MHz

receiver channel numbers->66(Acquisition) 22(tracking)

Satellite System->GPS(America), QZSS(Japan)

tracking sensitivity->-164dBm(typ.)

output data format->NMEA0183 V3.01

I/O Signal level->CMOS logic (3.3V), asynchronous serial signal

UART signaling rate->9600bps(default), from 4800 to 115200bps

Output data update rate-> once in second,from 1 to 10 in second

geodetic reference system->WGS1984

data bit-> 8bit / non parity, stop bit : 1 bit

flow control->none

GPIOs in use

GPS

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pigpio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#define TXD 29 //  5
#define RXD 11 //  17


int main(){
//  gpioTerminate();
char *buf;
int gsr = 0;
int count = 0;
gpioPulse_t pulse[2];
pulse[0].gpioOn = (1<<RXD);
pulse[0].gpioOff = 0;
pulse[0].usDelay = 10;

pulse[1].gpioOn = 0;
pulse[1].gpioOff = (1<<RXD);
pulse[1].usDelay = 10;

buf = malloc(100);

int wave_id;

if(buf == NULL) /* no memory */ exit(EXIT_FAILURE);

if (gpioInitialise() < 0){
    // pigpio initialisation failed.
    fprintf(stderr, "PIGPIO INITIALISATION FAILED\n");
    return 1;
}

gpioWaveClear();


gpioSetMode(TXD, PI_INPUT);

gpioSetMode(RXD, PI_OUTPUT);

gpioSerialReadOpen(TXD, 9600, 8);

gpioWaveAddNew();

gpioWaveAddGeneric(2, pulse);

wave_id = gpioWaveCreate();

char* errstr;

sprintf(errstr, "%d", wave_id);

strcat(errstr, ":PIGPIO WAVE WASN'T ESTABLISHED\n");

if(wave_id < 0){
    fprintf(stderr, errstr);
    return 1;
}

// pigpio initialised okey.

gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT); 


gpioSerialReadOpen(TXD, 9600, 8);

while(count < 5000){
    gsr = gpioSerialRead(TXD, buf, 8);
    if(gsr < 0)
    {
         printf("%d\n", gsr);
    }else{
        fwrite(buf, 8, 1, stdout);
    }
    sleep(100);
    count++;
}

gpioWaveTxStop();

gpioSerialReadClose(TXD);

gpioWaveTxStop();

    // release memory
    gpioTerminate();

    return 0;
}
  • As usuall newbibeibeiebiebeib.. YOU NEED MORE POWER! Please read about good power supply and invest some smaller amount of bucks to buy a good one! Then try again. Also , donut power the module from Pi GPIO.. power it from power adapter. Its newbie mistake number 2 – WillyWonka Nov 9 '16 at 8:10
  • Thanks Willy, So, you mean there are wrong wiring?, or should I have more current of electricity? I saw Arduino operate normally this module by softserial before. – NEWBIEEBIEE Nov 9 '16 at 8:38
  • PSU issues are common, but extremely unlikely to give you a Segment Violation that the OP is getting here. – Dmitry Grigoryev Nov 9 '16 at 9:24
1

Two points.

  1. The crash is caused by attempting to write to unassigned memory. You declare errstr as a pointer to memory but don't point it anywhere and don't allocate any memory for where you would point it. Perhaps you intended to add a line errstr = malloc(100);
  2. You read up to 8 bytes using gpioSerialRead(). You need to check the return status which will be either <0 for error, 0 for no data available (the normal case), or >0 for data available. It is a non-blocking read, it will not wait for data to arrive.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pigpio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

#define TXD 5 // GPS's TXD to RPI's RX
#define RXD 17 //  RPI's TX to GPS's RXD

int main()
{
   char buf[128], errstr[128];
   gpioPulse_t pulse[2];
   int wave_id, count;

   pulse[0].gpioOn = (1<<RXD);
   pulse[0].gpioOff = 0;
   pulse[0].usDelay = 10;

   pulse[1].gpioOn = 0;
   pulse[1].gpioOff = (1<<RXD);
   pulse[1].usDelay = 10;

   if (gpioInitialise() < 0)
   {
      // pigpio initialisation failed.
      fprintf(stderr, "PIGPIO INITIALISATION FAILED\n");
      return 1;
   }

   gpioWaveClear();

   gpioSetMode(TXD, PI_INPUT);

   gpioSetMode(RXD, PI_OUTPUT);

   gpioSerialReadOpen(TXD, 9600, 8);

   gpioWaveAddGeneric(2, pulse);

   wave_id = gpioWaveCreate();

   sprintf(errstr, "%d", wave_id);

   strcat(errstr, ":PIGPIO WAVE WASN'T ESTABLISHED\n");

   if (wave_id < 0)
   {
      fprintf(stderr, errstr);
      return 1;
   }

  // pigpio initialised okey.

   gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT); 

   while(1)
   {
      count = gpioSerialRead(TXD, buf, sizeof(buf)-1);
      if (count > 0)
      {
         buf[count] = 0;
         printf("c=%d %s\n", count, buf);
      }
      sleep(1);
   }

   gpioWaveTxStop();

   gpioSerialReadClose(TXD);

   gpioWaveTxStop();

   // release memory
   gpioTerminate();

   return 0;
}
  • I modified the code,but the same error issue occured, There is a long way to go.By the way Thanks. – NEWBIEEBIEE Nov 9 '16 at 9:30
  • @NEWBIEEBIEE I added the modified code. – joan Nov 9 '16 at 9:44
  • @ joan ,Thank you for giving me a hand.That's it,finally I can take some message like "43455.807,,,,,0,0,,,M,,M,,*44 $GPGLL,,,,,043455.807,V,N*76 $GPGSA,A,1,,,,,,,,,,,,,,,*1E " Thank you so much. – NEWBIEEBIEE Nov 9 '16 at 9:58
  • I appreciate that you trim this code. beautiful. – NEWBIEEBIEE Nov 9 '16 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.