The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Use a Serial Console with Mac OS X Leopard joecassara OS X (Apple) 3 06-23-2009 05:01 PM
Serial Communication from shell Mac OS X PHP script cpfogarty OS X (Apple) 1 11-07-2008 02:45 AM
Controlling GUI from Shell Script rummage_bin UNIX for Dummies Questions & Answers 2 11-04-2008 09:23 AM
Controlling GUI from Shell Script rummage_bin Shell Programming and Scripting 0 04-28-2008 07:50 PM
PERL:controlling serial ports ? perleo Shell Programming and Scripting 1 06-13-2003 03:42 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-10-2009
Ishikawa Ishikawa is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 4
Controlling a RS-232 Serial Console from a Shell Script

I'm using OpenBSD 4.4 and I have a power strip that can be controlled through a RS-232 serial console. It works great from cu and minicom but it doesn't support any way of controlling it programatically, just a console designed for humans. I want to be able to send commands and read the output of the commands through a shell script or some other program that can be called from a shell script. I'm not necessarily looking for solutions to the following problems I'm just looking for any way to make this work.

I looked into using a C program that interacts with the serial ports using termios.h, unistd.h and fnctl.h but it looks like I would be reinventing the wheel and doing tons more work than I need to.

I also tried using expect to control cu. This seems like the best way of doing it since expect is designed to interact with CLI's that were designed for humans. It works fine and turns things on and off until expect reaches a read statement following an expect statment. See the bottom code window.


An example of it working properly with cu:
Code:
# cu -l /dev/tty00 -s 9600
Connected


RPC-22 Series
(C) 2000 by BayTech
F1.10

Option(s) Installed:
True RMS Current

RPC-22>status
True RMS Current:   0.5 Amps
Maximum Detected:   0.7 Amps


 1)...Cable Modem     : On
 2)...Router          : On    Locked
 3)...Switch          : On    Locked
 4)...Outlet  4       : Off
 5)...Outlet  5       : Off
 6)...Outlet  6       : Off
 7)...Outlet  7       : Off
 8)...Outlet  8       : Off
 9)...Outlet  9       : Off
10)...Outlet 10       : Off
11)...Outlet 11       : Off
12)...Outlet 12       : Off

Type "Help" for a list of commands

RPC-22>on 4,5,6
RPC-22>status
True RMS Current:   0.4 Amps
Maximum Detected:   0.7 Amps


 1)...Cable Modem     : On
 2)...Router          : On    Locked
 3)...Switch          : On    Locked
 4)...Outlet  4       : On
 5)...Outlet  5       : On
 6)...Outlet  6       : On
 7)...Outlet  7       : Off
 8)...Outlet  8       : Off
 9)...Outlet  9       : Off
10)...Outlet 10       : Off
11)...Outlet 11       : Off
12)...Outlet 12       : Off

Type "Help" for a list of commands

RPC-22>

An example of it failing to work with expect controlling cu. This happens whenever a send statment is used after a expect statement:
Code:
# expect -d
expect version 5.43.0
argv[0] = expect  argv[1] = -d
set argc 0
set argv0 "expect"
set argv ""
expect1.1> spawn cu -l /dev/tty00 -s 9600
spawn cu -l /dev/tty00 -s 9600
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {1870}
1870
expect1.2> send "off 12\r\n"
send: sending "off 12\r\n" to { exp5 }
expect1.3> send "on 11\r\n"
send: sending "on 11\r\n" to { exp5 }
expect1.4> send "status\r\n"
send: sending "status\r\n" to { exp5 }
expect1.5> expect RPC-22>

expect: does "" (spawn_id exp5) match glob pattern "RPC-22>"? no
Connected


RPC-22 Series
(C) 2000 by BayTech
F1.10

Option(s) Installed:
True RMS Current

RPC-22>off 12
RPC-22>on 11
RPC-22>status
True RMS Current:   0.4 Amps
Maximum Detected:   0.7 Amps


 1)...Cable Modem     : On
 2)...Router          : On    Locked
 3)...Switch          : On    Locked
 4)...Outlet  4       : On
 5)...Outlet  5       : On
 6)...Outlet  6       : On
 7)...Outlet  7       : Off
 8)...Outlet  8       : Off
 9)...Outlet  9       : Off
10)...Outlet 10       : Off
11)...Outlet 11       : On
12)...Outlet 12       : Off

Type "Help" for a list of commands

RPC-22>
expect: does "Connected\r\n\r\n\nRPC-22 Series\r\n(C) 2000 by BayTech\r\nF1.10\r\n\nOption(s) Installed:\r\nTrue RMS Current\r\n\r\nRPC-22>off 12\r\r\nRPC-22>on 11\r\r\nRPC-22>status\r\r\nTrue RMS Current:   0.4 Amps\r\nMaximum Detected:   0.7 Amps\r\n\r\n\r\n 1)...Cable Modem     :expect1.6>
expect1.6> send "off 11\r\n"
send: sending "off 11\r\n" to { exp5 }
write() failed to write anything - will sleep(1) and retry...
expect1.7> write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
write() failed to write anything - will sleep(1) and retry...
^Cwrite() failed to write anything - will sleep(1) and retry...
sighandler: handling signal(2)
async event handler: Tcl_Eval(exit 130)
#
  #2 (permalink)  
Old 01-10-2009
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Well, the only problem I see is stuff like:
send "off 12\r\n"

That's not what type when you use cu manually, is it? You almost certainly type "off 12" and then hit the enter or carriage return key. So try just doing exactly what you type:
send "off 12\r"

I'm not sure that the extra \n characters are really the problem, but I'd give it a try.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:03 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0