28.12.2013 Views

Serial Programming - upload.wikimedia....

Serial Programming - upload.wikimedia....

Serial Programming - upload.wikimedia....

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Serial</strong> DOS<br />

4.7.7 Command Line Input<br />

There are a number of different ways that you can "scan" the parameters that start the<br />

program. For example, if you start a simple terminal program in DOS, you can use this<br />

command to begin:<br />

C:> terminal COM1 9600 8 1 None<br />

or perhaps<br />

C:> terminal COM4 1200 7 2 Even<br />

Obviously there should not be a need to have the end-user recompile the software if they<br />

want to change something simple like the baud rate. What we are trying to accomplish here<br />

is to grab those other items that were used to start the program. In Turbo Pascal, there is<br />

function that returns a string<br />

ParamStr(index)<br />

which contains each item of the command line. These are passed to the program through<br />

strings. A quick sample program on how to extract these parameters can be found here:<br />

program ParamTst;<br />

var<br />

Index: Integer;<br />

begin<br />

writeln('Parameter Test -- displays all command line parameters of<br />

this program');<br />

writeln('Parameter Count = ',ParamCount);<br />

for Index := 0 to ParamCount do begin<br />

writeln('Param # ',Index,' - ',ParamStr(Index));<br />

end;<br />

end.<br />

One interesting "parameter" is parameter number 0, which is the name of the program that<br />

is processing the commands. We will not be using this parameter, but it is something useful<br />

in many other programming situations.<br />

4.7.8 Grabbing Terminal Parameters<br />

For the sake of simplicity, we are going to require that either all of the parameters are going<br />

to be in that format of baud rate, bit size, stop bits, parity; or there will be no parameters<br />

at all. This example is going to be mainly to demonstrate how to use variables to change<br />

the settings of the UART by the software user rather than the programmer. Since the added<br />

sections are self-explanatory, I'm just going to give you the complete program. There will<br />

be some string manipulation going on here that is beyond the scope of this book, but that<br />

is going to be used only for parsing the commands. To keep the user interface simple, we<br />

are using the command line arguments alone for changing the UART parameters. We could<br />

72

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!