Send bit #167
Replies: 4 comments 2 replies
This comment has been hidden.
This comment has been hidden.
-
also. FC6 (WriteSingleRegisterRequest) is not correct way to write single bit. You really should use FC5 or FC22 for it. Problem with FC6 is that it send 16 bit value thus overwriting other 15 bits in that register. FC5 can address single bit <?php
use ModbusTcpClient\Network\BinaryStreamConnection;
use ModbusTcpClient\Packet\ModbusFunction\MaskWriteRegisterRequest;
use ModbusTcpClient\Packet\ModbusFunction\MaskWriteRegisterResponse;
use ModbusTcpClient\Packet\ResponseFactory;
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/logger.php';
$connection = BinaryStreamConnection::getBuilder()
->setPort(5020)
->setHost('127.0.0.1')
->setLogger(new EchoLogger())
->build();
$readStartAddress = 12288;
$bitPosition = 2; // third bit (starts from 0)
$ANDMask = 0x0000; // 2 bytes
$ANDMask |= (1 << $bitPosition); // set the bit, set third bit to 1. $ANDMask = 0x0004, 0b00000100
$ORMask = 0x0000; // 2 bytes, bin = 0b00000111
//$ORMask &= ~(1 << $bitPosition); // clear the bit, set third bit to 0. $ORMask = 0x0003, 0b00000011
$unitID = 1;
$packet = new MaskWriteRegisterRequest(
$readStartAddress,
$ANDMask,
$ORMask,
$unitID
); // NB: This is Modbus TCP packet not Modbus RTU over TCP!
echo 'Packet to be sent (in hex): ' . $packet->toHex() . PHP_EOL;
try {
$binaryData = $connection->connect()->sendAndReceive($packet);
echo 'Binary received (in hex): ' . unpack('H*', $binaryData)[1] . PHP_EOL;
/* @var $response MaskWriteRegisterResponse */
$response = ResponseFactory::parseResponseOrThrow($binaryData);
echo 'Parsed packet (in hex): ' . $response->toHex() . PHP_EOL;
print_r($response->getANDMask());
print_r($response->getORMask());
} catch (Exception $exception) {
echo 'An exception occurred' . PHP_EOL;
echo $exception->getMessage() . PHP_EOL;
echo $exception->getTraceAsString() . PHP_EOL;
} finally {
$connection->close();
} |
Beta Was this translation helpful? Give feedback.
-
So i can use example https://github.com/aldas/modbus-tcp-client/blob/master/examples/fc5.php for a single bit?
If i want to address register 8193 and bit 2 the corresponding coil would be 8193*16+2? |
Beta Was this translation helpful? Give feedback.
-
I ask datakom - why this dont work - they answer - that we need to use decimal value converted from bit. |
Beta Was this translation helpful? Give feedback.
-
Hello.
I have modbus device Datakom generator controller.
I need to help with sending BIT command to emulate buttons on it.
I make library with function
Then i sending data, for example for the other gensed controller.
==
How using this way to send BIT 2 to register 8193 (as writen in manual)
Full manual located at https://www.datakom.com.tr/upload/Files/500_MODBUS.pdf
Beta Was this translation helpful? Give feedback.
All reactions