-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_VS421CPNTA.php
104 lines (78 loc) · 2.94 KB
/
example_VS421CPNTA.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
This example illustrates some basic communication with a device via an HTML form. In this example,
the device being communicated with is a StarTech.com video switcher, model VS421CPNTA.
http://www.startech.com/item/VS421CPNTA-4-Port-Component-Video-Switch-with-RS232.aspx
This device uses RS232 commands to switch between 1 of 4 inputs by sending an 'I' followed by a
single digit 1-4, and will echo back this same string in response.
Port settings are 9600-8-N-1, set specifically in the code below. Failure to specifically set these
may result in incorrect defaults being used.
A timer is used in the function that reads the serial input, .5 seconds is the allotted time for the
unit to echo back a response, other devices may need more time for their replies depending on port
speed and the amount of data being sent back, this is just an example and is sufficient for this
device. No end of line character is sent, but we could instead have used a loop to read until a
number was received, again, this code is just an example.
We then echo back the result received, and present the form again for additional input changes.
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$the_input = $_POST['the_input'];
if ($the_input == '') {
echo "<div id='newrequestbox'>";
echo "<form id='FormName' name='FormName' action='example_VS421CPNTA.php' method='post'>
<table width=500>
<tr>
<td>Switch to input? :</td>
<td><input type=text name=the_input maxlength=30 size=30></td>
<td><input type=submit value='Switch'></td>
</tr>
</table>
</form>";
echo "</div>";
} else {
include "php_serial.class.php";
// Let's start the class
$serial = new phpSerial;
// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
// $serial->deviceSet("COM1");
$serial->deviceSet("/dev/cu.usbserial-FTDY7ID6");
// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(2400);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
// Then we need to open it
$serial->deviceOpen();
// To write into
$serial->sendMessage("I".$the_input);
// Or to read from
$read = '';
$theResult = '';
$start = microtime_float();
while ( ($read == '') && (microtime_float() <= $start + 0.5) ) {
$read = $serial->readPort();
if ($read != '') {
$theResult .= $read;
$read = '';
}
}
// If you want to change the configuration, the device must be closed
$serial->deviceClose();
// etc...
echo "Read data: ".$theResult."<br>";
echo "<form id='FormName' name='FormName' action='example_VS421CPNTA.php' method='post'>
<table width=500>
<tr>
<td>Switch to input? :</td>
<td><input type=text name=the_input maxlength=30 size=30></td>
<td><input type=submit value='Switch'></td>
</tr>
</table>
</form>";
}
?>