Serial Parameters

I need to receive data from a source that is setup for 9600,7,e,1. I am unable to change the parameters at the source, so I must set the arduino (mega) to these parameters. It appears all of the code assumes 8,n,1 for the serial ports. Is there some way to set them for 7,e,1? I am sorry if the is too easy. I am a bit of a noob.

Serial.begin(speed);
UCSR0C = 0x24;   //  bits are: MMppSlllC
                 // where M is usart mode, p is parity mode
                 // S is stopbit select, l is byte length
                 // and C is clock polarity

(somewhat bad form to not use the symbolic constants. Oh well.)

Interestingly, there is nowhere in the current code that ever sets these parameters; it all relies on the reset state of the chip setting the mode desired. (or the bootloader, except the bootloader ONLY does the boot uart on MEGA)

So, for 9600,7,e,1 what does that come to?

Have you tried this just setting your serial to 9600? I am not 100% sure but I believe that e,7,1 will deliver 8 bit bytes with the top bit set to even parity. Just and it off with 0x3F and ignore it.

for 9600,7,e,1 what does that come to?

Um. 0x24, like I had in the first message, does the 7,e,1 part but doesn't change the speed (which is in a different register.) (I shoulda left out the comments!) (This data comes from the Atmel atmega168 datasheet, BTW.)

bill2009's idea should work too, if you're just receiving data. Come to think of it, his idea is probably better, since I'm not sure what will happen in the existing serial driver if a parity error actually occurs!

Bill2009 is correct. Just ignore the parity bit, which will be received as the most significant bit:c &= 0x7f;Note the corrected value for ANDing.

Regards,

-Mike

Try as I might, I cannot get this right. My mistake earlier was the baud rate is actually 2400. Easily corrected this. The ongoing problem is the data. I get data, but it is corrupted. I have tried 0x7f , and all of the other variations posted in these responses. I am looking at the data thru an O'Scope and it looks clean. Again , the settings are 2400, 7,e,1 for proper comm to my printer. Any other ideas?

What I am wanting should look like this.

Transactions:

Date Time Tran Acnt Drivr Vehcl Odomtr Keyboard Type Pump Prod Quantity Price Amount APR 11, 2006 15:35 0001 001 0005 ???? ?????? ?????????? 0-Normal 01 0 1-Ethanol 00000.300 $ 1.000 $ 0000.30 APR 11, 2006 15:38 0002 001 0005 ???? ?????? ?????????? 0-Normal 01 0 1-Ethanol 00000.200 $ 1.000 $ 0000.20 APR 11, 2006 15:52 0003 001 0005 ???? ?????? ?????????? 0-Normal 01 0 1-Ethanol 00000.000 $ 1.000 $ 0000.00 APR 11, 2006 15:54 0004 001 0006 ???? ?????? ?????????? 7-Comm 01 0 1-Ethanol 00000.000 $ 1.000 $ 0000.00 APR 11, 2006 16:03 0005 001 0006 ???? ?????? ?????????? 0-Normal 01 0 1-Ethanol 00000.000 $ 1.000 $ 0000.00 APR 11, 2006 16:12 0006 001 0006 ???? ?????? ?????????? 7-Comm 01 0 1-Ethanol 00000.000 $ 1.000 $ 0000.00 AUG 07, 2007 11:09 0001 001 0009 ???? ?????? ?????????? 0-Normal 01 0 1-Ethanol 00000.000 $ 1.000 $ 0000.00

But it looks like this:

Ôráîóáctioîó:

Dáte Ôiíe Ôráî Acît Driör Veècl Ïäoítr Keùâoárä Ôùðe Põíð Proä Ñõáîtitù Price Aíoõît APÒ ±±¬ ²006 ±5:35 000± 00± 0005 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.300 $ ±.000 $ 0000.30 APÒ ±±¬ ²006 ±5:3¸ 000² 00± 0005 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.²00 $ ±.000 $ 0000.²0 APÒ ±±¬ ²006 ±5:5² 0003 00± 0005 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00 APÒ ±±¬ ²006 ±5:5´ 000´ 00± 0006 ???? ?????? ?????????? ·-Ãoíí 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00 APÒ ±±¬ ²006 ±6:03 0005 00± 0006 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00 APÒ ±±¬ ²006 ±6:±² 0006 00± 0006 ???? ?????? ?????????? ·-Ãoíí 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00 AUG 0·¬ ²00· ±±:09 000± 00± 0009 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00

