代码拉取完成,页面将自动刷新
同步操作将从 不败少龙/modbus-tcp-client 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Exception\InvalidArgumentException;
use ModbusTcpClient\Packet\ErrorResponse;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Packet\ModbusRequest;
use ModbusTcpClient\Packet\ProtocolDataUnitRequest;
use ModbusTcpClient\Packet\Word;
use ModbusTcpClient\Utils\Types;
/**
* Request for Mask Write Register (FC=22)
*
* Example packet: \x81\x80\x00\x00\x00\x08\x10\x16\x00\x04\x00\xF2\x00\x25
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x08 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x10 - unit id
* \x16 - function code
* \x00\x04 - start address
* \x00\xF2 - AND mask
* \x00\x25 - OR mask
*
*/
class MaskWriteRegisterRequest extends ProtocolDataUnitRequest implements ModbusRequest
{
/**
* @var int AND mask to be sent to modbus
*/
private int $andMask;
/**
* @var int OR mask to be sent to modbus
*/
private int $orMask;
public function __construct(int $startAddress, int $andMask, int $orMask, int $unitId = 0, int $transactionId = null)
{
parent::__construct($startAddress, $unitId, $transactionId);
$this->andMask = $andMask;
$this->orMask = $orMask;
$this->validate();
}
public function validate(): void
{
parent::validate();
// value is 2 bytes in packet so it in range of uint16 (0 - 65535) or int16 (-32768 - +32767)
if (!(($this->andMask >= Types::MIN_VALUE_INT16) && ($this->andMask <= Types::MAX_VALUE_UINT16))) {
throw new InvalidArgumentException("AND mask is out of range (u)int16: {$this->andMask}", 3);
}
if (!(($this->orMask >= Types::MIN_VALUE_INT16) && ($this->orMask <= Types::MAX_VALUE_UINT16))) {
throw new InvalidArgumentException("OR mask is out of range (u)int16: {$this->orMask}", 3);
}
}
public function getFunctionCode(): int
{
return ModbusPacket::MASK_WRITE_REGISTER;
}
public function __toString(): string
{
return parent::__toString()
. Types::toRegister($this->getANDMask())
. Types::toRegister($this->getORMask());
}
/**
* @return int
*/
public function getANDMask(): int
{
return $this->andMask;
}
/**
* @return int
*/
public function getORMask(): int
{
return $this->orMask;
}
/**
* @return Word
*/
public function getANDMaskAsWord(): Word
{
return new Word(Types::toInt16($this->andMask));
}
/**
* @return Word
*/
public function getORMaskAsWord(): Word
{
return new Word(Types::toInt16($this->orMask));
}
protected function getLengthInternal(): int
{
return parent::getLengthInternal() + 4; // AND mask size (2 bytes) + OR mask size (2 bytes)
}
/**
* Parses binary string to MaskWriteRegisterRequest or return ErrorResponse on failure
*
* @param string $binaryString
* @return MaskWriteRegisterRequest|ErrorResponse
*/
public static function parse(string $binaryString): MaskWriteRegisterRequest|ErrorResponse
{
return self::parseStartAddressPacket(
$binaryString,
14,
ModbusPacket::MASK_WRITE_REGISTER,
function (int $transactionId, int $unitId, int $startAddress) use ($binaryString) {
$andMask = Types::parseInt16($binaryString[10] . $binaryString[11]);
$orMask = Types::parseInt16($binaryString[12] . $binaryString[13]);
return new self($startAddress, $andMask, $orMask, $unitId, $transactionId);
}
);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。