Page 1 of 1
Bash communication over tcp/upd
Posted: Sun Dec 25, 2011 11:51 pm
by Hathor27
Hi there,
these days I try to communicate with a tcp device: I need to write a command to a specific port and then read its answer from the same port to get its data - how can I perform that?
I read about the command line redirector
Code: Select all
echo ThisCommand > /dev/tcp/ServerIP/ServerPort
but this kind of redirector won't work on my ALIX - any idea why, or how I could get it run?
Or are there any other standard tcp comminication commands to be used in bash scripts?
Thanks a lot in advance
Re: Bash communication over tcp/upd
Posted: Mon Dec 26, 2011 5:49 pm
by wfpost
netcat
http://de.wikipedia.org/wiki/Netcat
http://linux.die.net/man/1/nc
e.g. this reads and writes the actual meteohub sensordata into a local file
nc localhost 5558 > sensordata.txt
Re: Bash communication over tcp/upd
Posted: Mon Dec 26, 2011 10:52 pm
by Hathor27
wfpost wrote:nc localhost 5558 > sensordata.txt
Thank you wfpost, but how do I have to proceed if I first have to write a command like
getadc 1 to my device?
Like
Code: Select all
Put MyDevice 50290 "getadc 1"
nc MyDevice 50290 > value.txt
Re: Bash communication over tcp/upd
Posted: Mon Dec 26, 2011 11:22 pm
by wfpost
^^^^ Don´t know - maybe like that
What values are you trying to get? From a solar-panel?
Code: Select all
echo getadc 1|nc xxx.xxx.xxx.xxx 50290 -w 1
Re: Bash communication over tcp/upd
Posted: Mon Dec 26, 2011 11:48 pm
by Hathor27
wfpost wrote:^^^^ Don´t know - maybe like that
What values are you trying to get? From a solar-panel?
Code: Select all
echo getadc 1|nc xxx.xxx.xxx.xxx 50290 -w 1
Wow! That works in one go - YMMD
I got an AVR NET-IO to read analog values over tcp:
http://www.mikrocontroller.net/articles ... von_Pollin (sorry for it's only in German). With this I want to read and log all relevant temperatures of my heating system.
Thanks a lot so far
Re: Bash communication over tcp/upd
Posted: Tue Dec 27, 2011 12:23 am
by Hathor27
It's me again: As I found now, this works fine from command line, but doesn't work as plug-in...
Code: Select all
#! /bin/sh
#
# runs as /home/meteohub/plugin_raw-net-io.sh
#
while :
do
# reads raw data of net-io.php
echo getadc 1 | nc MyDevice 50290 -w 1| gawk 'BEGIN {FS=" "} {printf "t1 %d\n", $1 }'
echo getadc 2 | nc MyDevice 50290 -w 1| gawk 'BEGIN {FS=" "} {printf "t2 %d\n", $1 }'
# make sure output gets flushed
sync
# wait 1 minutes for next read
sleep 60
done
For I get
CRLF from my device, I enriched the command line with
sed, removing
CR:
Code: Select all
echo getadc 1 | nc MyDevice 50290 -w 1 | sed s/'\r'//g | gawk 'BEGIN {FS=" "} {printf "t1 %d\n", $1 }'
At the command line I get a perfect output, like
but calling this script as plug-in, I get no sensor data... any idea why?
Re: Bash communication over tcp/upd
Posted: Tue Dec 27, 2011 11:13 am
by wfpost
what´s happens if you start the shell script manually from the commandline ???
Does this display an output?
sh shellscript
Re: Bash communication over tcp/upd
Posted: Tue Dec 27, 2011 11:32 am
by Hathor27
as I mentioned below
Hathor27 wrote:At the command line I get a perfect output, like
but calling this script as plug-in, I get no sensor data...
Re: Bash communication over tcp/upd
Posted: Tue Dec 27, 2011 11:58 am
by wfpost
hmm, --- dann bin ich mit meinem Latein am Ende

---
it´s strange, because that means that the commands are working also when called with the script.
I don´t know how exactly a plug-in runs the script ...
Maybe Boris has an idea why this is not working as plug-in??
Re: Bash communication over tcp/upd
Posted: Tue Dec 27, 2011 3:08 pm
by wfpost
could it be that stdout is missing for the plugin ???
--------
#! /bin/sh
#
# runs as /home/meteohub/plugin_raw-net-io.sh
#
while :
do
# reads raw data of net-io.php
echo getadc 1 | nc MyDevice 50290 -w 1 /dev/stdout 2>/dev/null | gawk 'BEGIN {FS=" "} {printf "t1 %d\n", $1 }'
echo getadc 2 | nc MyDevice 50290 -w 1 /dev/stdout 2>/dev/null | gawk 'BEGIN {FS=" "} {printf "t2 %d\n", $1 }'
# make sure output gets flushed
sync
# wait 1 minutes for next read
sleep 60
done
--------------
Re: Bash communication over tcp/upd
Posted: Tue Dec 27, 2011 4:03 pm
by Hathor27
wfpost wrote:could it be that stdout is missing for the plugin ???
Negative! - this doesn't change anything: The output at the command line is still OK, but it won't show any output as Plug-In
Re: Bash communication over tcp/upd
Posted: Tue Dec 27, 2011 4:14 pm
by Hathor27
I found a work-around with a PHP-Script, which runs on an other server (uah!!!) and fetches the values from my NET-IO:
Code: Select all
<?php
/* import raw data from NET-I/O and
print raw_data to stdout
*/
$commands = array("getadc 1", "getadc 2", "getadc 3", "getadc 4");
$raw_data = "";
$fp = fsockopen('MyDevice', 50290); // opens connection to MyDevice
foreach ($commands as $key => $command) // loop over all commands
{ fwrite($fp, $command); // request data
$reading = intval(fgets($fp, 4096)); // read from device
$raw_data = $raw_data."t$key $reading\n"; // forms rawdata: "t1 234\n"...
}
fclose($fp);
print $raw_data; // rawdata to stdout
?>
It does the task that the "
echo... | nc... | sed... | gawk..."-command should do.
If I request this PHP-Script from my Bash-Script, using WGET command, it also works as Plug-In - but it's not really a smart solution...