代码拉取完成,页面将自动刷新
<?php
declare(strict_types=1);
namespace ModbusTcpClient\Packet\ModbusFunction;
use ModbusTcpClient\Exception\InvalidArgumentException;
use ModbusTcpClient\Exception\ModbusException;
use ModbusTcpClient\Packet\ByteCountResponse;
use ModbusTcpClient\Packet\ModbusPacket;
use ModbusTcpClient\Utils\Types;
/**
* Response for Read Coils (FC=01)
*
* Example packet: \x81\x80\x00\x00\x00\x05\x03\x01\x02\xCD\x6B
* \x81\x80 - transaction id
* \x00\x00 - protocol id
* \x00\x05 - number of bytes in the message (PDU = ProtocolDataUnit) to follow
* \x03 - unit id
* \x01 - function code
* \x02 - coils byte count
* \xCD\x6B - coils data (2 bytes = 2 * 8 coils)
*
* @implements \IteratorAggregate<int, bool>
* @implements \ArrayAccess<int, bool>
*/
class ReadCoilsResponse extends ByteCountResponse implements \ArrayAccess, \IteratorAggregate
{
/**
* @var bool[]
*/
private array $coils;
/**
* @var int
*/
private int $coilsBytesLength;
public function __construct(string $rawData, int $unitId = 0, ?int $transactionId = null)
{
$data = substr($rawData, 1);
$this->coilsBytesLength = strlen($data);
$this->coils = Types::binaryStringToBooleanArray($data);
parent::__construct($rawData, $unitId, $transactionId);
}
public function getFunctionCode(): int
{
return ModbusPacket::READ_COILS;
}
/**
* @return bool[]
*/
public function getCoils(): array
{
return iterator_to_array($this->getIterator());
}
public function __toString(): string
{
return parent::__toString()
. Types::byteArrayToByte(Types::booleanArrayToByteArray($this->coils));
}
protected function getLengthInternal(): int
{
return parent::getLengthInternal() + $this->coilsBytesLength;
}
/**
* @return \Generator<bool>
*/
public function getIterator(): \Generator
{
$index = $this->getStartAddress();
foreach ($this->coils as $coil) {
yield $index++ => $coil;
}
}
/**
* @param int $offset
* @param bool $value
* @throws ModbusException
*/
public function offsetSet($offset, $value): void
{
throw new ModbusException('setting value in response is not supported!');
}
/**
* @param int $offset
* @throws ModbusException
*/
public function offsetUnset($offset): void
{
throw new ModbusException('unsetting value in response is not supported!');
}
/**
* @param int $offset
* @return bool
*/
public function offsetExists($offset): bool
{
return isset($this->coils[$offset - $this->getStartAddress()]);
}
/**
* @param int $offset
* @return bool
*/
public function offsetGet($offset): bool
{
$address = $offset - $this->getStartAddress();
$endAddress = ($this->getByteCount() * 8);
if ($address < 0 || $address >= $endAddress) {
throw new InvalidArgumentException('offset out of bounds');
}
return $this->coils[$offset - $this->getStartAddress()];
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。