UART packets from marvelmind GPS is not detected correctly SOLVED

hello there friends
I have a problem with UART communication to marvelmind GPS. it is a device that connects to the Arduino using TX/RX pins on both sides, the device has an Arduino code that sorts the incoming packets into their designed order as follow

#include <SoftwareSerial.h>

SoftwareSerial portOne(0,1);

int hedgehog_x, hedgehog_y;// coordinates of hedgehog (X,Y), cm
int hedgehog_h;// height of hedgehog, cm
int hedgehog_pos_updated;// flag of new data from hedgehog received

///

#define HEDGEHOG_BUF_SIZE 30 
#define HEDGEHOG_PACKET_SIZE 23
byte hedgehog_serial_buf[HEDGEHOG_BUF_SIZE];
byte hedgehog_serial_buf_ofs;
const int hedgehog_packet_header[5]= {0xff, 0x47, 0x01, 0x00, 0x10};
typedef union {byte b[2]; unsigned int w;} uni_8x2_16;

//    Marvelmind hedgehog support initialize
void setup_hedgehog() 
{
  //Serial.begin(57600); // hedgehog transmits data on 500 kbps  
  portOne.begin(57600);
  hedgehog_serial_buf_ofs= 0;
  hedgehog_pos_updated= 0;
}

// Marvelmind hedgehog service loop
void loop_hedgehog()
{int incoming_byte;
 int total_received_in_loop;
 int packet_received;
   portOne.listen();/////////////////////////////////////////////////////////////////////////////////

  total_received_in_loop= 0;
  packet_received= 0;
 // while(Serial.available() > 0)//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  while (portOne.available() > 0)
    {
      if (hedgehog_serial_buf_ofs>=HEDGEHOG_BUF_SIZE) break;// buffer overflow
      total_received_in_loop++;
      if (total_received_in_loop>100) break;// too much data without required header
      
      //incoming_byte= Serial.read();
      incoming_byte= portOne.read();/////////////////////////////////////////////////////////////////////////////////
      // check first 5 bytes for constant value
      if (hedgehog_serial_buf_ofs<5)
        {
          if (incoming_byte != hedgehog_packet_header[hedgehog_serial_buf_ofs]) 
            {
              hedgehog_serial_buf_ofs= 0;// restart bufer fill
              continue;
            }
        }     
      hedgehog_serial_buf[hedgehog_serial_buf_ofs++]= incoming_byte;  
      if (hedgehog_serial_buf_ofs == HEDGEHOG_PACKET_SIZE)
        {// received packet with required header
          packet_received= 1;
          hedgehog_serial_buf_ofs= 0;// restart bufer fill
          break; 
        }
    }

  if (packet_received)  
    {
      hedgehog_set_crc16(&hedgehog_serial_buf[0], HEDGEHOG_PACKET_SIZE);// calculate CRC checksum of packet
      if ((hedgehog_serial_buf[HEDGEHOG_PACKET_SIZE] == 0)&&(hedgehog_serial_buf[HEDGEHOG_PACKET_SIZE+1] == 0))
        {// checksum success
          hedgehog_x= int(hedgehog_serial_buf[9]) + (int(hedgehog_serial_buf[10])<<8);
          hedgehog_y= int(hedgehog_serial_buf[11]) + (int(hedgehog_serial_buf[12])<<8);// coordinates of hedgehog (X,Y), cm
          hedgehog_h= int(hedgehog_serial_buf[13]) + (int(hedgehog_serial_buf[14])<<8);// height of hedgehog, cm (FW V3.97+)
          hedgehog_pos_updated= 1;// flag of new data from hedgehog received
        }
    }
}

// Calculate CRC-16 of hedgehog packet
void hedgehog_set_crc16(byte *buf, byte size)
{uni_8x2_16 sum;
 byte shift_cnt;
 byte byte_cnt;

  sum.w=0xffffU;

  for(byte_cnt=size; byte_cnt>0; byte_cnt--)
   {
   sum.w=(unsigned int) ((sum.w/256U)*256U + ((sum.w%256U)^(buf[size-byte_cnt])));

     for(shift_cnt=0; shift_cnt<8; shift_cnt++)
       {
         if((sum.w&0x1)==1) sum.w=(unsigned int)((sum.w>>1)^0xa001U);
                       else sum.w>>=1;
       }
   }

  buf[size]=sum.b[0];
  buf[size+1]=sum.b[1];// little endian
}// hedgehog_set_crc16

//  END OF MARVELMIND HEDGEHOG RELATED PART
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
     //Echo_pin


