|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
|
Thread Tools | Display Modes |
|
|||
Controlling a RS-232 Serial Console from a Shell Script
Sorry Admins, I should have posted this in OpenBSD General or Programming.
I'm using OpenBSD 4.4. 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 from programs, 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. Works fine 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> Does not work with expect controlling cu. 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) # Last edited by ishikawanakano; 9th January 2009 at 10:35 PM. |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
shell script compare md5 sum | bsdnewbie999 | Programming | 1 | 11th April 2009 02:20 PM |
Setting up the serial console | PeterSteele | FreeBSD General | 6 | 3rd November 2008 04:42 PM |
Shell Script. | bsdnewbie999 | Programming | 21 | 15th July 2008 07:54 AM |
shell script with input | c0mrade | Programming | 5 | 13th July 2008 04:33 AM |
getty dies on serial console? | michaelk | FreeBSD General | 9 | 18th May 2008 08:44 AM |