Assigning output from LINEOUT to a variable
Hi,
I'm using kermit to control a quasar electronics 3108 serial module, at
present I am calling the kermit script i have written from a bash shell
script and it works ok, but it is inefficient.
I'm hoping to write the whole thing inside a kermit script, however I'm
stuck....
I can read in a file and then assign the value contained in that file
to a variable that works a treat! See below:
OPEN READ /u01/status/OUT58
READ \%a
close read-file
if ( == \%a 0 ) {
LINEOUT F1
INPUT 5 \"#\"
} else {
LINEOUT N1
INPUT 5 \"#\"
}
As I say that works well, however I need to be able to do (say)
LINEOUT I1
which will give me the status of input line 1 and then take the output
of this and if it is 1 then do something, if it is 0 then do something
else.
So I need something like:
LINEOUT I1 > \%a
INPUT 5 \"#\"
if ( == \%a 0 ) {
echo 1 > IN11
} else {
echo 0 > IN11
}
....but I've just made up the syntax of the fist line "LINEOUT I1 >
\%a" I need to know how to inject the output of this command to a
variable.
I hope that makes sense.
Thanks
-Dan
Re: Assigning output from LINEOUT to a variable
On 2006-06-20, [email]toastyboy@googlemail.com[/email] <toastyboy@googlemail.com> wrote:
From: Frank da Cruz <fdc@sesame.cc.columbia.edu>
To: [email]toastyboy@googlemail.com[/email]
Subject: Re: Assigning output from LINEOUT to a variable
In-Reply-To: <1150811644.629253.47720@r2g2000cwb.googlegroups.com>
: I'm using kermit to control a quasar electronics 3108 serial module, at
: present I am calling the kermit script i have written from a bash shell
: script and it works ok, but it is inefficient.
:
: I'm hoping to write the whole thing inside a kermit script, however I'm
: stuck....
:
: I can read in a file and then assign the value contained in that file
: to a variable that works a treat! See below:
:
: OPEN READ /u01/status/OUT58
: READ \%a
: close read-file
:
: if ( == \%a 0 ) {
: LINEOUT F1
: INPUT 5 \"#\"
: } else {
: LINEOUT N1
: INPUT 5 \"#\"
: }
:
: As I say that works well, however I need to be able to do (say)
:
: LINEOUT I1
:
: which will give me the status of input line 1 and then take the output
: of this and if it is 1 then do something, if it is 0 then do something
: else.
:
: So I need something like:
:
: LINEOUT I1 > \%a
: INPUT 5 \"#\"
: if ( == \%a 0 ) {
: echo 1 > IN11
: } else {
: echo 0 > IN11
: }
:
: ...but I've just made up the syntax of the fist line "LINEOUT I1 >
: \%a" I need to know how to inject the output of this command to a
: variable.
:
First let's assume you're using the current version of C-Kermit, 8.0:
[url]http://www.columbia.edu/kermit/ckermit.html[/url]
If not, you should pick it up. Then see:
[url]http://www.columbia.edu/kermit/ckscripts.html[/url]
about how to construct a Kermit script that you can run directly from
the shell, as if it were a shell script. Remember to put in IF FAIL or
IF SUCCESS tests after critical commands after which you don't want the
script to keep going if they fail:
#!/usr/local/bin/kermit
.file := /u01/status/OUT58
open read \m(file)
if fail exit 1 "?\m(file): Open failed"
read \%a
if fail exit 1 "?\m(file): Read failed"
close read-file
if == \%a 0 lineout F1
else lineout N1
input 5 \"#\"
if fail exit 1 "?INPUT timed out"
Now the next part I'm not sure about. You want to send "I1" and then read
back the response into a variable. I can show you how to do that but first
you have to tell me how to parse the response. Plus I'm not sure what
you're trying to accomplish the with the ECHO commands.
Usually the pattern is, you do an OUTPUT to make the thing on the other
end send some results, and then you do an INPUT to read the results. Since
you don't know what the results will be, the thing your INPUT command is
looking for is a line terminator, or the next prompt, etc. Then if the
INPUT command succeeds, the \v(input) variable contains whatever was read
by the INPUT command (and, in fact, previous ones too, unless you gave a
CLEAR INPUT command before issuing the INPUT command)
Then you would use the Kermit's string processing functions such as
\findex(), \fsubstring(), etc, to extract the desired item from the the
\v(input) variable.
- Frank
Re: Assigning output from LINEOUT to a variable
Frank,
Thanks for your response.
Regarding parsing the output, here is what I would simply expect to see
either a 1 or a 0 (depending on the state of the input) followed by a #
on a new line.
(The # is the command prompt on this device if you like)
See the below extract from an interactive session:
#
#I1
1
#I2
1
#I3
1
#I4
0
#
What I want is either to set the 0 or 1 as a variable and then based on
this write out a file with the 0 or 1 in it, or (better still) write
the 0 or 1 out to a file directly.
Thanks for the comprehensive response by the way, it is much
appreciated.
As a bit of background, what I am actually doing is effectively making
a file based interface to this serial module, it has 8 outputs and 4
inputs which can be either on (1) or off (0) I want to be able to
control it via 12 files OUT11 - 18 and IN11-14 who's content is either
a 1 or 0 to represent the desired output of the out lines, or the
actual status of the in lines.
I'm actually using it already to do some really cool home automation
stuff (I have 5 of these devices) and other assorted stuff, but the
current setup where I call the kermit script via a bash script (and
have to open and configure the serial connection each time) is really
slow and cpu intensive, so the plan is to have this new deamon
constantly looping and reading the status of the module and then I have
lots of shell scripts that check these files and make decisions based
on them... but I guess this is a bit off topic so I'll stop now!! ;-)
Thanks again
-Dan Mc
Re: Assigning output from LINEOUT to a variable
On 2006-06-20, [email]toastyboy@googlemail.com[/email] <toastyboy@googlemail.com> wrote:
: Regarding parsing the output, here is what I would simply expect to see
: either a 1 or a 0 (depending on the state of the input) followed by a #
: on a new line.
:
: (The # is the command prompt on this device if you like)
:
: See the below extract from an interactive session:
:
: #
: #I1
: 1
: #I2
: 1
: #I3
: 1
: #I4
: 0
: #
:
: What I want is either to set the 0 or 1 as a variable and then based on
: this write out a file with the 0 or 1 in it, or (better still) write
: the 0 or 1 out to a file directly.
:
You want to send a command to this device, get back a single-character
response, and write the response to a file. This is seemingly
straightforward, but there's a bit more to it, because maybe the device echoes
what you send, and since the single-character responses really come with some
other stuff, such as carriage returns and linefeeds. What do you want to go
into your file -- a single digit, or the digit followed by carriage return
and/or linfeed?
Let's say you want to store <digit><CR><LF>. Then the above sequence
would be programmed something like this:
clear input ; start with a clean INPUT buffer
output \13 ; send a carriage return (\13) to make a prompt appear
input 5 \# ; wait 5 sec for the '#' prompt
if fail stop 1 ; make sure we got it
output I1\13 ; Send "I1" and a carriage return
; Let's assume the devices echoes "I1<CR><LF>"
input 5 I1\13\10 ; Absorb the echo
if fail ... ; Do something if the echo does not appear
clear input ; clear the input buffer again
input 5 \10 ; Input everything up to a linefeed
if fail ...
; At this point \v(input) should contain the resonse.
; Now you can write it to a file.
fopen /write \%c somefilename
if fail ...
fwrite /line \%c \ftrim(\v(input))
fclose \%c
and so on for I2, I3, and I4. I didn't show what to do on failure,
that's up to you. Maybe there is some corrective action you can take.
You can loop and try again, whatever. The statement:
fwrite /line \%c \ftrim(\v(input))
trims the CRLF from the response, and then writes it to disk in native
format for a line, which is not necesarily text terminated by CRLF.
- Frank
Re: Assigning output from LINEOUT to a variable
Frank!!
That's great! Starting to make sense now, I've used LINEOUT rather
than send the carriage return at the end, but thanks for explaining
I've been stuck on this for weeks - even ordered the kermit manual from
amazon.com! :-) ...should be useful anyway I guess.
Bad news is that I still have my original problem (which I probably
didn't explain to start with) with my *all new* script the performance
is awesome so that's good, my garage lights come on immediately that I
open the door and the front door chimes much earlier than it used to,
from moving from a shell script calling kermit, to a kermit script I
reckon has increased performance by 500% easily, so that's great.
However, what i had always had problems with was that over time my
server would run out of memory and grind to a halt, even with my new
script I'm getting the same problem.
Is it possible that my kermit script needs to flush buffers or
inputs/outputs each time it cycles?
The script is included below (it's quite long)
#!/u01/bin/kermit +
set line /dev/ttyS5
set speed 9600
set stop-bits 1
set parity none
set flow-control none
SET CARRIER-WATCH OFF
open line /dev/ttyS5
while true {
OPEN READ /u01/status/OUT11
READ \%a
close read-file
OPEN READ /u01/status/OUT12
READ \%b
close read-file
OPEN READ /u01/status/OUT13
READ \%c
close read-file
OPEN READ /u01/status/OUT14
READ \%d
close read-file
OPEN READ /u01/status/OUT15
READ \%e
close read-file
OPEN READ /u01/status/OUT16
READ \%f
close read-file
OPEN READ /u01/status/OUT17
READ \%g
close read-file
OPEN READ /u01/status/OUT18
READ \%h
close read-file
if ( == \%a 0 ) {
LINEOUT F1
INPUT 1 F1
INPUT 1 \"#\"
} else {
LINEOUT N1
INPUT 1 N1
INPUT 1 \"#\"
}
if ( == \%b 0 ) {
LINEOUT F2
INPUT 1 F2
INPUT 1 \"#\"
} else {
LINEOUT N2
INPUT 1 N2
INPUT 1 \"#\"
}
if ( == \%c 0 ) {
LINEOUT F3
INPUT 1 F3
INPUT 1 \"#\"
} else {
LINEOUT N3
INPUT 1 N3
INPUT 1 \"#\"
}
if ( == \%d 0 ) {
LINEOUT F4
INPUT 1 F4
INPUT 1 \"#\"
} else {
LINEOUT N4
INPUT 1 N4
INPUT 1 \"#\"
}
if ( == \%e 0 ) {
LINEOUT F5
INPUT 1 F5
INPUT 1 \"#\"
} else {
LINEOUT N5
INPUT 1 N5
INPUT 1 \"#\"
}
if ( == \%f 0 ) {
LINEOUT F6
INPUT 1 F6
INPUT 1 \"#\"
} else {
LINEOUT N6
INPUT 1 N6
INPUT 1 \"#\"
}
if ( == \%g 0 ) {
LINEOUT F7
INPUT 1 F7
INPUT 1 \"#\"
} else {
LINEOUT N7
INPUT 1 N7
INPUT 1 \"#\"
}
if ( == \%h 0 ) {
LINEOUT F8
INPUT 1 F8
INPUT 1 \"#\"
} else {
LINEOUT N8
INPUT 1 N8
INPUT 1 \"#\"
}
LINEOUT I1
INPUT 1 I1
MINPUT 1 1 0
..\%n := \v(minput)
if ( == \%n 1 ) {
RUN (echo 1 > /u01/status/IN11)
} else {
RUN (echo 0 > /u01/status/IN11)
}
INPUT 1 \"#\"
LINEOUT I2
INPUT 1 I2
MINPUT 1 1 0
..\%o := \v(minput)
if ( == \%o 1 ) {
RUN (echo 1 > /u01/status/IN12)
} else {
RUN (echo 0 > /u01/status/IN12)
}
INPUT 1 \"#\"
LINEOUT I3
INPUT 1 I3
MINPUT 1 1 0
..\%p := \v(minput)
if ( == \%p 1 ) {
RUN (echo 1 > /u01/status/IN13)
} else {
RUN (echo 0 > /u01/status/IN13)
}
INPUT 1 \"#\"
LINEOUT I4
INPUT 1 I4
MINPUT 1 1 0
..\%q := \v(minput)
if ( == \%q 1 ) {
RUN (echo 1 > /u01/status/IN14)
} else {
RUN (echo 0 > /u01/status/IN14)
}
INPUT 1 \"#\"
clear input
}
quit
Now that my script is running more often i'm running out of memory even
quicker than before, so I'm fairly sure that it's kermit causing my
problems (although I'm sure it's my poor scripting and not kermit's
fault)
Thanks to all again
-Dan McGrath
Re: Assigning output from LINEOUT to a variable
Oh,
forgot to say last night (was in a hurry) that I will of course tidy up
the script and put some error handling in (honest!!)
and Frank, I just checked my amazon order and realised you are the
author of the Kermit book I've bought!!
Interestingly also, I've been monitoring memory usage overnight and the
usage reported by free (whilst still going up) is increasing much
slower than it was, so I guess it's possible that it will plateau out
soon (currenty using 85Meg, and I have 512Meg)
Cheers
-Dan McGrath
Re: Assigning output from LINEOUT to a variable
On 2006-06-21, [email]toastyboy@googlemail.com[/email] <toastyboy@googlemail.com> wrote:
: That's great! Starting to make sense now, I've used LINEOUT rather
: than send the carriage return at the end, but thanks for explaining
: I've been stuck on this for weeks - even ordered the kermit manual from
: amazon.com! :-) ...should be useful anyway I guess.
:
"LINEOUT foo" is usually but not always a synomym for "OUTPUT foo\13".
The "not always" applies to network connections, but for serial connections
they are the same. However sometimes it's better to explicit about exactly
what characters you are sending and looking for.
: Bad news is that I still have my original problem (which I probably
: didn't explain to start with) with my *all new* script the performance
: is awesome so that's good, my garage lights come on immediately that I
: open the door and the front door chimes much earlier than it used to,
: from moving from a shell script calling kermit, to a kermit script I
: reckon has increased performance by 500% easily, so that's great.
:
: However, what i had always had problems with was that over time my
: server would run out of memory and grind to a halt, even with my new
: script I'm getting the same problem.
:
: Is it possible that my kermit script needs to flush buffers or
: inputs/outputs each time it cycles?
:
You didn't say what version of C-Kermit you have, but of course over the
years we have fixed problems when they were encountered, including some
memory leaks. Suggest you try the latest development code:
[url]http://www.columbia.edu/kermit/ckdaily.html[/url]
- Frank
Re: Assigning output from LINEOUT to a variable
Hi,
I'm running:
[root@relics root]# /u01/bin/kermit --v
8.0.211 [8.0.211]
I'll try the latest development code tonight.
Thanks again.
-Dan