void setup()
{ 

  setup_hedgehog();//    Marvelmind hedgehog support initialize
  Serial.begin(57600); // to display via USB serial port
}

void loop()
{ 
  loop_hedgehog();// Marvelmind hedgehog service loop

   if (hedgehog_pos_updated)
     // new data from hedgehog available
       hedgehog_pos_updated= 0;// clear new data flag 
       // output hedgehog position to LCD



Serial.print("X=");
Serial.print(hedgehog_x);
Serial.print("   ");  
       
Serial.print("Y=");
Serial.print(hedgehog_y);
Serial.print("   ");  

Serial.print("H=");
Serial.print(hedgehog_h);
Serial.print("   ");  

Serial.println();
}

problem is the (if (packet_received) ) condition responsible for giving valid data to xyz variables is not becoming true that is why I keep getting zeros, can anyone help me with this problem

thank you

Which Arduino board are you using ?

I ask because on AVR based boards, such as the Uno and Nano pins 0 and 1 are used by the hardware UART associated with the USB interface to the PC and, as such, are used by the Serial monitor and uploading to the PC

On some other boards pins 0 and 1 are not used for that purpose as there is a second connection to the PC but then you would not need to use SoftwareSerial and could use the Serial1 interface on pins 0 an1

If you are using some other board, such as an ESP8266 or ESP32 then things are different again

Mega
And tried to use all the different possibilities for serial communications
Serial1, portserial, altserial and the TX rx pins

If you are using a Mega then you do not need to use SoftwareSerial

Use Serial1 on pins 18 (Tx) and 19(Rx) but make sure that the Rx and Tx pins of the GPS are cross connected with those on the Mega (Tx to Rx and vice versa), that the 2 devices have a common GND and that you are using the correct baud rate then use a simple sketch like this

void setup() 
{
	Serial.begin(<Serial monitor baud rate here>);
  Serial1.begin(<GPS baud rate here>);
}

void loop() 
{
  if (Serial1.available())
  {
    Serial.write(Serial1.read());   // EDIT - added missing brace
  }
}

Thank you for the answer bro
I tried exactly what you mentioned but still getting the same result all zero
The only way I got results is by disabling the if conditions I get inaccurate results but there are actually data that you can see

Disabling the test as to whether data is available() does not make sense. If data is available then the sketch will print what is received. If you leave out the test then I would expect mostly values of -1 because that is what Serial.read() returns if a byte is not available to read

Please post a link to the GPS device that you are using

I used this code to get the results

byte hedgehog_x, hedgehog_y;// coordinates of hedgehog (X,Y), cm
byte hedgehog_h;// height of hedgehog, cm
byte hedgehog_pos_updated;// flag of new data from hedgehog received
//#include <SoftwareSerial.h>
#include <AltSoftSerial.h>

AltSoftSerial altSerial(9, 8, 1);//rx tx
#define HEDGEHOG_BUF_SIZE 30 
#define HEDGEHOG_PACKET_SIZE 23
byte hedgehog_serial_buf[HEDGEHOG_BUF_SIZE];
byte hedgehog_serial_buf_ofs;
const int hedgehog_packet_header[5]= {0xff, 0x47, 0x01, 0x00, 0x10};
typedef union {byte b[2]; unsigned int w;} uni_8x2_16;

//////////Marvelmind hedgehog support initialize////////////////////////
void setup_hedgehog() 
{
    Serial.begin(9600); // hedgehog transmits data on 500 kbps
    Serial1.begin(9600);
    //Serial1.begin(9600);
    hedgehog_serial_buf_ofs= 0;
    hedgehog_pos_updated= 0;
}

