Skip to content

Commit 0ee828f

Browse files
committed
Removed double newline from send command.
If you added a newline to the send, the server would add a 2nd newline. Now it's up to the sender to choose a newline. This required refactoring of all commands. Also added help commands when connect to websocket. Also inlined the home.html file so no more dependency errors. Former-commit-id: e634c683f8e8f1d6d0cab9d2cdd7111f311f44c8 [formerly 506a90befec4e2d0c240c20e57b6aa3c255e737d] Former-commit-id: a547b00d3e44a08b83b407b5cf0ea98d6d1d52fa
1 parent a1f590a commit 0ee828f

File tree

5 files changed

+144
-31
lines changed

5 files changed

+144
-31
lines changed

conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Supports Windows, Linux, Mac, and Raspberr Pi
1+
// Supports Windows, Linux, Mac, and Raspberry Pi
22

33
package main
44

hub.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,32 @@ func (h *hub) run() {
3636
select {
3737
case c := <-h.register:
3838
h.connections[c] = true
39+
// send supported commands
40+
c.send <- []byte("{\"Commands\" : [\"list\", \"open [portName] [baud]\", \"send [portName] [cmd]\", \"close [portName]\"]} ")
3941
case c := <-h.unregister:
4042
delete(h.connections, c)
4143
close(c.send)
4244
case m := <-h.broadcast:
4345
log.Print("Got a broadcast")
44-
log.Print(string(m))
45-
//log.Print(h.broadcast)
46-
checkCmd(m)
47-
log.Print("-----")
48-
49-
for c := range h.connections {
50-
select {
51-
case c.send <- m:
52-
log.Print("did broadcast to ")
53-
log.Print(c.ws.RemoteAddr())
54-
//c.send <- []byte("hello world")
55-
default:
56-
delete(h.connections, c)
57-
close(c.send)
58-
go c.ws.Close()
46+
//log.Print(m)
47+
//log.Print(len(m))
48+
if len(m) > 0 {
49+
//log.Print(string(m))
50+
//log.Print(h.broadcast)
51+
checkCmd(m)
52+
log.Print("-----")
53+
54+
for c := range h.connections {
55+
select {
56+
case c.send <- m:
57+
log.Print("did broadcast to ")
58+
log.Print(c.ws.RemoteAddr())
59+
//c.send <- []byte("hello world")
60+
default:
61+
delete(h.connections, c)
62+
close(c.send)
63+
go c.ws.Close()
64+
}
5965
}
6066
}
6167
case m := <-h.broadcastSys:
@@ -82,7 +88,7 @@ func (h *hub) run() {
8288
func checkCmd(m []byte) {
8389
//log.Print("Inside checkCmd")
8490
s := string(m[:])
85-
//log.Print(s)
91+
log.Print(s)
8692

8793
sl := strings.ToLower(s)
8894

@@ -97,7 +103,9 @@ func checkCmd(m []byte) {
97103
go spErr("You did not specify a serial port")
98104
return
99105
}
100-
baud, err := strconv.Atoi(args[2])
106+
107+
baudStr := strings.Replace(args[2], "\n", "", -1)
108+
baud, err := strconv.Atoi(baudStr)
101109
if err != nil {
102110
go spErr("Problem converting baud rate " + args[2])
103111
return
@@ -107,16 +115,22 @@ func checkCmd(m []byte) {
107115
} else if strings.HasPrefix(sl, "close") {
108116

109117
args := strings.Split(s, " ")
110-
go spClose(args[1])
118+
if len(args) > 1 {
119+
go spClose(args[1])
120+
} else {
121+
go spErr("You did not specify a port to close")
122+
}
111123

112-
} else if strings.HasPrefix(sl, "send ") {
124+
} else if strings.HasPrefix(sl, "send") {
113125

114126
//args := strings.Split(s, "send ")
115127
go spWrite(s)
116128

117-
} else if s == "list" {
129+
} else if strings.HasPrefix(sl, "list") {
118130
go spList()
119131
//go getListViaWmiPnpEntity()
132+
} else {
133+
go spErr("Could not understand command.")
120134
}
121135

122136
//log.Print("Done with checkCmd")

main.go

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Supports Windows, Linux, Mac, and Raspberry Pi
1+
// Version 1.2
2+
// Supports Windows, Linux, Mac, and Raspberry Pi, Beagle Bone Black
23

34
package main
45

@@ -7,15 +8,15 @@ import (
78
"go/build"
89
"log"
910
"net/http"
10-
"path/filepath"
11+
//"path/filepath"
1112
"text/template"
1213
)
1314

1415
var (
15-
VERSION = "1.1"
16-
addr = flag.String("addr", ":8989", "http service address")
17-
assets = flag.String("assets", defaultAssetPath(), "path to assets")
18-
homeTempl *template.Template
16+
VERSION = "1.1"
17+
addr = flag.String("addr", ":8989", "http service address")
18+
assets = flag.String("assets", defaultAssetPath(), "path to assets")
19+
//homeTempl *template.Template
1920
)
2021

2122
func defaultAssetPath() string {
@@ -28,7 +29,7 @@ func defaultAssetPath() string {
2829
}
2930

3031
func homeHandler(c http.ResponseWriter, req *http.Request) {
31-
homeTempl.Execute(c, req.Host)
32+
homeTemplate.Execute(c, req.Host)
3233
}
3334

3435
func main() {
@@ -40,7 +41,7 @@ func main() {
4041
flag.Parse()
4142
f := flag.Lookup("addr")
4243
log.Print("Started server and websocket on localhost" + f.Value.String())
43-
homeTempl = template.Must(template.ParseFiles(filepath.Join(*assets, "home.html")))
44+
//homeTempl = template.Must(template.ParseFiles(filepath.Join(*assets, "home.html")))
4445

4546
// launch the hub routine which is the singleton for the websocket server
4647
go h.run()
@@ -55,3 +56,101 @@ func main() {
5556
log.Fatal("ListenAndServe:", err)
5657
}
5758
}
59+
60+
var homeTemplate = template.Must(template.New("home").Parse(homeTemplateHtml))
61+
62+
// If you navigate to this server's homepage, you'll get this HTML
63+
// so you can directly interact with the serial port server
64+
const homeTemplateHtml = `<!DOCTYPE html>
65+
<html>
66+
<head>
67+
<title>Serial Port Example</title>
68+
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
69+
<script type="text/javascript">
70+
$(function() {
71+
72+
var conn;
73+
var msg = $("#msg");
74+
var log = $("#log");
75+
76+
function appendLog(msg) {
77+
var d = log[0]
78+
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
79+
msg.appendTo(log)
80+
if (doScroll) {
81+
d.scrollTop = d.scrollHeight - d.clientHeight;
82+
}
83+
}
84+
85+
$("#form").submit(function() {
86+
if (!conn) {
87+
return false;
88+
}
89+
if (!msg.val()) {
90+
return false;
91+
}
92+
conn.send(msg.val() + "\n");
93+
msg.val("");
94+
return false
95+
});
96+
97+
if (window["WebSocket"]) {
98+
conn = new WebSocket("ws://{{$}}/ws");
99+
conn.onclose = function(evt) {
100+
appendLog($("<div><b>Connection closed.</b></div>"))
101+
}
102+
conn.onmessage = function(evt) {
103+
appendLog($("<div/>").text(evt.data))
104+
}
105+
} else {
106+
appendLog($("<div><b>Your browser does not support WebSockets.</b></div>"))
107+
}
108+
});
109+
</script>
110+
<style type="text/css">
111+
html {
112+
overflow: hidden;
113+
}
114+
115+
body {
116+
overflow: hidden;
117+
padding: 0;
118+
margin: 0;
119+
width: 100%;
120+
height: 100%;
121+
background: gray;
122+
}
123+
124+
#log {
125+
background: white;
126+
margin: 0;
127+
padding: 0.5em 0.5em 0.5em 0.5em;
128+
position: absolute;
129+
top: 0.5em;
130+
left: 0.5em;
131+
right: 0.5em;
132+
bottom: 3em;
133+
overflow: auto;
134+
}
135+
136+
#form {
137+
padding: 0 0.5em 0 0.5em;
138+
margin: 0;
139+
position: absolute;
140+
bottom: 1em;
141+
left: 0px;
142+
width: 100%;
143+
overflow: hidden;
144+
}
145+
146+
</style>
147+
</head>
148+
<body>
149+
<div id="log"></div>
150+
<form id="form">
151+
<input type="submit" value="Send" />
152+
<input type="text" id="msg" size="64"/>
153+
</form>
154+
</body>
155+
</html>
156+
`

serial.go.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b1c45a900b5a217fdf35ff4a6d99bf159e3da5b4
1+
4b4a2756e40ce076283ed745ce36528a896c1058

serialport.go.REMOVED.git-id

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a421013945592ac5be5e1c2801a7cd79d65685d8
1+
06dd8da3d283650c785c0e949f4acbae22be3974

0 commit comments

Comments
 (0)