-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvanced.php
More file actions
71 lines (53 loc) · 1.66 KB
/
advanced.php
File metadata and controls
71 lines (53 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use PhpSerial\Configuration;
use PhpSerial\SerialPort;
if (file_exists(__DIR__ . '/../.env')) {
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();
}
$device = $_ENV['SERIAL_PORT'] ?? $_SERVER['SERIAL_PORT'] ?? false;
if (!$device) {
echo "Error: SERIAL_PORT environment variable not set.\n";
exit(1);
}
try {
$config = new Configuration(
baudRate: 115200,
dataBits: 8,
parity: Configuration::PARITY_NONE,
stopBits: Configuration::STOP_BITS_1
);
$port = new SerialPort($device, $config);
echo "Opening serial port: {$device} at 115200 baud\n";
$port->open();
echo "Waiting for Arduino to initialize...\n";
sleep(2); // Arduinoのリセット待機
// Arduino の起動メッセージを読み捨て
$startupMessage = $port->readLine(timeout: 1000);
if ($startupMessage) {
echo "Arduino startup: {$startupMessage}\n\n";
}
echo "Sending multiple commands...\n\n";
$commands = [
"LED_ON\n",
"GET_TEMP\n",
"GET_STATUS\n",
];
foreach ($commands as $cmd) {
echo "Sending: " . trim($cmd) . "\n";
$port->write($cmd);
// Arduinoの処理とレスポンス待機
usleep(200000); // 200ms待機
$response = $port->readLine(timeout: 2000);
echo "Response: " . ($response ?: "(none)") . "\n\n";
// 次のコマンド前に少し待機
usleep(100000); // 100ms待機
}
$port->close();
echo "Done.\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
exit(1);
}