Reading Stop and Start Bits


I want to do a bit error rate analysis over a particular serial channel. I can easily send the bytes using my laptop. The issue is to receive ALL the bits, including the start and stop bits (in this case there is one of each with no parity). This is to able to check whether there was an error in those bits.

The problem is that the regular Arduino digitalRead is way too slow and misses most of the bits. I wanted to know if anyone would know how to maybe modify the software serial library to somehow feed me back the start and sop bits, or any kind of interrupt method (I have been trying to use interrupts but to no avail.

At first I'm trying to read the bits at 19 200 baud, but would love to read them at 57 600.

I would be very grateful for any input!


What? The start bit is always 0 and the stop bit always 1. Why do you want to read them?

If the stop bit is not 1 you will get a framing error.

You can't have an "error" in the start bit (by definition). If it isn't 0 the byte is not read. If the stop bit is not 1 you get a framing error.

That's my problem though. If I receive "0|10101001|11|10011010|1" for example (I have seperated the start and stop bits with '|'). So if the second start bit is an error (as I've shown with bold) then the second byte is going to be wrong (will start late) and will be extremely difficult to say where the error was exactly. But I need to know exactly which bit was an error because the point is to send a stream of bits over the channel and then compare the received bits with the original stream to 'examine' the nature of the bit errors (this is for research).

EDIT: Just to clarify, the channel is going to be quite noisy, and this will affect the bits that are received. So the sender will send a '0' start bit but the receiver may detect a '1' instead, which is why I would like to record all bits received.

You can't start with a 1 start bit, that's the point. A serial byte starts with 0. You simply can't start with 1.

Let me try clarify some more. I'm sending the serial data to a transceiver that sends the information over a powerline. The receiving transceiver pumps out whatever bits it receives on the power line (the transceivers use a special phase modulation to send/receive bits over the power line). However, the power line is noisy, so the sender sends a 0 start bit but the receiver detects that a 1 was sent. The problem is that if that error occurs on a start/stop bit then I can't use ordinary UART to detect it because that will just produce frame errors and/or produce the wrong byte and I will lose out on detecting which bits caused the error.

I just need some way of sampling the serial output of the receiver fast enough to be able to record the full bit stream of what the receiver is detecting.

The serial has to be sent in one long stream and I can't have 'breaks' inbetween because the point is to characterise the powerline as a communication channel in terms of bit errors, and having breaks will not give a good representation.

Are you talking about synchronous or asynchronous serial communication? I the case of asynchronous serial communication the byte does never start with a 1 bit but always with a 0 bit and you cannot detect a 1 start bit because the line holds the 1 until the next start bit 0 has to be sent.

Yea asynchronous. That's kind of my problem. I'm going to be sending a continuous stream of bits, and if there's an error in the start bit then I'm not going to detect it.

For example, I send 2 bytes, one straight after the other. The output of the sender onto the powerline will be 0|10100110|10|11000111|1. So there is a 0 start bit, followed by 8 data bits, then a stop bit and then the start bit to start the next byte. But now let's say the noisy powerline corrupts one of the bits and the following sequence is received by the receiving transceiver: 0|10100110|11|11000111. Due to the corrupted bit, a normal UART will not tell me that there has been an error there and instead just send to me: 00111111 as data bits for the second byte.

Remember the transceivers have 2 interfaces: serial and the powerline. Whatever it receives on its serial rx line it pumps into the powerline, and whatever it detects on the powerline it pumps into its serial tx line. So the one transceiver will be receiving a nice long stream of serial from my laptop, which it will pump into the powerline. The receiving transceiver will then receive the bits. The transceiver holds its tx line high (as usual serial) and then when the powerline modulated signal is detected it changes it to low and high as it detects the signal (I'd imagine this is similar to how wireless devices that interface with serial work as well, but I'm not sure).

Point is I need a way to periodically sample the serial line at a high speed, but my Arduino does not seem to be fast enough, but I was thinking surely it must be fast enough somehow if it has a software serial library it must be able to sample it that quickly.

So at 57 600 you have about 17uS per bit. That's only 27 instructions to sample and do something with the results.

No chance I would say unless you just want to do something really simple when you detect an error, like turn a LED on or increment a counter.

Why not just buy a logic analyser, or do you need to sample over a long period then analyse the results.

I gather you have access to both ends if the wire so you can compare in vs out?


It seems you control both sides of the communication. Why don't you use Manchester encoding to have at least one state change in every bit sent. Then use some CRC to check for transmission errors. This has the drawback that higher transmission frequencies for the same amount of data are needed but if you have such a noisy communication channel you probably have higher transfer rates in the end.

Ya that's what I'm slowly figuring out - it's too slow :frowning: Maybe the new Arduino Due would be able to do the trick if I can get a hold of it?

A logic analyzer would have been perfect, but yes, that's exactly what I have to do - sample over a long period of time and analyze afterwards (and the analyzers I have looked at can not hold nearly enough samples).

I do have access to both ends. My plan, though, was to generate a pseudo random sequence of bits and send them from my laptop. So I'll have that particular sequence stuffed with start and stop bits from my laptop's UART. Then, obviously I know what the sequence must be and thus can compare after receiving.

pylon, not such a bad idea but involves a lot of implementation work! If all else fails I'll look into it.

For $150 you can get a Salae LA that will sample to disc, AFAIK the sample size is essentially unlimited, but then you have to analyse the data. I think you can export to various file formats but I’ve not looked into that.

All you really need to do as far as I can tell is compare one bit against another and count the differences. The actual data doesn’t matter. Start and stop bits don’t matter. It’s just a bit stream and you want to see if it comes out the same way it went in, and if not how many times was it different.

I reckon you could possibly do that,

write bit
wait half bit time
read bit
if <> inc counter
wait half bit time
dec loop counter

either in C or maybe drop to assembler.

Of course with 27 cycles the “wait half bit time” is probably not actually implemented as the loop overhead would about do it.

A Due would have no trouble.


I'll look into this Salae LA, sounds particularly useful.

Unfortunately it is the bit error positions that are just as important as the error event itself because I need to make a statistical model of the channel (as part of my Master's Thesis). The errors would usually occur in bursts and thus the positions of the errors to observe these bursts are quite important. Maybe another way to do it is have a counter count the number of bits between errors and when there is an error, save the counter value then reset the counter.

I'll also see if I can get a hold of the Due as soon as possible.

Thanks for your help! I'll post again if I get it to work.

PS I hate assembler!

I doubt assembler will do much. You still only have a few microseconds.

Logic analyzer: Logic Analyzers from Saleae - #1 with Professional Engineers

You can take 10 billion samples with that. That should do it, if you got one of them. ($US 149).

You can save the samples to disk, then analyze them.