Saturday, 2 June 2012

Create simple RS232 pushbutton switch code

A 14 year itch

I was jealous of a housemate for a reason. We both had jobs tiding us over through the writing up stage of our PhD's. We had both studied Physics at Newcastle and we were both living in the same house in Nottingham, studying Chemical Physics and Physics at The University of Nottingham. Generous as ever, he got me a temporary job at Nottingham Trent University (NTU). He was demonstrating labs on computer control of electronic circuits using C.  NTU were looking for someone to help in the labs for HTML for scientists.

 I knew enough HTML tags to get by and had coded for peanuts previously. He told the NTU staff that I could do the job, they interviewed me and I was a part-time lecturer/demonstrator.  He had the more interesting job of the two of us  (I wouldn't have been capable of doing it anyway). His PhD involved a lot of computer controlled NMR hardware and writing custom C. Mine was pretty much 1970s hardware and then compiling code to run simulations under *nix.

 I've always wondered how his labs worked - talking to components at a low level using Serial ports and such-like.

 Half way through demonstrating the course (the aim of the lab was to build a homepage), a new version of Netscape was rolled out at NTU with a built-in WYSIWYG HTML editor. Most students gave up learning HTML tags and designed a website using the editor.

It turns out it is fairly simple to create a pushbutton switch which can be detected by a serial connection under Linux. To do this, I took apart a serial connector, wiring Pin 4 to one end of the pushbutton switch and Pin 1 to the other end of the pushbutton switch.

(Diagram created using Fritzing). A PL2303 based USB->Serial adaptor with my C cobbled together:

It is heavily based on the excellent setserial (standard debian/ubuntu package), with the ncurses pieces removed. The code is compiled using:

$ gcc -o simple simple_switch.c

Either run this code as root, or add yourself as a user to the dialout group.

# simple /dev/ttyUSB0

On my machine it gives:

tng@marwood:~/$ ./simple /dev/ttyUSB0
Attempting to open device /dev/ttyUSB0 
CAR pushed. Port 1 DB9
CAR pushed. Port 1 DB9


Sebastian Rolek said...

Hello. You can also use DTR-DCD/CTS/RNG/DSR and have four independent buttons. In my opinion 2s dealy are not necessary.

if (!!(arg & TIOCM_CAR) == 1) {
printf("CAR OK. Pin DTR-CAR (\n");
usleep (250000);
} else if (!!(arg & TIOCM_CTS) == 1) {
printf("CTS OK. Pin DTR-CTS (\n");
usleep (250000);
} else if (!!(arg & TIOCM_RNG) == 1) {
printf("RNG OK. Pin DTR-RNG (\n");
usleep (250000);
} else if (!!(arg & TIOCM_DSR) == 1) {
printf("DSR OK. Pin DTR-DSR (\n");
usleep (250000);

om lugus said...
This comment has been removed by the author.
om lugus said...

I want to turn of/on a 'LED' by using rs232.

1. If I press enter/return button the light/LED will be turn on
2. If I press esc/escape button the light/LED will be turn off.

How to do that? Thanks for your attention.