Author |
Topic: Serial I/O Runtime Overhead (Read 1531 times) |
|
g3nrw
Junior Member
member is offline


Posts: 74
|
 |
Serial I/O Runtime Overhead
« Thread started on: Oct 18th, 2013, 6:45pm » |
|
Hi Richard
[First, I must say that having used BBC4W for a few weeks now, I am finding it more and more useful. Thank you for a superb product].
However, I have a snag with serial I/O. Here is a code snippet from a program I have been working on. It simply transfers ASCII characters in both directions between two serial ports, A and B:
Code:
PortA = openup ("COM7: baud=115200 parity=N data=8 stop=1 rts=on")
PortB = openup ("COM5: baud=115200 parity=N data=8 stop=1 rts=on")
txchar$ = ""
rxchar$ = ""
repeat
char% = ext#PortA
if char% <> 0 then
txchar$ = chr$(bget#PortA)
if txchar$ <> "" then
bput #PortB, txchar$ ;
endif
else
char% = ext#PortB
if char% <> 0 then
for cnt%=1 to char%
rxchar$ = chr$(bget#PortB)
bput #PortA, rxchar$ ;
next
endif
endif
until false
This is fine. Logically it works well -- no problem.
But the runtime overhead is enormous. Even when no traffic is passing between the ports, the program consumes about 25% CPU utilization on my Windows8/64 5-core 3GHz machine. Most other programs -- including a CPU-hungry software-defined-radio program -- consume well under 5%. And on my old, slow Windows XP/32 machine, this program devours around 90% CPU!
Compiling the program doesn't seem to make any noticeable difference to the utilization.
Right now this is a showstopper for me. Is there any way to work around this overhead?
Ian
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Serial I/O Runtime Overhead
« Reply #1 on: Oct 18th, 2013, 7:50pm » |
|
on Oct 18th, 2013, 6:45pm, g3nrw wrote:This is fine. Logically it works well -- no problem. |
|
Actually I wouldn't say it's "fine". For example why are 'port A' and 'port B' handled differently? Is there some reason for the 'asymmetry'? Also, txchar$ is tested for being an empty string, but it never can be!
Quote:But the runtime overhead is enormous. |
|
Indeed! It's an 'infinite loop' so will inevitably use 100% CPU time (if it's registering only 25% that simply means that you have a CPU with four cores). Even worse, it risks overheating the chip - you will probably notice the cooling fan ramping up - and will rapidly run down the battery on a laptop.
It's for all these reasons that you should never write a program that, whilst 'idling' (i.e. with nothing useful to do), actually races around a loop as fast as it can.
The simplest way to solve the problem is to add a WAIT 0 statement in the loop. Normally that will be an entirely satisfactory solution but it will introduce a small latency (if an incoming serial character arrives during the WAIT period it won't be processed immediately).
In the (very) rare situation that the small latency is unacceptable, you would need to set up a 'serial event' and then wait on that event. That involves using the Windows API.
Richard.
|
« Last Edit: Oct 18th, 2013, 7:50pm by admin » |
Logged
|
|
|
|
g3nrw
Junior Member
member is offline


Posts: 74
|
 |
Re: Serial I/O Runtime Overhead
« Reply #2 on: Oct 18th, 2013, 8:42pm » |
|
Now that you say it that way, it's quite obvious! The WAIT 0 fixed the "problem", of course.
(Don't worry about the other seemingly incorrect/superfluous statements. This fragment was lifted from a much larger program, where the statements made sense -- I just didn't get around to sanitizing everything).
Thanks for your help. The show is back on the road again!
Ian
|
|
Logged
|
|
|
|
|