PHP code example of orderlyconnect / astm-e1394-91
1. Go to this page and download the library: Download orderlyconnect/astm-e1394-91 library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Astm\Delimiters;
$d = new Delimiters(field: '!', component: '@', repeat: '#', escape: '$');
$message = Astm::build($d)->sender('MY-LIS')->result(...)->build();
// Clone a received message and enrich it
$enriched = Astm::modify($received)
->comment('Critical values phoned to ward at 09:15', 'LIS', 'I')
->build();
// Sequence numbers continue correctly from the original
// Compare the two messages
$diff = Astm::diff($received, $enriched);
if ($diff->hasDifferences()) {
foreach ($diff->getSummary() as $line) {
echo $line . "\n";
}
}
// TCP — one-liner
Astm::sendTcp($message, '192.168.1.50', port: 3001);
// TCP — fine-grained
use Astm\Sender;
use Astm\Transport\TcpTransport;
$transport = new TcpTransport('192.168.1.50', 3001, connectTimeout: 10, readTimeout: 30);
$sender = new Sender($transport, maxEnqRetries: 3, maxFrameRetries: 3);
$transport->connect();
try {
$sender->send($message);
} finally {
$transport->disconnect();
}
// Serial port
use Astm\Transport\SerialTransport;
$transport = new SerialTransport('/dev/ttyUSB0', baud: 9600);
(new \Astm\Sender($transport))->send($message);
// Write LLP-framed binary file
Astm::writeFile($message, '/tmp/output.astm');
$collection = Astm::readFile('/tmp/output.astm');
use Astm\Receiver;
use Astm\Transport\TcpTransport;
$transport = new TcpTransport('0.0.0.0', 3001);
$transport->connect();
$receiver = new Receiver(
transport: $transport,
onMessage: function (\Astm\Message $msg): void {
foreach ($msg->getAbnormalResults() as $r) {
printf("⚠ %-12s %6s [%s]\n", $r->getTestName(), $r->getValue(), $r->getAbnormalFlag());
}
},
);
// Non-blocking (event loop)
while (true) {
$receiver->tick();
while ($msg = $receiver->popMessage()) { handleMessage($msg); }
usleep(1_000);
}
// Blocking
$receiver->listen();
$collection = Astm::parseFile('/batch/session.astm');
$collection->getAbnormalResults(); // all H/L/A across all sessions
$collection->getHighResults(); // H/HH only
$collection->getLowResults(); // L/LL only
$collection->getResultsByTest('WBC'); // cross-session
$collection->getMessagesWithAbnormalities(); // messages with any flag
$collection->getAllResultsMapped(); // grouped by test name
use Astm\DateTimeHelper;
DateTimeHelper::parse('20250329143330'); // DateTimeImmutable
DateTimeHelper::parseDate('19920801'); // DateTimeImmutable
DateTimeHelper::format($dt); // '20250329143330'
DateTimeHelper::formatDate($dt); // '19920801'
DateTimeHelper::now(); // current UTC as YYYYMMDDHHMMSS
DateTimeHelper::today(); // current UTC date as YYYYMMDD
DateTimeHelper::isValid('20250329143330'); // true
use Astm\Records\AbstractRecord;
final class ManufacturerRecord extends AbstractRecord
{
public const TYPE = 'M';
public function getType(): string { return self::TYPE; }
public function getVendorData(): string { return $this->getField(2); }
}
\Astm\Parser::registerRecordType('M', ManufacturerRecord::class);
$message = (new \Astm\Parser())->parse($raw);
$mRecords = $message->getRecordsByType('M');