simple serial issue

Hi
I'm connecting my GPS to pin 0 and 1 on my arduino, in the hope of receiving any data from it. The GPS outputs EMEA-data as soon as it is turned on. It's led is turned on, so it has poower, and I'm running this code on the arduino:

while (true) {
Serial.println("true-loop:");
incomingByte = Serial.read();
Serial.println(incomingByte);
}

the only output i get is:

true-loop:
-1
true-loop:
-1

etc...

I guess the "-1" means that no bytes were read. I've tried switching around the TX and the RX lines to the GPS, it makes no difference. The GPS runs at 4800bps and the arduino is configured to use 9600, but this shouldn't make a difference. The GPS should still send something, even though the arduino will interpret it weirdly. I get NOTHING - not even something weird. What can be wrong? The same setup worked fine with a BASIC STAMP.

The GPS runs at 4800bps and the arduino is configured to use 9600, but this shouldn't make a difference.

It might matter. A lot of things I have worked with will reject anything that is not the correct baud. If this is the case with the Arduino, then the data will be ignored before ever reaching the serial buffer.

Try matching the baud and make sure your voltage levels are correct. on the TX/RX pin, you may need a TTL to RS232 (Logic level) converter.

don't assume that baud rate mismatch will do something... very often a mismatch will result in nothing being displayed.

You can avoid those -1 outputs by checking if data is available before trying to read it.

while (true) {
if(Serial.available() ){
incomingByte = Serial.read();
Serial.printlincomingByte);
}
}

But you will not get anything useful if you don't have the baud rate set correctly.

Yes, first thing to do is to set the baud rate to 4800 for the NMEA data.

Now the baud rate is correct:

while(true) {
int serialInput;
Serial.begin(4800);
serialInput = Serial.read();
Serial.begin(9600);
Serial.println("Seria input was:");
Serial.println(serialInput);
}

But the output is still always -1

I measured the voltage the GPS receives: its 4,75
Next step will be to hook it up to the Basic Stamp and make it work there, and then transition the RX and TX line to the Arduino, but leave it power fed from the Basic Stamp, to rule out power supply being an issue. But that'll be tomorrow.. I have to go to sleep now.

You are still setting it to 9600
Take out the second Serial.begin()

Also, that should be in the void setup()

