C programming question
olli at lurza.secnetix.de
Tue Apr 7 04:45:11 PDT 2009
Valentin Bud wrote:
> Hello community,
> I have built with a micro controller a system of power plugs that can be
> controlled through the serial port.
> I have 2 plugs that i can start/stop and check the status of them. This is
> accomplished by sending different
> letters (eg. A/W) to start/stop one of the plugs and another set of letter
> for the other plug and one letter
> to check the status.
> Taking into account the fact that my C skills are almost 0 how complicated
> would be to write a program
> so I can control that micro controller through the serial port. Or is there
> some kind of program that can
> read/write from/to the serial port from the command line. I don't want an
> interactive program like minicom,
> just a program that connects and send a command (a letter in my case) to the
> serial port.
You can do this with existing shell tools. With stty(1) you
can select the serial port parameters on the "init" device
(see sio(4) for details about the "init" and "lock" devices).
Then you can use printf(1) or echo -n to send single letters
to the serial port.
To read single characters, you can use dd(1) with bs=1 and
count=1. Make sure that the serial line is in "cbreak" or
"raw" mode (with stty). This is a common trick to read
single characters from stdin (without having to press the
Enter key) in order to implement simple menu systems in
echo -n "Press any key: "
stty cbreak -echo
KEY=$(dd bs=1 count=1 2>/dev/null)
stty -cbreak echo
echo "You pressed the \"$KEY\" key."
To read from a serial port instead of standard input, you
have to redirect input from the serial port's device for
the stty and dd commands, of course.
> Now back to my original question, how hard/complicated will it be to write
> a C program to control the micro controller
> through the serial port.
Well, it's not hard or complicated, but it requires a certain
amount of knowledge. First you have to learn C programming.
Then you have to learn about the termios(4) interface and
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606, Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758, Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart
FreeBSD-Dienstleistungen, -Produkte und mehr: http://www.secnetix.de/bsd
"UNIX was not designed to stop you from doing stupid things,
because that would also stop you from doing clever things."
-- Doug Gwyn
More information about the freebsd-questions