PDA

View Full Version : Serial communication help


devil6600
10-22-07, 01:21 PM
i used the following program to receive data from a GPS (serial interface), the program is working but i get output in the form of repeated characters and most often characters are skipped, for example if the output is "$HCHDG,1.4,,,0.2,W*3C" then i am getting something like this $$$$$$$$$$$$$$$$$$$$$$$HCHHHHHDDDDDGGGG,,,,,,,,,,,,,,,,,,,,WWWWW******
333333333333333333333333333cccccccccccccccccccccccc

i have no idea what to do
how can i remove this problem?

The device is working on baud 4800, 8 data bits, no parity, 1 stop bit

#include "dos.h"
using std::cout;
using std::endl;

int main(void)
{
char st[100];
int i=0;
outportb(0x3F8+0x03,0x80);
outportb(0x3F8+0x01,0x00);
outportb(0x3F8+0x00,0x18);
outportb(0x3F8+0x03,0x03);
outportb(0x3F8+0x01,0x00);
outportb(0x3F8+0x02, 0xC7);
outportb(0x3F8+0x04, 0x0B);
unsigned char rr;
while(1)
{
rr = inportb(0x3F8);
st[i++] = rr;
if(rr==0x0d)
{
st[i] = '\0';
cout<<st<<endl;
i=0;
}
}
}

RMDumse
10-22-07, 05:57 PM
You're asking about programming on the PC, which is outside our area of expertise, but I will try to at least offer and idea what I'd check.

Your while loop where you check for characters,
rr = inportb(0x3F8);
I would guess the inportb() function merely peeks the passed location. Instead, you need another while loop waiting first to see a full character has arrived, before doing the inportb() call.

It looks to me like you're reading the same character over and over, which is likely given the greater speed of the PC compared to a 4800 baud serial stream. Sometimes it looks like you're getting a partially formed character as the bits are sliding in. Otherwise, most of the characters look like they could be valid bits coming from the GPS, with single characters repeated way too often.