I set it to 9600 when the communication is intended for my PC. I put it back to 4800 when I take data in from the GPS. The GPS can't receive data, and therefore shouldn't care about the bits floating to it from the 9600baud sentences sent to the PC.
The code is in the void loop() method (I'm not sure whether this method will loop all by itself, so I built my own loop ("while (true)")

And now it got a proper 5.02v power supply. It still doesn't work though. I can't get my Basic Stamp Editor to recognize my BS2pe, so I can't test whether it would work with the basic stamp. It did make it work with the basic stamp this summer though..
crap.

"Should" and reality are often very different. RS232 is not a multi-drop interface. Pins 0 and 1 are TTL level, not PC standard RS232...is your GPS TTL-serial? If the PC is talking to the Arduino via the USB port, then those pins are under full control of the internal FTDI chip. With the GPS connected to those pins, you would be seeing the data, garbled or not, come into the PC before the Arduino even had a chance to see it. Your while-true loop may be interfering with the Arduino's ability to check the serial buffers (I don't know if that's true, I have little knowledge of how the Arduino handles this).

Just a few of the possible problems here.

The GPS can run of both 12v and 5v. This I found out by experiment. Also, it can communicate with my basic stamp, which is purely TTL. So yes, my GPS is TTL-serial.

I will check your suggestion about Arduino not seeing the signals because of the FTDI "taking over" by listening to the FTDI using windows Hyperterminal (can't do it right now though, as I'm at work..).
It seems implausible though, as that would render the hardware-serial-port of the arduino useless for anything else than PC-communication, and I know other projects has used the serial interface of the arduino to talk with other devices than the PC.

I'll also try to remove the while loop.

have you tried the Software Serial library for the GPS? This would keep the normal serial for communicating with the PC, so theres no problems on that end and you don't have to kee changing the baud rate.

Simple thing to try: are you sure you have the right gps wires connected to the right pins? "tx" and "rx" are ambiguous after all; one devices TX MUST talk to the other devices RX.

to macegr:
I tried listening to the COM-port using hyperterminal, but of course the result is the same as when listening to the line via the arduino serial monitor. IE. no extra (garbled) stuff from the GPS were to be found. No matter whether it was the arduino or the FTDI chip that handled the incomming data from the GPS, it would be the PC that received it. And it doesn't.

I also removed my own "while (true)" loop in case it caused problems. It didn't change anything. I also found out that "void loop()" function loops by itself, so no use for my own while loop.

to westfw:
I always try both options, just in case.

I tried the SoftwareSerial. No luck. Then I tried connecting the GPS to the PC. No luck either. I'm starting to suspect that my GPS is somehow defective. Tomorrow I'll try with its original cable, just for a good measurement, but I'm too tired to search for that now.
Sleep well!

So, not to beat a dead horse, but it sounds really fishy for the GPS to be "both 12V and 5V". Normally a TTL-serial interface won't work at all with equipment expecting 12V RS232 levels. And the fact that you used the Basic Stamp isn't proof you were using TTL...it's not purely TTL. The Basic Stamp has RS232 level-conversion hardware if you're using it with the pins that are also used for downloading the program, which happens if you choose pin 16 in the SERIN and SEROUT commands.

to macegr:
Are you sure on that one? I have a RS232 converter myself, and judging by the size of the RS232 conversion chip itself, I don't think this functionality can possibly be embedded on the Basic Stamp. Also, on my Basic Stamp development board, there's a separate RS232-conversion chip right behind the 9-pin RS232-port. This would be redundant if the BS could do the conversion itself.

Will be up and looking for my original GPS cable in just a minute :slight_smile:

My GPS actually WORKS. Now I'm using its original cable, attached to a PC and then using hyperterminal for communication. Loads of NMEA-sentences are comming in.
The original cable is a USB to PS/2-jack converter. The GPS is a little black box with a cable with a PS/2-jack. That is, I don't believe the GPS is using the true PS/2-protocol - it's just using the same jack. If it were using the PS/2-protocol, my Basic Stamp wouldn't have been able to communicate with it using its standard Serial library. Also, In windows XP, the adapter is listed as a "USB-to-serial bridge".

All this doesn't solve my problem though - just puzzles me even more. This really sucks. I hadn't expected serial communication using an arduino to be so impossible.

This really keeps puzzling me. This is the code I'm using now:

#include <SoftwareSerial.h>

#define rxPin 2
#define txPin 3

SoftwareSerial softSerial = SoftwareSerial(rxPin, txPin);

void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
softSerial.begin(4800);
Serial.begin(4800);
}

void loop() {
char someChar = softSerial.read();
char oneMoreChar = Serial.read();
// print out the character:
Serial.println("Output: ");
Serial.println(someChar, DEC);
Serial.println(oneMoreChar, DEC);
delay(1000);
}

As you see I check for input on both the hardware- and the software-serial. I attach the TX line from the GPS to the pins 0,1,2,3 ten seconds at a time. I always get this output:

Output:
0
-1

That is, I get "0" from the hardwareserial and "-1" from the softwareserial.

My PC is listening using 4800 baud, so no need for baud-changing all the time.

This has already been mentioned but, Sounds to me like your GPS is RS-232 12v, and I believe the Stamps are as well, depending on how old it is.

I just got my Arduino talking to my PC, the Arduino is TTL logic level or 3.3v. The PC expects 12volt levels. I used the ST232 to convert the TTL to RS-232, with out it, my PC shows the same thing you are getting -1 or 0, over and over, or sometimes nothing at all.

Disconnect your com cable and leave the GPS powered. Measure the voltage on the TX pin of the GPS to ground. It's best with a analog meter because depending on how often the GPS sends data the meter will jump around so you might need to put your meter on high hold. Analog meters you can see bounce as data is sent.

Anyways, if you see levels > 5 volts, then you will need to convert TTL/CMOS to RS-232

What GPS are you using? Which pin numbers from the GPS are you connecting to which pins on the arduino? Looking at the code I can see you're a bit confused - the -1 is coming from the hardware serial (connected to your PC) not the software serial. If you used more meaningful variable names you'd probably avoid some confusion.

That said, the -1 is -expected- in this case and mem explained why earlier in the thread. Serial.read() will return -1 unless there is data available on the port. You're not testing to see if there's data before you do your read and since your PC isn't actively sending data you're always going to get a -1 out of it.

Post a photo of the wiring going from your GPS to your arduino - it might offer some clue as to what's going on here. Serial IO is really really easy on the Arduino so you've probably just made a dumb mistake. Happens to all of us.