/** * Title * * Description * * @access public */ function poll_device($id) { $rec = SQLSelectOne("SELECT * FROM modbusdevices WHERE ID='" . (int) $id . "'"); if (!$rec['ID']) { return; } $rec['CHECK_LATEST'] = date('Y-m-d H:i:s'); $rec['CHECK_NEXT'] = date('Y-m-d H:i:s', time() + (int) $rec['POLLPERIOD']); SQLUpdate('modbusdevices', $rec); if ($rec['LINKED_OBJECT'] && $rec['LINKED_PROPERTY'] && ($rec['REQUEST_TYPE'] == 'FC5' || $rec['REQUEST_TYPE'] == 'FC6' || $rec['REQUEST_TYPE'] == 'FC15' || $rec['REQUEST_TYPE'] == 'FC16' || $rec['REQUEST_TYPE'] == 'FC23')) { $rec['DATA'] = getGlobal($rec['LINKED_OBJECT'] . '.' . $rec['LINKED_PROPERTY']); } require_once dirname(__FILE__) . '/ModbusMaster.php'; $modbus = new ModbusMaster($rec['HOST'], $rec['PROTOCOL']); if ($rec['PORT']) { $modbus->port = $rec['PORT']; } if ($rec['REQUEST_TYPE'] == 'FC1') { //FC1 Read coils try { $recData = $modbus->readCoils($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']); if (is_array($recData)) { foreach ($recData as $k => $v) { $recData[$k] = (int) $v; } } } catch (Exception $e) { // Print error information if any $rec['LOG'] = date('Y-m-d H:i:s') . " FC1 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC2') { //FC2 Read input discretes try { $recData = $modbus->readInputDiscretes($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']); if (is_array($recData)) { foreach ($recData as $k => $v) { $recData[$k] = (int) $v; } } } catch (Exception $e) { // Print error information if any $rec['LOG'] = date('Y-m-d H:i:s') . " FC2 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC3') { //FC3 Read holding registers try { $recData = $modbus->readMultipleRegisters($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']); } catch (Exception $e) { // Print error information if any $rec['LOG'] = date('Y-m-d H:i:s') . " FC3 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC4') { //FC4 Read holding input registers try { $recData = $modbus->readMultipleInputRegisters($rec['DEVICE_ID'], $rec['REQUEST_START'], $rec['REQUEST_TOTAL']); } catch (Exception $e) { // Print error information if any $rec['LOG'] = date('Y-m-d H:i:s') . " FC4 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC5') { //FC5 Write single coil if ((int) $rec['DATA']) { $data_set = array(TRUE); } else { $data_set = array(FALSE); } try { $modbus->writeSingleCoil($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set); } catch (Exception $e) { $rec['LOG'] = date('Y-m-d H:i:s') . " FC5 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC6') { //FC6 Write single register try { $data_set = array((int) $rec['DATA']); if ($rec['RESPONSE_CONVERT'] == 'r2f') { $dataTypes = array("REAL"); $swapregs = false; } elseif ($rec['RESPONSE_CONVERT'] == 'r2fs') { $dataTypes = array("REAL"); $swapregs = true; } elseif ($rec['RESPONSE_CONVERT'] == 'd2i' || $rec['RESPONSE_CONVERT'] == 'dw2i') { $dataTypes = array("DINT"); $swapregs = false; } elseif ($rec['RESPONSE_CONVERT'] == 'd2is' || $rec['RESPONSE_CONVERT'] == 'dw2is') { $dataTypes = array("DINT"); $swapregs = true; } else { $dataTypes = array("INT"); $swapregs = false; } $recData = $modbus->writeSingleRegister($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set, $dataTypes, $swapregs); } catch (Exception $e) { // Print error information if any $rec['LOG'] = date('Y-m-d H:i:s') . " FC6 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC15') { //FC15 Write multiple coils $data_set = explode(',', $rec['DATA']); foreach ($data_set as $k => $v) { $data_set[$k] = (bool) $v; } try { $modbus->writeMultipleCoils($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set); } catch (Exception $e) { $rec['LOG'] = date('Y-m-d H:i:s') . " FC15 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC16') { //FC16 Write multiple registers try { $data_set = explode(',', $rec['DATA']); $dataTypes = array(); foreach ($data_set as $k => $v) { if ($rec['RESPONSE_CONVERT'] == 'r2f') { $dataTypes[] = "REAL"; $data_set[$k] = (double) $v; $swapregs = false; } elseif ($rec['RESPONSE_CONVERT'] == 'r2fs') { $dataTypes[] = "REAL"; $data_set[$k] = (double) $v; $swapregs = true; } elseif ($rec['RESPONSE_CONVERT'] == 'd2i' || $rec['RESPONSE_CONVERT'] == 'dw2i') { $dataTypes[] = "DINT"; $data_set[$k] = (int) $v; $swapregs = false; } elseif ($rec['RESPONSE_CONVERT'] == 'd2is' || $rec['RESPONSE_CONVERT'] == 'dw2is') { $dataTypes[] = "DINT"; $data_set[$k] = (int) $v; $swapregs = true; } else { $data_set[$k] = (int) $v; $dataTypes[] = "INT"; $swapregs = false; } } $recData = $modbus->writeMultipleRegister($rec['DEVICE_ID'], $rec['REQUEST_START'], $data_set, $dataTypes, $swapregs); } catch (Exception $e) { // Print error information if any $rec['LOG'] = date('Y-m-d H:i:s') . " FC16 Error: {$modbus} {$e}\n" . $rec['LOG']; } } elseif ($rec['REQUEST_TYPE'] == 'FC23') { //FC23 Read/Write multiple registers //TO-DO } //echo $rec['LOG'];exit; if ($rec['REQUEST_TYPE'] == 'FC1' || $rec['REQUEST_TYPE'] == 'FC2' || $rec['REQUEST_TYPE'] == 'FC3' || $rec['REQUEST_TYPE'] == 'FC4' && is_array($recData)) { // PROCESS RESPONSE if ($rec['RESPONSE_CONVERT'] == 'r2f') { //REAL to Float $values = array_chunk($recData, 4); $recData = array(); foreach ($values as $bytes) { echo $recData[] = PhpType::bytes2float($bytes, false); } } elseif ($rec['RESPONSE_CONVERT'] == 'r2fs') { //REAL to Float (swap regs) $values = array_chunk($recData, 4); $recData = array(); foreach ($values as $bytes) { echo $recData[] = PhpType::bytes2float($bytes, true); } } elseif ($rec['RESPONSE_CONVERT'] == 'd2i') { //DINT to integer $values = array_chunk($recData, 4); $recData = array(); foreach ($values as $bytes) { echo $recData[] = PhpType::bytes2signedInt($bytes, false); } } elseif ($rec['RESPONSE_CONVERT'] == 'd2is') { //DINT to integer (swap regs) $values = array_chunk($recData, 4); $recData = array(); foreach ($values as $bytes) { echo $recData[] = PhpType::bytes2signedInt($bytes, true); } } elseif ($rec['RESPONSE_CONVERT'] == 'dw2i') { //DWORD to integer $values = array_chunk($recData, 4); $recData = array(); foreach ($values as $bytes) { $recData[] = PhpType::bytes2unsignedInt($bytes, false); } } elseif ($rec['RESPONSE_CONVERT'] == 'dw2is') { //DWORD to integer (swap regs) $values = array_chunk($recData, 4); $recData = array(); foreach ($values as $bytes) { $recData[] = PhpType::bytes2unsignedInt($bytes, true); } } elseif ($rec['RESPONSE_CONVERT'] == 'i2i') { //INT to integer $values = array_chunk($recData, 2); $recData = array(); foreach ($values as $bytes) { $recData[] = PhpType::bytes2signedInt($bytes, false); } } elseif ($rec['RESPONSE_CONVERT'] == 'w2i') { //WORD to integer $values = array_chunk($recData, 2); $recData = array(); foreach ($values as $bytes) { $recData[] = PhpType::bytes2unsignedInt($bytes, false); } } elseif ($rec['RESPONSE_CONVERT'] == 's') { //String $recData = array(PhpType::bytes2string($recData)); } else { // } $result = implode(',', $recData); if ($result && $result != $rec['DATA']) { $rec['LOG'] = date('Y-m-d H:i:s') . " " . $result . "\n" . $rec['LOG']; } $rec['DATA'] = $result; SQLUpdate('modbusdevices', $rec); if ($rec['LINKED_OBJECT'] && $rec['LINKED_PROPERTY']) { setGlobal($rec['LINKED_OBJECT'] . '.' . $rec['LINKED_PROPERTY'], $rec['DATA'], array($this->name => '0')); } } else { SQLUpdate('modbusdevices', $rec); } }
<?php require_once dirname(__FILE__) . '/../Phpmodbus/ModbusMaster.php'; // Create Modbus object $modbus = new ModbusMaster("192.192.15.51", "UDP"); try { // FC 1 $recData = $modbus->readCoils(0, 12288, 12); } catch (Exception $e) { // Print error information if any echo $modbus; echo $e; exit; } // Print status information echo "</br>Status:</br>" . $modbus; // Print read data echo "</br>Data:</br>"; var_dump($recData); echo "</br>";