Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Interfacing / Re: GPS EM-406 example on: October 21, 2007, 04:20:49 pm
Must be a problem in the SoftwareSerial read routine because I got the exact same results (unreadable nmea-like strings) with the following code - here I'm only using the software serial for the read part and the hardware uart for the write results to debug console.  You'll notice in the main loop() the same results come from either the readbyte/printbyte method or readline/printline method (commented out).  I also changed the name of the char2string function to getLine:

Code:
#include <ctype.h>

#define bit9600Delay 84  
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94

byte rx = 6;   // EM406A GPS tx line connected to pin 6 on Arduino (4800baud)
byte SWval;
char line[80]="";

void setup() {
  pinMode(rx,INPUT);
  digitalWrite(13,HIGH);     // turn on debugging LED
  Serial.begin(4800);        // Setup USB serial port monitor to 4800baud
}

int SWread()
{
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit4800Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit4800Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit4800Delay);
    delayMicroseconds(bit4800Delay);
    return val;
  }
}

void getLine()
{
  int i = 0;
  line[0] = SWread();
  if (line[0] == 36) //string starts with $
  {
    i++;
    line[i] = SWread();
    while(line[i] != 13 & i<80) //carriage return or max size
    {
      i++;
      line[i] = SWread();
    }
    line[i+1] = 0; //make end to string
  }
} // end getLine()


void loop()
{
    SWval = SWread();
    Serial.print(SWval);
    
    // getLine();
    // Serial.println(line);
    
}

Am currently using the code at the following URL with perfect results using the EM-406 instead of the Parallax module (also connected via 3 wires pwr/gnd/tx):

http://www.arduino.cc/playground/Tutorials/GPS

2  Forum 2005-2010 (read only) / Interfacing / Re: GPS EM-406 example on: October 18, 2007, 01:37:10 pm
Sure the following is the code used:

Code:
//Created August 15 2006
//Heather Dewey-Hagborg
//http://www.arduino.cc
//reworked to GPS reader
//Dirk, december 2006

#include <ctype.h>
#include <string.h>

#define bit9600Delay 84  
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94

byte rx = 6;
byte tx = 7;
byte SWval;
char dataformat[7] = "$GPRMC";
char messageline[80] = "";
int i= 0;

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  digitalWrite(13,HIGH); //turn on debugging LED
  SWprint('h');  //debugging hello
  SWprint('i');
  SWprint(10); //carriage return
  Serial.begin(9600);  // this is used to echo what is read from the GPS out the USB->SerialDebugMonitor
}

void SWprint(int data)
{
  byte mask;
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit4800Delay);
  for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & mask){ // choose bit
      digitalWrite(tx,HIGH); // send 1
    }
    else{
      digitalWrite(tx,LOW); // send 0
    }
    delayMicroseconds(bit4800Delay);
  }
  //stop bit
  digitalWrite(tx, HIGH);
  delayMicroseconds(bit4800Delay);
}

char SWread()
{
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit4800Delay);
    for (int offset = 0; offset < 8; offset++) {
      delayMicroseconds(bit4800Delay);
      val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit4800Delay);
    delayMicroseconds(bit4800Delay);
    return val;
  }
}
void char2string()
{
  i = 0;
  messageline[0] = SWread();
  if (messageline[0] == 36) //string starts with $
  {
    i++;
    messageline[i] = SWread();
    while(messageline[i] != 13 & i<80) //carriage return or max size
    {
      i++;
      messageline[i] = SWread();
    }
    messageline[i+1] = 0; //make end to string
  }
}

void loop()
{
  digitalWrite(13,HIGH);
  
  //
  // 1st way to print only the $GPRMC message
  //
  
  //only print string with the right dataformat
  // char2string();
  // if (strncmp(messageline, dataformat, 6) == 0 & i>4)
  // {
  //   for (int i=0; messageline[i] != '\0'; i++)
  //   {
  //      Serial.print(messageline[i], BYTE);
  //   }
  // }
  
  //
  // 2nd way to print only the $GPRMC message
  //
  
  //   char2string();
  //   if (strncmp(messageline, dataformat, 6) == 0 & i>4)
  //   {
  //     Serial.println(messageline);
  //   }
  
  //
  // way to print ALL RECEIVED MESSAGES!
  //
  
   Serial.print(SWread(),BYTE); //use this to get all GPS output, comment out from char2string till here
  
}
 

The last bytewise print of the raw stream gave the best results of the 3 different ways to print output strings.

3  Forum 2005-2010 (read only) / Interfacing / Re: GPS EM-406 example on: October 15, 2007, 04:59:24 pm
First post - cool hardware BTW...

Thanks bigengineer and everyone else - I now have my EM406A talking to my Arduino Decimilla (4 wires, 5V/GND/TX/RX) using the code posted.  I even tried all three examples of how to print the resulting strings to the Debug Window.  My problem is that they all look like this:



These are printing ALL strings BTW.  I understand that the conversation from the GPS unit is 4800 baud (hence the 4800 delays) and received thru Digital pin 6 on the Arduino.  I also understand that the strings are output via the onboard debug serial lines (hardware uart that the USB FTDI chip uses - with the following in Setup(): Serial.begin(9600); ) hence them showing up at 9600 baud in the Serial Debug Monitor.  I get the same garbage if I set the output to 4800 via Serial.begin(4800); in Setup();

What am I missing here?  TIA

mopowered
4  Forum 2005-2010 (read only) / Interfacing / Re: serial communication question on: October 24, 2007, 06:38:08 pm
Yes you can have hardware attached to pins 0 and 1 (the hardware rx and tx uart) on the Decimilia - and communicate to them/it while the USB is connected.  You just cant initiate a programming (or other computer-generated conversation) while both the USB and your serial hardware are connected at the same time.

I do exactly this to communicate with my serial GPS and watch the output in the serial debug monitor (can even send arbitrary strings to the connected hardware via the serial debug monitor as well) while connected to and powered by the USB cable.  I only have to disconnect the GPS when uploading new code.

mopowered
Pages: [1]