// Marvelmind hedgehog service loop////////////
void loop_hedgehog()
{
  int incoming_byte;
  int total_received_in_loop;
  int packet_received;

  total_received_in_loop= 0;
  packet_received= 0;
  while(Serial1.available() > 0)
    {
      //Serial.print(hedgehog_serial_buf_ofs);
      if (hedgehog_serial_buf_ofs>=HEDGEHOG_BUF_SIZE) break;// buffer overflow
      total_received_in_loop++;
      //Serial.println(hedgehog_serial_buf_ofs);
      if (total_received_in_loop>100) break;// too much data without required header
      {
      incoming_byte= Serial1.read();
      //Serial.println(incoming_byte);
      //Serial.println(total_received_in_loop);
      //delay(300);
      }
      /*if (hedgehog_serial_buf_ofs<5)
        {
          //Serial.println(hedgehog_serial_buf_ofs);
          if (incoming_byte != hedgehog_packet_header[hedgehog_serial_buf_ofs]) 
            {
              //hedgehog_serial_buf_ofs= 0;// restart bufer fill
              //Serial.println("continue");
              continue;
            }
        }  */   
      hedgehog_serial_buf[hedgehog_serial_buf_ofs++]= incoming_byte; 
      //Serial.println(hedgehog_serial_buf_ofs);
      if (hedgehog_serial_buf_ofs == HEDGEHOG_PACKET_SIZE)
        {// received packet with required header
          //Serial.println("message");
          packet_received= 1;
          hedgehog_serial_buf_ofs= 0;// restart bufer fill
          break; 
        }
   delay(10) ;}//end while
  if (packet_received)  
    {
      
      hedgehog_set_crc16(&hedgehog_serial_buf[0], HEDGEHOG_PACKET_SIZE);// calculate CRC checksum of packet
          hedgehog_x= (hedgehog_serial_buf[9]) + ((hedgehog_serial_buf[10])<<8);
          hedgehog_y= (hedgehog_serial_buf[11]) + ((hedgehog_serial_buf[12])<<8);// coordinates of hedgehog (X,Y), cm
          hedgehog_h= (hedgehog_serial_buf[13]) + ((hedgehog_serial_buf[14])<<8);// height of hedgehog, cm (FW V3.97+)
          hedgehog_pos_updated=1;// flag of new data from hedgehog received
      if ((hedgehog_serial_buf[HEDGEHOG_PACKET_SIZE] == 0)&&(hedgehog_serial_buf[HEDGEHOG_PACKET_SIZE+1] == 0))
        {// checksum success
          Serial.println("ok");
          hedgehog_x= (hedgehog_serial_buf[9]) + ((hedgehog_serial_buf[10])<<8);
          hedgehog_y= (hedgehog_serial_buf[11]) + ((hedgehog_serial_buf[12])<<8);// coordinates of hedgehog (X,Y), cm
          hedgehog_h= (hedgehog_serial_buf[13]) + ((hedgehog_serial_buf[14])<<8);// height of hedgehog, cm (FW V3.97+)
          hedgehog_pos_updated=1;// flag of new data from hedgehog received
        }
    }
}//void loop_hedgehog

// Calculate CRC-16 of hedgehog packet
void hedgehog_set_crc16(byte *buf, byte size)
{uni_8x2_16 sum;
 byte shift_cnt;
 byte byte_cnt;

  sum.w=0xffffU;

  for(byte_cnt=size; byte_cnt>0; byte_cnt--)
   {
   sum.w=(unsigned int) ((sum.w/256U)*256U + ((sum.w%256U)^(buf[size-byte_cnt])));

     for(shift_cnt=0; shift_cnt<8; shift_cnt++)
       {
         if((sum.w&0x1)==1) sum.w=(unsigned int)((sum.w>>1)^0xa001U);
                       else sum.w>>=1;
       }
   }

  buf[size]=sum.b[0];
  buf[size+1]=sum.b[1];// little endian
}// hedgehog_set_crc16

///END OF MARVELMIND HEDGEHOG RELATED PART///
//SoftwareSerial mySerial(10, 11); //RX and TX
void setup()
{
  Serial.begin(9600);
  Serial1.begin(9600);
  setup_hedgehog();//Marvelmind hedgehog support initialize
  //Serial.print(hedgehog_serial_buf_ofs);
  //altSerial(10, 9);
  //Serial1.begin(9600);
  //Serial1(19, 18, 1);//RX and TX and inverting logic
  //Serial2.begin(9600);
}

void loop()
{ 
   loop_hedgehog();// Marvelmind hedgehog service loop

   if (hedgehog_pos_updated);
     {
          //hedgehog_pos_updated=0;

          //hedgehog_x= altSerial.read();
          //hedgehog_y= altSerial.read();
          //hedgehog_h= altSerial.read();
          
          Serial.print("X=");
          Serial.print(hedgehog_x*0.01,DEC );
          Serial.print(" ");  
                 
          Serial.print("Y=");
          Serial.print(hedgehog_y*0.01,DEC );
          Serial.print(" ");  
          
          Serial.print("H=");
          Serial.print(hedgehog_h*0.01,DEC );
          Serial.print(" \n");  
      }
}

the packets are arriving but the code with the if condition is not detecting it
that is why I used the above code to bypass it


this is a link
https://marvelmind.com/product/starter-set-super-mp-3d/

the problem was solved
the code is fine but the marvelmind GPS interface is the problem
you have to change the settings of some parameters that are ironically not related to the problem such as (TX only) by switching selection until the problem goes on its own

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.