Serial.write logic 0 between bytes?

Is it possible to have the Arduino write a logical 0 between bytes written on a serial output?

I have a project that seems to use a 0 between bytes for timing or something.

Yes

for timing or something.

It may be a good idea to find out why they are required.

Ryanthegreat1:
Is it possible to have the Arduino write a logical 0 between bytes written on a serial output?

I have a project that seems to use a 0 between bytes for timing or something.

Maybe your project requires 2 stop bits? The default Arduino behaviour is 8N1, but you can change that. See the Reference page.

Post a link to the datasheet for the device you are trying to communicate with.

…R

I did find where to change the parity and stop bits. 8N2 is almost right but is not exact and this device is being picky. Also tired the even and odd parity bits with various stop bit combos. Nothing seems to be a perfect match. Really the best I am finding is 8N1 with an added 0 between each byte.

I have no datasheet, and I doubt the vendor is going to supply it. I have reversed engineered quite a bit out of it. I will post up some examples of what I am seeing on the bus tonight. I have both the sender and receiver of this system but I want to replace the sender with an Arduino for better control.

If you really want to write your own alternative to the standard Serial system the code in Yet Another Software Serial might provide a starting point.

...R

Ryanthegreat1:
Really the best I am finding is 8N1 with an added 0 between each byte.

Isn't that exactly what you get with 8N2? If not, exactly how are they different? Are you using an oscilloscope or logic analyzer to measure the timing?

johnwasser:
Isn't that exactly what you get with 8N2? If not, exactly how are they different? Are you using an oscilloscope or logic analyzer to measure the timing?

Software oscilloscope with serial decoding. A Picoscope.

With serial decoding set to 8n1 it decodes perfect other than the additional 0's between bytes. Changing to any sort of parity bit or 8n2 the decode goes all wonky.

When writing from Arduino in 8N2 I get more than the single 0 between some bytes.

Screenshot / photo?

septillion:
Screenshot / photo?

Will post examples tonight.

Can you just Serial.write(0)?

KeithRB:
Can you just Serial.write(0)?

I think that would write a full byte equal to 0 and the start stop bits?

Yeah. He never said how much was too much....

Ryanthegreat1:
With serial decoding set to 8n1 it decodes perfect other than the additional 0's between bytes. Changing to any sort of parity bit or 8n2 the decode goes all wonky.

Once we see the waveform (with timing information) we'll be able to see what the protocol is. I hope it is something standard.

If you do Serial.write(0) on an 8N1 line you would get nine LOW bits (one start bit and 8 data bits) and one HIGH bit (stop bit) followed by HIGH signal until the next start bit. If you do Serial.write(0xFF) on an 8N1 line you would get one LOW bit (start bit) and nine HIGH bits (8 data bits and one stop bit) followed by HIGH signal until the next start bit.

Maybe the communication protocol is synchronous.

Okay here is some data off the scope.

Original is what the original signal looks like. 8N1 and 8N2 is what the Arduino is putting on the line.

9N1 seems to fit a lot better on the serial decode.

I don't see a 9N1 option for Serial.begin.

8bit with a space parity and 1 stop bit also fits well and passes parity on all bytes but the first one.

Also don't see an 8S1 option.

Reading section 22 in the ATmega2560 datasheet I see the hardware supports 9 data bits. I could fake 8S1 by running 9N1 and never exceeding FF for the byte value. The 9th bit should always be a 0 right?

I am guessing to get 9N1 I will have to get into modification of the USART registers?

What do you want exactly? A 0-bit after each byte? Why? A zero bit is a start bit. That doesn't make sense. If you want to emulate 8N2 just add a slight delay after each byte.

Yes a 0-bit after each byte but before the stop bit. A space parity I believe is what I am after. Just no direct support for it with the Arduino.

I did a 9-bit version of HardwareSerial here: Mods to HardwareSerial to handle 9-bit data - Libraries - Arduino Forum

If you make the 9th bit zero you should achieve what you want.

Amazing! You may have just saved my project! I will fiddle with it and report back.

Thanks everyone for the guidance!

The 9th bit depends of what you need there. It usually has one of two meanings:

  1. address
  2. data

So you can e.g. set the 9th to 1 and connected devices will consider it an address. If it is the device's address, it will accept the data that follow with the 9th bit set to 0. If it is not its address, it will ignore the data.