I'm working on a project that interacts with two relay boards connected by serial ports. I have successfully sent information through the serial port that's on the motherboard. However, I'm trying to send information to a serial port expansion PCI card, but can't seem to get it to work. Also, I know that the connection works because there is some software that came with the relay boards and it is able to control both boards just fine. I'm very confident that it's a php problem that's holding me up. Here are the two files I'm using to test this:
pump_functions.php
function setup_com() {
//standard com port
`mode com1: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
//expansion slot is on port 3
`mode com3: BAUD=9600 PARITY=N data=8 stop=1 xon=off`;
}
function compute_ing_time($amount, $visc) {
$time = $amount * $visc;
return $time;
}
function compute_port_number($pump_num) {
if ($pump_num <= 8) {
$port_num = 1;
} else {
$port_num = 3;
}
return $port_num;
}
function timed_pump($pump_num, $time) {
$port_num = compute_port_number($pump_num);
$port_string = "COM" . $port_num . ":";
$pump_string = "T" . $pump_num . "\r";
echo "Port Number is " . $port_num . " and port string is " . $port_string . "<br>";
flush();
$fp = fopen($port_string, "w+");
if(!$fp) {
echo "Error opening " . $port_string . " for pump number " . $pump_num;
} else {
fwrite($fp, $pump_string);
fflush($fp);
echo "opening pump<br>";
flush();
usleepWindows($time);
fwrite($fp, $pump_string);
fflush($fp);
echo "closing pump<br><br>";
flush();
}
fclose($fp);
}
function usleepWindows($usec)
{
$start = gettimeofday();
do
{
$stop = gettimeofday();
$timePassed = 1000000 * ($stop['sec'] - $start['sec'])
+ $stop['usec'] - $start['usec'];
}
while ($timePassed < $usec);
}
testRelays.php
include "pump_functions.php";
setup_com();
timed_pump(3, 2500000);
timed_pump(5, 500000);
timed_pump(12, 500000);
timed_pump(10, 500000);
timed_pump(9, 500000);
It seems to me that this should work just fine... the only difference (unless I'm missing something, which I hope I am) is that one opens COM1 while the other opens COM3, yet COM3 never works. Any help or suggestions would be greatly appreciated.
A couple extra notes that really aren't important but should save me from having to answer them in the future:
I know that the usleep function eats all the CPU, but it's ok because this project is meant for a dedicated machine so the CPU won't be needed for anything else anyway. Then you might ask why I used php, and the answer is that my project does a lot with databases and I'm fairly experienced with php and mysql so it was the easy way to go.