Pages: [1] 2   Go Down
Author Topic: Mixed Parity Odd and Even in Serial Protocol  (Read 1992 times)
0 Members and 1 Guest are viewing this topic.
Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!


* Bildschirmfoto 2013-02-14 um 14.22.25.png (15.1 KB, 704x171 - viewed 29 times.)
« Last Edit: February 14, 2013, 08:25:39 am by schmid01 » Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1018
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
http://arduino.cc/en/Serial/Begin

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 ?
« Last Edit: February 14, 2013, 09:44:25 am by Krodal » Logged

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Offline Offline
Edison Member
*
Karma: 9
Posts: 1018
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 http://arduino.cc/en/Serial/Begin ), 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
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 534
Posts: 26970
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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                 

Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

 http://arduino.cc/en/Reference/SoftwareSerial

Lefty
Logged

Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17293
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Quote
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
Logged

Pages: [1] 2   Go Up
Jump to: