Serial parity Even

Hello,

I hope somebody can guide me, I am not an expert, but I have some knowledge about programming.

I am writing a simple program using Arduino IDE, and I communicate it with a serial machine. This machine expects first byte with Even party and the rest with Odd parity (8bits).

I use to see exactly what I'm sending though Serial a Logical Analyzer.

I setup the Serial with Even parity 8bits and I don't know why when I'm writing 0x02 0x3D on the Analyzer I see 0x102 0x13D (expected 0x102 0x03D) and when I'm writing 0x02 0x56 I see 0x102 0x056 which is correct.

Don't understand why with 56 works properly and with 3D does not works.

void setup() {
   delay(2000);
   //Output is correct in the Analyzer: 0x102 0x056
   Serial2.begin(19200,SERIAL_8E1);
   Serial2.write(0x02);
   Serial2.write(0x56);

   delay(3000);

    //Output in incorrect. I'm getting 0x102 0x13D. Expected: 0x102 03D
   Serial2.begin(19200,SERIAL_8E1);
   Serial2.write(0x02);
   Serial2.write(0x3D);  
}

I also tried to change parity after writing 0x02 but the same result.

Thank you.2

I have executed your following sketch using Serial2 Port and correctly received with setting of 19200, SERIAL_8E1. So, the parity encoding and decoding is correct.
Sender Code:

void setup()
{
  Serial2.begin(19200, SERIAL_8E1);
}

void loop()
{
  //Output in incorrect. I'm getting 0x102 0x13D. Expected: 0x102 03D
  Serial2.begin(19200, SERIAL_8E1);
  Serial2.write(0x02);
  Serial2.write(0x3D);
  delay(2000);
}

Receiver Code:

void setup() 
{
  Serial.begin(19200);
  Serial1.begin(19200, SERIAL_8E1);
}

void loop() 
{
  if (Serial1.available()) 
  {    
    Serial.print(Serial1.read(), HEX);  
  }
}

smk.png

smk.png

Hi GolamMostafa,

Thank you your answer. The Output inside monitor is looking good also on my machine.
My issue is when I'm checking the parity with Logical Analyzer.

I have attached a print screen from Analyzer.

Regards,
Radu

why different parity for different bytes? and how do you check for it?

1. Upload the following sketch; where, only 0x02 is being sent after each 100-ms interval.

void setup()
{
  Serial2.begin(19200, SERIAL_8E1);
}

void loop()
{
  //Output in incorrect. I'm getting 0x102 0x13D. Expected: 0x102 03D
  Serial2.begin(19200, SERIAL_8E1);
  Serial2.write(0x02);
  delay(100);
}

2. Synchronize your analyzer/scope (could be difficult job to synchronize an asynchronous signal; where is the trigger signal?).

3. Check that the following pattern sustains in the analyzer/scope.
asynFrameEP.png

4. In my scope, I can see the frame very well 0 0 1 0 0 0 0 0 0 1 1; but, it is not synchronized.

asynFrameEP.png

Thank you for your answer.

Basically this is what I need to do. I have to mix the parity.

void setup() {
  Serial2.begin(19200, SERIAL_8E1);
  Serial2.write(0x02);
  Serial2.begin(19200, SERIAL_8O1);
  Serial2.write(0x3D);
}

But is not working as expected. The output for 0x02 is 0x000 (error) in this case.

by the time you change the parity, the 2nd byte have probably already been received.

why different parities for different bytes?

gcjr:
by the time you change the parity, the 2nd byte have probably already been received.

why different parities for different bytes?

Is a machine requirement.

the arduino may not be capable of doing this.
you may be better off ignoring parity unless there is a high chance of line errors

Try

void setup() {
  Serial2.begin(19200, SERIAL_8E1);
  Serial2.write(0x02);
  Serial2.flush();
  Serial2.end();

  Serial2.begin(19200, SERIAL_8O1);
  Serial2.write(0x3D);
}

aarg:
Try

void setup() {

Serial2.begin(19200, SERIAL_8E1);
  Serial2.write(0x02);
  Serial2.flush();
  Serial2.end();

Serial2.begin(19200, SERIAL_8O1);
  Serial2.write(0x3D);
}

I tried with your code but the machine is now receiving 2 separate commands.

I managed to change the parity while writing with Software Serial. I don't know now, what are the desavantages using Software Serial.

Thank you.

Software Serial's main disadvantage is speed. It won't run at 115200 for example. I think 38400 is about the best it will do reliably.

ArduProim:
I tried with your code but the machine is now receiving 2 separate commands.

Separate in what sense? Serial is generally regarded as asynchronous - you can never predict the length of time between characters.

Is this procedure just a trick to produce a certain arbitrary sequence of bits (since you haven't said anything about your "machine")?