hserout vs serout speed

womai

Senior Member
I thought the following may be of interest to many. A while ago Technical mentioned that - apart from allowing higher data rates - hardware serial (hserout/hserin) should be faster than bit-banged serial (serout/serin) because the hardware can send data in the background while the Basic program already continues. From what I know about the Microchip USART the buffer is only one or two bytes deep so short packets should profit most (once the buffer runs full the program has to wait no matter what). So I put together a small benchmark program, shown below (only one block, i.e. either serout our hserout, respectively, is active, the other one is commented out).

Basic data: baud rate 19200 for both, Picaxe 28X1 running on 16 MHz external oscillator. Loop with 10000 repeats. Each serial command sends out a packet of 1, 2, 3, and 4 bytes to see how it scales with packet size. All times are in seconds

Code:
packet  hserout serout
(bytes) secs    secs
   1     5.6    10.4
   2    10.8    16.6
   3    16.0    22.9
   4    21.4    29.1
The trend is nicely linear for both as expected, and (also as expected) the hserout has much lower overhead (because it is partially working in the background):

hserout: 0.53 msec/byte, plus 0.03ms overhead
serout: 0.62 msec/byte, plus 0.4ms overhead

The surprising part is that in addition to smaller overhead, also the incremental time per byte is significantly less. What we learn from all that is

- hserout has a significant speed advantage even at the same baud rate
- if possible, it is best to send smaller packets and do work in between, rather than collecting all the data and sending one big chunk. That way the buffer never runs full and more of the data can get sent in the background.

Wolfgang


Here is the code:

Code:
setfreq em16

hsersetup B19200_16, %00

high 1 ' status LED on
for w0 = 1 to 10000
    serout 7, N19200_16, ("ABCD")
next w0
low 1 ' status LED off
end


'high 1
'for w0 = 1 to 10000
'    hserout 0, ("ABCD")
'next w0
'low 1
'end
 
Last edited:

womai

Senior Member
Now I had a look with a scope as well. In the test routine, the hserout is really sending continuously, i.e. there is no time lost due to the for-next loop because that time gets used to empty out the send buffer. Format is truly 1 start bit, 8 data bits, 1 stop bit. Guess that's possible because the Picaxe interpreter can execute the for-next loop code as well as put one byte into the buffer while the previous one is still being transmitted.

On the other hand, the serout command is sending 3 stop bits instead of just one, which explains the additional 0.1msec per byte. Probably because the bit-banging routine sends the byte and only then fetches the next one, so there is some dead time between bytes.

Wolfgang
 
Last edited:
Top