Mixed Parity Odd and Even in Serial Protocol

Hello

I have to send a Serial Command to a Device, that needs mixed Parity. So every Word contains:

1 Start Bit
8 Data Bits
1 Parity Bit (Odd or Even)
1 Stop Bit

The first 3 Words are Even Parity, the Following Words, including the Checksum is Odd.

How can I realise that?

My Command is for Example

01h 02h 03h 04h 05h FFh

Thanks for any help!

I think it is allowed to call Serial.begin() multiple times.
Receiving data is probably interrupted, but I assume that you are not receiving data at the same time.

Using Serial.end() stops the serial port.
So I would try this:
Use Serial.begin() with baudrate and SERIAL_8E1
Write the 3 bytes.
Wait until the 3 bytes are transmitted with Serial.flush().
Use Serial.begin() once more with SERIAL_8O1
Write the next 3 bytes.
And wait again for those bytes to be transmitted with Serial.flush().

Or do you use SoftwareSerial ?

Thanks Krodal.

That meight be a way to test sending words But I think every word is ended by a LF or CR with the println command.

The chalenge is also that I have to receive an answer in of the same kind too after every sent command.

Maybe software serial is a possibility, how must that be done?

Use 9-bit data, no parity, and create the 9th parity bit in software.
Do the same receiving - check the parity in software.

Serial.println will add a CR LF, but Serial.print and Serial.write don't.

If you set the last Serial.begin(), the data can be received. But only in the same format. So after you sent the last byte, the device starts responding and that data can be received in a normal way.

CrossRoads, 9-bit data ? Is that even possible ?

As a related question, if one even enables a specific parity mode in the serial.begin command does the hardware serial library even notify the user that a parity error has ever been received? I'm pretty sure it doesn't without the user adding extra code or modifying the serial library, so why bother? Just do as CrossRoads suggested and run 9 bit mode, add the required parity on transmit and just ignore parity on receive characters, or read them as 9 bit characters and test for parity in your sketch after you receive the character from the library?

I guess to rephrase the question, does the existing hardware serial library really support parity error detection other then just generating proper parity bit on it's transmit side?

Lefty

Yes, see section 20 of the datasheet

Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits
Odd or Even Parity Generation and Parity Check Supported by Hardware

C Code examples are also provided for setting up the required registers.

CrossRoads:
Yes, see section 20 of the datasheet

Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits
Odd or Even Parity Generation and Parity Check Supported by Hardware

C Code examples are also provided for setting up the required registers.

I understand that, but does the existing arduino serial hardware library utilize parity checking? How does the serial library report that an incoming character had bad parity? I don't think it does anything presently when such a character is received. I think it's up to the user to use register commands to determine that for each and every character received?

I don't think the library does anything but 8N1. Have to look at the underlying code to really find out.

CrossRoads:
I don't think the library does anything but 8N1. Have to look at the underlying code to really find out.

No, serial.begin() was enhanced with I think IDE 1.0, to allow setting up for various setup beyond 8N1 ( see options in Serial.begin() - Arduino Reference ), but as I said prior I don't think even if setup for parity creation and detection, I don't think the serial library has a means to tell you that it's received a parity error, that has to be done with low level code by the user. Correct or wrong?

Lefty

The link only discusses 5,6,7,8 bits. Special attention needed for 9bits apparently.

Thanks for your advices so far. Is it possible to transmit the whole command breaked down to its bits,
so that I can build me own Binary Code including the start,parity and stop bits like:

1 0000 0000 1 1 instead of just 00h ?
| | | \ Stop Bit
| | \ Parity Bit
| \ 8 Data bits
\ Start Bit

schmid01:
Thanks for your advices so far. Is it possible to transmit the whole command breaked down to its bits,
so that I can build me own Binary Code including the start,parity and stop bits like:

1 0000 0000 1 1 instead of just 00h ?
| | | \ Stop Bit
| | \ Parity Bit
| \ 8 Data bits
\ Start Bit

Well that is effectively what the softserial library does, so perhaps you could read it's source code to see how they accomplish it. It's basically just 'bit banging' the serial data out a single digital output pin. Transmitting is somewhat simpler then receiving but both are contained in the SoftSerial library code.

Lefty

I just cramed the source code of softwareserial, but didn't really get it. Now I red another forum (mikrocontroller.net) and came up with the idea to send 9N1. So I could set the 9th data bit. But is it even possible to open a serial port with 9 data bits?

schmid01:
I just cramed the source code of softwareserial, but didn't really get it. Now I red another forum (mikrocontroller.net) and came up with the idea to send 9N1. So I could set the 9th data bit. But is it even possible to open a serial port with 9 data bits?

Yes and no. The existing hardware serial library offers so support for 9 bit mode, but the underlining hardware with proper register manipulations does allow for 9 data bits to be sent and received. Datasheet for the 328P has a bullet point:

Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits

But you would either have to modify the existing hardware serial library or write your own to use this addition UART hardware mode. Again the datasheet is your friend in such matters.

Lefty

Well I have some AT90S2313 left overs, and just glanced the data sheet. Looks great, since I have fill access to the whole 9 bits (data bits and parity bit). Once up on a time, I used to programm this AVRs directly with AVR Studio, so I'll try to install AVR Studio again and see, what I can do :roll_eyes:

It turned out, that it's not that easy to bring AVR Studio to work on Mac. And I'd prefere, to solve this Topic completly with Arduino. So, I thought of a Library? Does anyone know, where in the SerialLibrary the Parity is integratet?