Ôráîóáctioîó:

Dáte Ôiíe Ôráî Acît Driör Veècl Ïäoítr Keùâoárä Ôùðe Põíð Proä Ñõáîtitù Price Aíoõît APÒ ±±¬ ²006 ±5:35 000± 00± 0005 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.300 $ ±.000 $ 0000.30 APÒ ±±¬ ²006 ±5:3¸ 000² 00± 0005 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.²00 $ ±.000 $ 0000.²0 APÒ ±±¬ ²006 ±5:5² 0003 00± 0005 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00 APÒ ±±¬ ²006 ±5:5´ 000´ 00± 0006 ???? ?????? ?????????? ·-Ãoíí 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00 APÒ ±±¬ ²006 ±6:03 0005 00± 0006 ???? ?????? ?????????? 0-Noríál 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00 APÒ ±±¬ ²006 ±6:±² 0006 00± 0006 ???? ?????? ?????????? ·-Ãoíí 0± 0±-Åtèáîol 00000.000 $ ±.000 $ 0000.00

The strange thing is that if the serial parametres was not right one would expect that everything including the linebreaks would be messed up.

Some of your text like "price" comes out correct.

Could it be some kind of codepage or character encoding that the printer should be set up to use?. What i mean is it's like the printer is printing turkish or something like that.

In the good old DOS days you could run into problems like that if you were using the wrong codepage.

Is there some kind of initialisation sequence you could send to the printer, or maybe some dip switches you could set ?

Just a wild guess.

There is no printer. There was a printer, but I am replacing it with this Arduino capturing the data. If I feed the serial port into a hyperterminal session, I get it clean, like I showed. But, capturing it into the Arduino, it is messed up, like the messed up one I showed. I can’t figure out why. I can even monitor it with Hyperterminal while capturing it into the Arduino and the Hyperterminal comes out clean, the Arduno is messed up.

Update. I created a path like this: The arduino in on Serial 1 RX, feeds out on TX 0 (So, I can see it in the Serial Monitor) and also sends it out on TX 3 which I connected to the hyperterminal. Expected to get the same garbage on both the Monitor and the Hyperterminal. However, The monitor continues to be garbage while the hyperterminal also conitnues to be not garbage. So, is this just a serial monitor problem and no real problem at all?

Is it a "normal" serial port or a TTL level serial port that you're connecting to the Arduino?

A normal rs-232 port. I have addressed the issue of the voltage by going thru a max232 chip.

Sounds like the hardware is order. Please post the Sketch.

void setup() { // initialize both serial ports: Serial.begin(2400); Serial1.begin(2400); }

void loop() { // read from port 1, send to port 0: if (Serial1.available()) { int inByte = Serial1.read(); Serial.print(inByte, BYTE); } }

This directly from the examples except for the baud rate.

Give this a try and let us know what happens...

void setup() {
 // initialize both serial ports:
 Serial.begin(2400);
 Serial1.begin(2400);
}

void loop() {
 // read from port 1, send to port 0:
 if (Serial1.available()) {
   [glow]byte[/glow] inByte = Serial1.read();
   [glow]inByte = inByte & 0x7F;[/glow]
   Serial.print(inByte, BYTE); 
 }
}

OK, I seem to have the serial data coming in OK. I am almost there. I thank all of you who have helped me. I now seem unable to make an if statement work.

I have a variable "str" which is "char" type and is 126 long. It has a lot of ascii data in it (it is full). I need to examine the value of str[28] and if it is a 2, then do something. It is a 2, but my statement won't execute. Here is the statement:

if (str[28]=='2')

Any ideas?

perhaps it is interpreting it as an integer 2 instead of an ascii "2".

maybe set a character variable and set it to a "2" and compare that instead.

so I tried this

char receipt = '2'

now my if is:

if (str[28] == receipt)

still no workie

Any ideas?

See the second sentence in reply #13.