[prog] Do I need to write a kernel module?

Tuzemi tuzemi at gmail.com
Mon Dec 28 12:35:44 UTC 2009


On Sunday 27 December 2009 12:27:13 pm softwaregurl wrote:
> I am trying to interface with a relay board originaly used by Walmart to
> switch car radios into different speakers.  They have since been obsoleted
> and replaced, but I have one.  They were designed to be controled by a
> touch screen but that was never implimented and there is no info available
> except for a pin marked 'data'. I have been controling it with MSdos and
> Qbasic (yes I know).  I found with a recording scope that I can duplicate
> the signal on a com port and convert to the level on the input by a 3
> component resistor/capacitor/transistor circuit using (don't quote
> me..pulling specs from memory) 4800bps 5 data bits 1 stop bit and no parity
> but there is a non-standard delay between the 2 bytes it needs.  In Qbasic
> I just did a for next loop. I tryed using something like this in sh
>
> echo -ne "\x31" >/dev/ttyS2
> usleep "$SLP"
> echo -ne "\x0e" >/dev/ttyS2
>
> and starting it with different nice values and $SLP as root but it looks on
> the scope like the output is still getting buffered and is inconsistant. 
> This was just a shot in the dark first try. Could I write the same thing in
> C/C++ or would it have the same sort of problem? I see Kernel land and User
> land modules but am not sure where to start.  Maybe just hack the existing
> module? I am 2 years into Linux at home but experience writing web server
> apps in Perl and some hardware and software experience back as far as the
> AppleII series.  Still pretty inexperienced at bash (sh) and have only
> dabbled in hacking C under Linux so far. If there are any questions or
> could be stated more clearly, please ask.  Some of my terminology can be
> old or use the wrong term or acronym.
>
> System specs:
> Dell PII 233
> Tiny Core Linux with custom compiled 2.6.26 kernel.
>
> ty
> -SG

Unless you're interfacing with a new kind of chip/board, you probably don't 
need a kernel module.  The RS232 interface is fully supported from userland, 
typically C-style termios (tcgetattr(), tcsetattr(), cfmakeraw(), 
cfsetispeed(), cfsetospeed(), and others).

You can set the serial port baud/stop bits/etc. from the command line via 
stty.  This sets 4800 bps/5 data bits/1 stop bit:

$ stty -F /dev/ttyS1 4800 cs5 -cstopb

You might also need to add "raw" to disable (much of) the TTY handling:

$ stty -F /dev/ttyS1 raw 4800 cs5 -cstopb

After this your echo / usleeps will work just as well as write() calls from a 
C program would.  stty essentially gives you command-line access to termios.

If you need finer-grained control of the RS232 interface, such as being able 
to explicitly control other pins like DTR, CTS, etc., you can do that with 
ioctl(TIOCMGET) and ioctl(TIOCMSET) .  I had to use this to control DTR 
myself for one device.  (Typically open() and close() on the serial port do 
the right to DTR, but my device had its own oddball logic so I had to use 
ioctl() after I had open()d it.)

Finally, if you need real D/A type control and you D/A board uses port IO 
control, you can get there on i386 with inb()/outb() and friends, defined in 
sys/io.h.  For that to work, you have to run your program as root, and you 
have to call ioperm() before calling inb() or outb().

-- 
Tuzemi <tuzemi at gmail.com>


More information about the Programming mailing list