12.4  PHP code files - initialising the RPi serial or USB port

Client javascipt code sends a php file name request to the server to do this specific task.

The file is wcajaxserialinit.php. Parameters are added after the filename.

An Ajax command is used to generate the call. In this call, there is no data to return to the client.

AJAX is demonstrated elsewhere using a more complex situation that returns data to the client



wcajaxserialinit,php uses PhpSerial.php which has class: $serial containing the functions
used to initialise and access the RPi serial or USB port connected to the radio.

These functions must be accessed as $serial->deviceSet (..)  etc etc.
This is because the method (subroutine) deviceSet(..) is defined within the class $serial.


  ini_set("output_buffering","On");  error_reporting(E_ALL);

  ini_set('display_errors', '1'); ob_start();  session_start();


  require_once 'PhpSerial.php';   require_once 'wchamlib.php';


  $baudrate = $_POST['baudrate'];

  $stopbits = $_POST['stopbits'];

  $charbits = $_POST['charbits'];

  $parity = $_POST['parity'];

  $connection = $_POST['connection'];

  $catcomms = $_POST['catcomms'];

  $rigfix = $_POST['rigfix'];

  $hamlib = $_POST['hamlib'];

  $civaddr = $_POST['civaddr'];

  $vfomode = $_POST['vfomode'];


  if($connection == 'USB'){$tty = "/dev/ttyUSB0";} else {$tty = "/dev/ttyAMA0";};


  $_SESSION['tty'] = $tty;                        $_SESSION['connection'] = $connection;

  $_SESSION['catcomms'] = $catcomms;        $_SESSION['rigfix'] = $rigfix;

  $_SESSION['hamlib'] = $hamlib;                $_SESSION['civaddr'] = $civaddr;

  $_SESSION['vfomode'] = $vfomode;        


  if($catcomms == 'HAMLIB')    // For Hamlib we communicate with the API, 

  {                                     // not directly with the rig.

    $ret = $Hamlib->startRigctld($hamlib, $tty, $baudrate,
                                         $stopbits, $charbits, $civaddr, $vfomode);




    $serial->deviceSet($tty);                $serial->confBaudRate($baudrate);

    $serial->confParity($parity);        $serial->confCharacterLength($charbits);

    $serial->confStopBits($stopbits);        $serial->confFlowControl("none");

    $serial->deviceSetParams();                $ret = "OK";


  echo $ret;        


Note the separate startup call for Hamlib which is in file wchamlib.php.
We start rigctld which is the Hamlib API. $hamib is the rig number, $civaddr is only for Icom.

piWebCAT communicates with the rigctld socket (localhost:4532). rigctld communicates with the rig.  

The ten values are picked up from the calling parameter string using $_POST['baudrate'] etc

$serial->deviceSet(..) tells the PhpSerial.php code to use RPi serial port: /dev/ttyAMA0

or the USB connection: /dev/ttyUSB0.
(Note that an access permission rule must be set for /dev/ttyUSB0 -   See; 14.4 SD card config)

The four serial settings are then applied, ie: baudrate, parity, bits/char  and stopbits.

Although this PHP process and its data die when the job is done, the serial port in the RPi

will retain its settings for ttyAMA0 or ttyUSB0.
So when we subsequently use the port, we only need to call deviceSet(/dev/tty....)
 to use the device. (It does not need reconfiguring)

The $_SESSION commands set up  session variables which are retained by the webserver
and CAN be accessed by subsequent PHP processes:

$connection is  SERIAL or USB (or ENCAT - via my EncoderCat unit... not with Hamlib)

$catcomms is
          - for piWebCAT direct control:        ASCII (text based Yaesu, Kenwood),

YAESU5 (older 5 byte Yaesu)
            CIV (for Icom)

- or HAMLIB (using Hamlib rig database and API)