Skip to content

Commit 840397e

Browse files
committed
Initial commit
1 parent 60fe947 commit 840397e

File tree

7 files changed

+1231
-0
lines changed

7 files changed

+1231
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## COM Port to WebService Proxy Tools
2+
3+
These are some very simple tools to proxy data from a serial port on windows (COM) to a WebSocket so that
4+
a html page can read this data and act on them.
5+
6+
The project is written in PHP and consists of two separate programs:
7+
8+
`com2ip` - connects to a COM port and forwards the data to a local TCP port
9+
`ip2web` - Acts as the server. Establishes listening ports for the raw TCP port and a WebSocket server. This should be started first before anything else.
10+
11+
# Installation
12+
13+
You need to have php installed (preferrably 7.0+) and composer installed. Then run `composer install` to download all the dependencies.

com2ip.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
try {
4+
5+
if(count($argv)<2) throw new Exception("Invalid number of arguments");
6+
7+
if(preg_match('#^(?:COM)?(\d+)$#',$argv[1],$match)) $com = intval($match[1]);
8+
else throw new Exception("Invalid COM port");
9+
10+
$tcp_port = 8081;
11+
if(isset($argv[2])) {
12+
if (preg_match('#^\d+$#', $argv[2], $match)) $tcp_port = intval($match[0]);
13+
else throw new Exception("Invalid specified TCP port");
14+
}
15+
16+
$comPath = "\\\\.\\COM{$com}";
17+
18+
$file = fopen($comPath, "r+b");
19+
if (!$file) throw new Exception("Can't open COM$com port");
20+
21+
$fp = stream_socket_client("tcp://127.0.0.1:$tcp_port");
22+
if(!$fp) throw new Exception("Can't open TCP port $tcp_port for writing");
23+
24+
print "All ports OPEN. Proxying data. Press CTRL-C to quit.\n";
25+
26+
while (true) {
27+
$data = fgets($file);
28+
$data = preg_replace('#\s#','',$data);
29+
if($data == '0') continue; // Filter out 0, since we aren't interested in those
30+
if ($data===false) break;
31+
fwrite($fp, $data);
32+
}
33+
34+
fclose($file);
35+
fclose($fp);
36+
}
37+
catch(Exception $e) {
38+
print "Error: " . $e->getMessage() . "\n";
39+
print "Usage: com2ip COMPORT IPPORT\n";
40+
print " COMPORT - The windows COM port to open. Format: integer or COM#\n";
41+
print " IPPORT - The socket tcp port to relay data to. This port must be already listening for connections (default: 8081)\n";
42+
}

composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "ttk/com2web-proxy",
3+
"description": "A COM port to WebSocket proxy",
4+
"type": "project",
5+
"authors": [
6+
{
7+
"name": "Tom Kaminski",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"autoload": {
12+
"psr-4": {
13+
"MyApp\\": "src"
14+
}
15+
},
16+
"require": {
17+
"cboden/ratchet": "^0.4.1"
18+
}
19+
}

0 commit comments

Comments
 (0)