refactor socket send and receive to separate method to reduce code duplication

pull/2/head
toimtoimtoim 8 years ago
parent dea30f53b4
commit 1e1c8ee5ce
  1. 305
      src/ModbusMaster.php

@ -161,21 +161,17 @@ class ModbusMaster
public function readCoils($unitId, $reference, $quantity) public function readCoils($unitId, $reference, $quantity)
{ {
$this->status .= "readCoils: START\n"; $this->status .= "readCoils: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC 1 function () use ($unitId, $reference, $quantity) {
$packet = $this->readCoilsPacketBuilder($unitId, $reference, $quantity); return $this->readCoilsPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) use ($quantity) {
// receive response return $this->readCoilsParser($data, $quantity);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$receivedData = $this->readCoilsParser($rpacket, $quantity);
// disconnect
$this->disconnect();
$this->status .= "readCoils: DONE\n"; $this->status .= "readCoils: DONE\n";
// return
return $receivedData; return $receivedData;
} }
@ -337,7 +333,7 @@ class ModbusMaster
$readsocks[] = $this->sock; $readsocks[] = $this->sock;
$writesocks = null; $writesocks = null;
$exceptsocks = null; $exceptsocks = null;
$rec = ""; $rec = '';
$totalReadTimeout = $this->timeout_sec; $totalReadTimeout = $this->timeout_sec;
$lastAccess = microtime(true); $lastAccess = microtime(true);
$readTout = $this->secsToSecUsecArray($this->socket_read_timeout_sec); $readTout = $this->secsToSecUsecArray($this->socket_read_timeout_sec);
@ -451,11 +447,22 @@ class ModbusMaster
* *
* Disconnect the socket * Disconnect the socket
*/ */
private function disconnect() protected function disconnect()
{ {
if (is_resource($this->sock)) {
socket_close($this->sock); socket_close($this->sock);
$this->status .= "Disconnected\n"; $this->status .= "Disconnected\n";
} }
}
/**
* Close socket it still open
*/
public function __destruct()
{
$this->disconnect();
}
/** /**
* fc2 * fc2
@ -491,21 +498,17 @@ class ModbusMaster
public function readInputDiscretes($unitId, $reference, $quantity) public function readInputDiscretes($unitId, $reference, $quantity)
{ {
$this->status .= "readInputDiscretes: START\n"; $this->status .= "readInputDiscretes: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC 2 function () use ($unitId, $reference, $quantity) {
$packet = $this->readInputDiscretesPacketBuilder($unitId, $reference, $quantity); return $this->readInputDiscretesPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) use ($quantity) {
// receive response return $this->readInputDiscretesParser($data, $quantity);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$receivedData = $this->readInputDiscretesParser($rpacket, $quantity);
// disconnect
$this->disconnect();
$this->status .= "readInputDiscretes: DONE\n"; $this->status .= "readInputDiscretes: DONE\n";
// return
return $receivedData; return $receivedData;
} }
@ -591,21 +594,17 @@ class ModbusMaster
public function readMultipleRegisters($unitId, $reference, $quantity) public function readMultipleRegisters($unitId, $reference, $quantity)
{ {
$this->status .= "readMultipleRegisters: START\n"; $this->status .= "readMultipleRegisters: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC 3 function () use ($unitId, $reference, $quantity) {
$packet = $this->readMultipleRegistersPacketBuilder($unitId, $reference, $quantity); return $this->readMultipleRegistersPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) {
// receive response return $this->readMultipleRegistersParser($data);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$receivedData = $this->readMultipleRegistersParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "readMultipleRegisters: DONE\n"; $this->status .= "readMultipleRegisters: DONE\n";
// return
return $receivedData; return $receivedData;
} }
@ -696,21 +695,17 @@ class ModbusMaster
public function readMultipleInputRegisters($unitId, $reference, $quantity) public function readMultipleInputRegisters($unitId, $reference, $quantity)
{ {
$this->status .= "readMultipleInputRegisters: START\n"; $this->status .= "readMultipleInputRegisters: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC 4 function () use ($unitId, $reference, $quantity) {
$packet = $this->readMultipleInputRegistersPacketBuilder($unitId, $reference, $quantity); return $this->readMultipleInputRegistersPacketBuilder($unitId, $reference, $quantity);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) {
// receive response return $this->readMultipleInputRegistersParser($data);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$receivedData = $this->readMultipleInputRegistersParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "readMultipleInputRegisters: DONE\n"; $this->status .= "readMultipleInputRegisters: DONE\n";
// return
return $receivedData; return $receivedData;
} }
@ -778,7 +773,7 @@ class ModbusMaster
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function fc5($unitId, $reference, $data) public function fc5($unitId, $reference, array $data)
{ {
return $this->writeSingleCoil($unitId, $reference, $data); return $this->writeSingleCoil($unitId, $reference, $data);
} }
@ -798,24 +793,21 @@ class ModbusMaster
* @return bool Success flag * @return bool Success flag
* @throws \Exception * @throws \Exception
*/ */
public function writeSingleCoil($unitId, $reference, $data) public function writeSingleCoil($unitId, $reference, array $data)
{ {
$this->status .= "writeSingleCoil: START\n"; $this->status .= "writeSingleCoil: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC5 function () use ($unitId, $reference, $data) {
$packet = $this->writeSingleCoilPacketBuilder($unitId, $reference, $data); return $this->writeSingleCoilPacketBuilder($unitId, $reference, $data);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) {
// receive response return $this->writeSingleCoilParser($data);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$this->writeSingleCoilParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "writeSingleCoil: DONE\n"; $this->status .= "writeSingleCoil: DONE\n";
return true; return $receivedData;
} }
/** /**
@ -828,7 +820,7 @@ class ModbusMaster
* @param array $data * @param array $data
* @return string * @return string
*/ */
private function writeSingleCoilPacketBuilder($unitId, $reference, $data) private function writeSingleCoilPacketBuilder($unitId, $reference, array $data)
{ {
$dataLen = 0; $dataLen = 0;
// build data section // build data section
@ -883,7 +875,7 @@ class ModbusMaster
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function fc6($unitId, $reference, $data) public function fc6($unitId, $reference, array $data)
{ {
return $this->writeSingleRegister($unitId, $reference, $data); return $this->writeSingleRegister($unitId, $reference, $data);
} }
@ -903,24 +895,36 @@ class ModbusMaster
* @return bool Success flag * @return bool Success flag
* @throws \Exception * @throws \Exception
*/ */
public function writeSingleRegister($unitId, $reference, $data) public function writeSingleRegister($unitId, $reference, array $data)
{ {
$this->status .= "writeSingleRegister: START\n"; $this->status .= "writeSingleRegister: START\n";
// connect $result = $this->sendAndReceive(
function () use ($unitId, $reference, $data) {
return $this->writeSingleRegisterPacketBuilder($unitId, $reference, $data);
},
function ($data) {
return $this->writeSingleRegisterParser($data);
}
);
$this->status .= "writeSingleRegister: DONE\n";
return $result;
}
public function sendAndReceive(callable $buildRequest, callable $parseResponse)
{
try {
$this->connect(); $this->connect();
// send FC6 $packet = $buildRequest();
$packet = $this->writeSingleRegisterPacketBuilder($unitId, $reference, $data);
$this->status .= $this->printPacket($packet);
$this->send($packet); $this->send($packet);
// receive response $data = $this->rec();
$rpacket = $this->rec();
$this->status .= $this->printPacket($rpacket); $this->status .= $this->printPacket($data);
// parse packet return $parseResponse($data);
$this->writeSingleRegisterParser($rpacket); } finally {
// disconnect
$this->disconnect(); $this->disconnect();
$this->status .= "writeSingleRegister: DONE\n"; }
return true;
} }
/** /**
@ -985,7 +989,7 @@ class ModbusMaster
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function fc15($unitId, $reference, $data) public function fc15($unitId, $reference, array $data)
{ {
return $this->writeMultipleCoils($unitId, $reference, $data); return $this->writeMultipleCoils($unitId, $reference, $data);
} }
@ -1004,24 +1008,21 @@ class ModbusMaster
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function writeMultipleCoils($unitId, $reference, $data) public function writeMultipleCoils($unitId, $reference, array $data)
{ {
$this->status .= "writeMultipleCoils: START\n"; $this->status .= "writeMultipleCoils: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC15 function () use ($unitId, $reference, $data) {
$packet = $this->writeMultipleCoilsPacketBuilder($unitId, $reference, $data); return $this->writeMultipleCoilsPacketBuilder($unitId, $reference, $data);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) {
// receive response return $this->writeMultipleCoilsParser($data);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$this->writeMultipleCoilsParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "writeMultipleCoils: DONE\n"; $this->status .= "writeMultipleCoils: DONE\n";
return true; return $receivedData;
} }
/** /**
@ -1109,7 +1110,7 @@ class ModbusMaster
* @return bool * @return bool
* @throws \Exception * @throws \Exception
*/ */
public function fc16($unitId, $reference, $data, $dataTypes) public function fc16($unitId, $reference, array $data, array $dataTypes)
{ {
return $this->writeMultipleRegister($unitId, $reference, $data, $dataTypes); return $this->writeMultipleRegister($unitId, $reference, $data, $dataTypes);
} }
@ -1131,24 +1132,21 @@ class ModbusMaster
* @return bool Success flag * @return bool Success flag
* @throws \Exception * @throws \Exception
*/ */
public function writeMultipleRegister($unitId, $reference, $data, $dataTypes) public function writeMultipleRegister($unitId, $reference, array $data, array $dataTypes)
{ {
$this->status .= "writeMultipleRegister: START\n"; $this->status .= "writeMultipleRegister: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC16 function () use ($unitId, $reference, $data, $dataTypes) {
$packet = $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes); return $this->writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) {
// receive response return $this->writeMultipleRegisterParser($data);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$this->writeMultipleRegisterParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "writeMultipleRegister: DONE\n"; $this->status .= "writeMultipleRegister: DONE\n";
return true; return $receivedData;
} }
/** /**
@ -1163,7 +1161,7 @@ class ModbusMaster
* @param array $dataTypes * @param array $dataTypes
* @return string * @return string
*/ */
private function writeMultipleRegisterPacketBuilder($unitId, $reference, $data, $dataTypes) private function writeMultipleRegisterPacketBuilder($unitId, $reference, array $data, array $dataTypes)
{ {
$dataLen = 0; $dataLen = 0;
// build data section // build data section
@ -1254,21 +1252,18 @@ class ModbusMaster
public function maskWriteRegister($unitId, $reference, $andMask, $orMask) public function maskWriteRegister($unitId, $reference, $andMask, $orMask)
{ {
$this->status .= "maskWriteRegister: START\n"; $this->status .= "maskWriteRegister: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC22 function () use ($unitId, $reference, $andMask, $orMask) {
$packet = $this->maskWriteRegisterPacketBuilder($unitId, $reference, $andMask, $orMask); return $this->maskWriteRegisterPacketBuilder($unitId, $reference, $andMask, $orMask);
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) {
// receive response return $this->maskWriteRegisterParser($data);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$this->maskWriteRegisterParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "maskWriteRegister: DONE\n"; $this->status .= "maskWriteRegister: DONE\n";
return true; return $receivedData;
} }
/** /**
@ -1333,7 +1328,7 @@ class ModbusMaster
* @return false|array * @return false|array
* @throws \Exception * @throws \Exception
*/ */
public function fc23($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes) public function fc23($unitId, $referenceRead, $quantity, $referenceWrite, array $data, array $dataTypes)
{ {
return $this->readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes); return $this->readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes);
} }
@ -1358,27 +1353,22 @@ class ModbusMaster
* @return false|array Success flag or array of data. * @return false|array Success flag or array of data.
* @throws \Exception * @throws \Exception
*/ */
public function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes) public function readWriteRegisters($unitId, $referenceRead, $quantity, $referenceWrite, array $data, array $dataTypes)
{ {
$this->status .= "readWriteRegisters: START\n"; $this->status .= "readWriteRegisters: START\n";
// connect
$this->connect(); $receivedData = $this->sendAndReceive(
// send FC23 function () use ($unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes) {
$packet = $this->readWriteRegistersPacketBuilder( return $this->readWriteRegistersPacketBuilder(
$unitId, $referenceRead, $quantity, $referenceWrite, $data, $unitId, $referenceRead, $quantity, $referenceWrite, $data, $dataTypes
$dataTypes
); );
$this->status .= $this->printPacket($packet); },
$this->send($packet); function ($data) {
// receive response return $this->readWriteRegistersParser($data);
$rpacket = $this->rec(); }
$this->status .= $this->printPacket($rpacket); );
// parse packet
$receivedData = $this->readWriteRegistersParser($rpacket);
// disconnect
$this->disconnect();
$this->status .= "writeMultipleRegister: DONE\n"; $this->status .= "writeMultipleRegister: DONE\n";
// return
return $receivedData; return $receivedData;
} }
@ -1400,9 +1390,10 @@ class ModbusMaster
$referenceRead, $referenceRead,
$quantity, $quantity,
$referenceWrite, $referenceWrite,
$data, array $data,
$dataTypes array $dataTypes
) { )
{
$dataLen = 0; $dataLen = 0;
// build data section // build data section

Loading…
Cancel
Save