Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 7 Apr 2009 13:44:46 +0200 (CEST)
From:      Oliver Fromme <olli@lurza.secnetix.de>
To:        freebsd-questions@FreeBSD.ORG, valentin.bud@gmail.com
Subject:   Re: C programming question
Message-ID:  <200904071144.n37BikRT005234@lurza.secnetix.de>
In-Reply-To: <139b44430904070241j5227d178jd75f6a93057a150a@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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
shell scripts:

 echo -n "Press any key: "
 stty cbreak -echo
 KEY=$(dd bs=1 count=1 2>/dev/null)
 stty -cbreak echo
 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
related things.

Best regards
   Oliver

-- 
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200904071144.n37BikRT005234>