Sunday, June 24, 2012

Controling the Kenwood TH-D7 from a PHP script

I was asked to write a blog post about the TH-D7 PHP script I wrote and posted about on twitter and youtube. One thing, all of my scripts were and are written for the command line, thou I see no reason why you couldn't use this script from a website, maybe with some slight modifications. You'll need to get the php_serial.class.php written by Rem Sanchez It can be found here: http://www.phpclasses.org/browse/file/17926.html This php script is the key to making the whole thing work, well at least it's one of the keys. The other the D7 and other Kenwood radios can be controlled from the serial port on the radio, you just need to know what to send to the port for the radio to do what needs to be done. For the command protocol you should look here: http://www.radiohound.com/command.htm So the basics: 1 include the php_serial.class.php 2 open the com port 3 send the command to the radio 4 Jump up and down because it worked! LOL It really is as simple as that thou. So, let look at my code, which is still very much a work in progress, sooner or later I will add/change the code so that it will ask the Freq or ask the message, at this time, it's hard written in the code, but it will give you an idea. CODE: D7.PHP require("php_serial.class.php"); $update = toserial($msg, "/dev/ttyUSB0"); function toserial($text, $comport) { //Initialize the class $serial = new phpSerial(); //Specify the serial port to use... in this case COM1 $serial->deviceSet($comport); //Set the serial port parameters. The documentation says 9600 8-N-1, so $serial->confBaudRate(9600); //Baud rate: 9600 $serial->confParity("none"); //Parity (this is the "N" in "8-N-1") $serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1") $serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1") $serial->confFlowControl("none");//Device does not support flow control of any kind, $serial->deviceOpen(); $serial->sendMessage($text); $serial->deviceClose(); return; } The above is will be called from the other scripts, it's meant to try and keep all the scripts small and easy to use, mainly what this script does is setup and send the command to the serial port. My FREQCHANGE.PHP $freq = '00144390000'; $msg = "FQ " . $freq . ",00" .chr(13) .chr(10); include ('d7.php'); OK, so here I set what Frequency I want to change to, you might notice how the frequency has leading zeros and trailing zeros, this is required by the radio, otherwise it's not going to work correctly. The $msg is what is passed to the D7.PHP script and is the command for the radio so if we look at the command protocol we will see that FQ is for setting the frequence and is in the form of FQ(space)FREQ,00 and a return and line feed need to both be sent I have two scripts setup for turning on and off the TNC of the radio as well, they are pretty much the same script: $msg = "TNC 1" .chr(13) .chr(10); //will turn TNC on $msg = "TNC 0" . chr(13) .chr(10); //will turn TNC off But again I refereed to the command protocol for those commands :-) of course the fun command is to send an APRS message! For that I wrote a short PHP script called: APRSMSGSEND.PHP $tocall = 'CALLSIGN-SSID'; $tomsg = 'Testing APRS Msg via script'; $msg = "AMSG 00," .$tocall . "," . $tomsg . chr(13) . chr(10); include ('d7.php'); SO as you can probably see by now, it's just setting the correct command to send to the serial port of the D7 $tocall call be a callsign or a callsign with SSID, and just remember the D7 is limited in how many characters it can send for the message SO, really the key to make this work is the serial port script! The D7 has a lot of other things that can be controlled via the serial port so I will refer you to the command protocol. My scripts are still very much a work in progress. Sooner or later I would like to make it look for key words off twitter probably just to change the freq, and I'll hook the audio up to a streaming service so people can listen in! :-) Other radios probably can also be controlled by this method as well, you just need to know the command protocol for your radio! Happy Radioing, and 73, LeRoy, KD8BXP

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hey Mr. Miller, I'd like to ask you a question regarding use of your PHP code and licensing/copyright information. Please email me: jsell08 (AT) gmail.com since I haven't been able to successfully locate your contact information.
    Thanks for the example!
    -Jason

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Hi, is this project still active? I am interested in developing a system like for a CubeSat project and would be interested in hearing from you.

    ReplyDelete