As the original Missing Socks page has disappeared into the intergallactic bit bucket at the same time the British MoD closed down its UFO investigations unit, we now link to a new page on the missing socks mystery
This is a page devoted to socket communication between a mac and a Soft-I/O module to let our beautiful Mac computers interface to the real world.
Let's face it: although the device comes with a web interface, sometimes one wants to KEEP that beautiful data and, say, graph it with JPGraph. Having no other programming environment at hand, a home server running MAMP appeared to have all the neccessary stuff available. All that was required, was putting it together. The code is a compilation of begged for, borrowed and stolen, but writing it all up in this page helped me to get things going.
We have the hardware, the current page is about the software. The results are in the graph pages
Sometimes, you just want to make sure that socket communication is working and that it is not your computer that is at fault. To that end, here are two pieces of code that can talk to one another. It is not elegant, not foolproof but it works (for me at least ;-).
This goes in the file 'server1234.php' in the htdocs folder:
<?
//set some variables
$hostname="localhost";
$port=1234;
$host=gethostbyname($hostname);//obtain the IP address of the socket server
echo "Host:$host:$port\n";
//don't time out!
set_time_limit(0);
//create socket
$socket=socket_create(AF_INET,SOCK_STREAM,0) or die("Could not create socket\n");
echo"created socket\n";
//bind socket to port
$result=socket_bind($socket,$host,$port) or die("Could not bind to socket\n");
echo"bound to socket\n";
//start listening for connections
$result=socket_listen($socket,3) or die("Could not set up socket listener\n");
echo"listening to incoming sockets\n";
//accept incoming connections
//spawn another socket to handle communication
$spawn=socket_accept($socket) or die ("Could not accept incoming connection\n");
echo"spawned socket\n";
//readclientinput
$input=socket_read($spawn,1024)ordie("Couldnotreadinput\n");
echo"spawnreceived$input\n";
//clean up input string
$input=trim($input);
//reverse client input and sendback
$output=strrev($input)."\n";
socket_write($spawn,$output,strlen($output)) or die("Could not write output\n");
//closesockets
socket_close($spawn);
socket_close($socket);
?>
This goes in the file' do1234.php' in the htdocs folder:
<?
//socket client for commnunication with socket server on port 1234
//this simulated soft-i/o module has one virtual device,an analog input,
//whose tag is ANIN1(default)
$commands="ANIN1.Value&";//append requesting its value and conclude with the mandatory '&' sign
$command=$commands;//this is the place to insert loops if you have multiple virtual devices
$hostname="localhost";
$port=1234;
$host=gethostbyname($hostname);//obtain the IP address of the socket server
echo"Host:$host:$port\n";//show feedback to see if correct IP has been used
$mysocket=socket_create(AF_INET,SOCK_STREAM,0); //open a client connection
if(!$mysocket){die("could not create socket");}
; //if you can't create a socket,something is wrong
//and you need to connect to a created socket before you can use it.
if(!socket_connect($mysocket,$host,$port)){die("could not connect socket");}
$message=$command;
$nchars=strlen($message);
echo"sending'".$message."'as string\0";//feedback
$nsentchar=socket_send($mysocket,$message,$nchars,0);//send the actual command
if(!($nsentchar==$nchars)){//could not send all characters;feedback how many did go out
echo"could not send string".$message."sent only".$nsentchar." of ".$nchars." characters";
}else{//successfully sent the command, now see if you can receive responses
echo"sent '".$message."' as string of ".$nsentchar." characters\n";
$ncharerreceived=socket_recv($mysocket,$string,256,0);//assume max 256 chars
if(!$ncharerreceived){
echo"nothing received\n";
}else{
echo"received ".$ncharerreceived." characters;the string was '".$string."'\n";
}
}
socket_close($mysocket);
?>
Save the file in the htdocs folder of MAMP, fire up MAMP (heck, you might not even need MAMP fired up?), open two Terminal windows.
In Terminal window 1 ([ENTER]means press the Enter key) type:
/Applications/MAMP/bin/php5/bin/php -q /Applications/MAMP/htdocs/server1234.php[ENTER]
In Terminal window 2 type:
/Applications/MAMP/bin/php5/bin/php -q /Applications/MAMP/htdocs/do1234.php[ENTER]
See how sockets communicate. You should get this output:
Host: 127.0.0.1:1234
sending 'ANIN1.Value&' as stringsent 'ANIN1.Value&' as string of 12 characters
received 13 characters; the string was '&eulaV.1NINA
'
If you do not see anything, open the file /Applications/MAMP/logs/php_error.log to see what went wrong.
Soft-I/O modules can be read out using socket communication. A stand-alone application can be used to establish this communication or php can be used. Having php and a web server at hand, I decided to get an example php script that reads 1000 values from an analog input. I'm using firmware version 1.0.9 and MAMP on Mac OS 10.4 (having LAMP, WAMP etc. as alternatives on Linux and Windos).
Open a browser to your soft-io module. Configure the Soft-I/O module to have an analog input and give it 'ANIN4' as tag (soft-devices, set up an analog input, add soft device, Versatile Voltage Input, 0-10V, connect the pins (I set up an analog 10 mV/K temp sensor on pin 2, connected its power supply on pin 3 via a resistor of 2K2 to pin2 and gnd on pin here), click 'show additional configuration options', enter ANIN4 in the 'Tag' field, submit.
In the 'setup/Protocol' browser window of the Soft/IO module, set port 23 as telnet port, leave port 8001 as program port and save.
Next, check the availability of devices using the 'querydevs' command in Terminal. Open the Terminal application from the /Applications/Utilities, set up a telnet session to your softdevice: In the Terminal window, enter
telnet softio-xxxx.local.
whereby you substitute your Soft-I/O device number for the xxxx. Press [ENTER] a few times until you see a response. If the module does not reply as expected, repeat yourself (see example below). Type help[ENTER] in the Terminal window to see the available commands:
$telnet xxxxx
Trying xxxxx...
Connected to x.
Escape character is '^]'.
Soft-I/O Command Interface
Notes:
1. Backspace and delete are not supported at this time
2. You might need to turn on Local Echo if using telnet window
3. Type help for Command Line interface supported commands
softio# help
ERROR: Invalid Command (help
)
softio# help^M
ERROR: Invalid Command (help)
softio# help^M
Notes
-----
1. Backspace and delete are not supported at this time
2. You might need to turn on Local Echo if using telnet window
Commands
--------
exit : Close connection
stat : Query connection status
querydevs : Query Soft-Devices
Return Values
-------------
ERROR: Invalid Command : Command name not recognized
ERROR: Invalid Device : Device tag not recognized
softio#
Now check the Tag name of your device by typing querydevs[ENTER] in the Terminal window. If all is well, the module responds with the following communication:
softio# querydevs^M
Type Tag Name
gnd GND1 ground
seq __BPCSTARTUP Power Up Sequence
vltg VLTG1 +5V
anin ANIN4 LM335Z
seq __BPCERROR Error Sequence
The name of the TAG is 'ANIN4'.
Now open a php editor (PageSpinner comes to mind) and type the following code; you can omit any comments inidicated by // or copy the code below:
<?
//socket client for commnunication with socket server on port 8001
//this soft-i/o module has one virtual device, an analog input, whose tag is ANIN1 (default)
$repeats=1000;
$commands="ANIN4.value&";//append requesting its value and conclude with the mandatory '&' sign
$command=$commands;//this is the place to insert loops if you have multiple virtual devices
$hostname="softio-4467.local.";
$port = 8001;//command port
$host=gethostbyname($hostname);//obtain the IP address of the socket server
echo "Host: $host:$port\n"; //show feedback to see if correct IP has been used
$mysocket=socket_create(AF_INET,SOCK_STREAM,0); // open a client connection
if(!$mysocket){die("could not create socket");}
//if you can't create a socket, something is wrong
//and you need to connect to a created socket before you can use it.
if (!socket_connect($mysocket,$host,$port)){die("could not connect socket");}
$message=$command;
$nchars=strlen($message);
echo "sending '".$message. "' as string\0";//feedback
while($repeats-->0){
$nsentchar=socket_send($mysocket, $message, $nchars,0);//send the actual command
if(!($nsentchar==$nchars)){//could not send all characters; feedback how many did go out
echo "could not send string ".$message. " sent only ".$nsentchar." of ".$nchars. " characters";
}else{
//successfully sent the command, now see if you can receive responses
//echo "sent '".$message. "' as string of ".$nsentchar." characters\n";
$ncharerreceived=socket_recv($mysocket, $string,256,0);//assume max 256 chars
if (!$ncharerreceived){
echo "nothing received\n";
}else{
echo $string." \r";
}
}
}
//insert end of loop if you have multiple virtual devices
socket_close($mysocket);
?>
Save this code in /Applications/MAMP/htdocs/ as 'dorepeated8001.php'.
In the MAMP folder, open the folder htdocs and the folder bin/php5/bin/. The latter holds the php engine. Start MAMP by double-clicking the MAMP application. Start the servers, if they don't do so by themselves already.
As soon as Mamp indicates that the server is running, go back to the Terminal application and open a new window (the old one is still running the telnet session). You will now call the socket command script dorepeated8001.php in the telnet window. Easiest this is done by dragging and dropping the php engine into the Terminal window, typing '-q' and then dragging and dropping your dorepeated8001.php file into the Terminal folder. The result is a single, long, line:
/Applications/MAMP/bin/php5/bin/php -q /Applications/MAMP/htdocs/dorepeated8001.php
If all is well, you should be seeing a fast moving number in your terminal window. This is the full-speed readout of the Soft-I/O module. On a Powerbook G4/1GHz, this takes about 18 seconds for reading out 1000 values. Now you have proven socket communication to work with php and soft-I/O.