Skip to content

Commit 0a9ae26

Browse files
committed
Add Processing example and fix typos in Python comments
1 parent 865640f commit 0a9ae26

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import processing.net.*;
2+
3+
/*
4+
Very simple example of how to use the Line-us API. The most important thing to
5+
remember is to read the response after sending a command. The TCP buffer on
6+
Line-us is small and it does not cope well with having to buffer more than
7+
one message.
8+
9+
Good Luck!
10+
*/
11+
12+
LineUs myLineUs;
13+
14+
void setup() {
15+
myLineUs = new LineUs(this);
16+
}
17+
18+
void draw() {
19+
20+
delay(1000);
21+
22+
myLineUs.g01(900, 300, 0);
23+
myLineUs.g01(900, -300, 0);
24+
myLineUs.g01(900, -300, 1000);
25+
26+
myLineUs.g01(1200, 300, 0);
27+
myLineUs.g01(1200, -300, 0);
28+
myLineUs.g01(1200, -300, 1000);
29+
30+
myLineUs.g01(900, 0, 0);
31+
myLineUs.g01(1200, 0, 0);
32+
myLineUs.g01(1200, 0, 1000);
33+
34+
myLineUs.g01(1500, 150, 0);
35+
myLineUs.g01(1500, -300, 0);
36+
myLineUs.g01(1500, -300, 1000);
37+
38+
myLineUs.g01(1500, 250, 0);
39+
myLineUs.g01(1500, 300, 0);
40+
myLineUs.g01(1500, 300, 1000);
41+
42+
delay(1000);
43+
44+
exit();
45+
}
46+
47+
//An example class to show how to use the Line-us API
48+
49+
class LineUs {
50+
51+
Client lineUs;
52+
Boolean connected = false;
53+
String helloMessage;
54+
55+
LineUs(PApplet papp) {
56+
lineUs = new Client(papp, "line-us.local", 1337);
57+
connected = true;
58+
helloMessage = readResponse();
59+
}
60+
61+
String getHelloString() {
62+
if(connected) {
63+
return helloMessage;
64+
} else {
65+
return("Not connected");
66+
}
67+
}
68+
69+
//Close the connection to the Line-us
70+
void disconnect() {
71+
lineUs.stop();
72+
connected = false;
73+
}
74+
75+
//Send a G01 (interpolated move), and wait for the response before returning
76+
void g01(int x, int y, int z) {
77+
String cmd = "G01 X";
78+
cmd += str(x);
79+
cmd += " Y";
80+
cmd += str(y);
81+
cmd += " Z";
82+
cmd += str(z);
83+
sendCommand(cmd);
84+
readResponse();
85+
}
86+
87+
//Read from the socket one byte at a time until we get a null
88+
String readResponse() {
89+
String line = "";
90+
int c;
91+
while(true) {
92+
c = lineUs.read();
93+
if(c != 0 && c != -1) {
94+
line += (char) c;
95+
} else if(c == 0) {
96+
break;
97+
}
98+
}
99+
return line;
100+
}
101+
102+
//Send the command to Line-us
103+
void sendCommand(String command) {
104+
command += "\0";
105+
lineUs.write(command);
106+
}
107+
108+
}

Python/HelloWorld.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def disconnect(self):
2323
self.__connected = False
2424

2525
def g01(self, x, y, z):
26-
"""Send a G01 (interpolated move, and wait for the response before returning"""
26+
"""Send a G01 (interpolated move), and wait for the response before returning"""
2727
cmd = b'G01 X'
2828
cmd += str(x).encode()
2929
cmd += b' Y'
@@ -34,7 +34,7 @@ def g01(self, x, y, z):
3434
self.__read_response()
3535

3636
def __read_response(self):
37-
"""Read form the socket one byte at a time until we get a null"""
37+
"""Read from the socket one byte at a time until we get a null"""
3838
line = b''
3939
while True:
4040
char = self.__line_us.recv(1)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Line-us can be controlled using a simple TCP sockets API. The commands are a sub
1111
- [Co-ordinate System](#co-ordinate-system)
1212
- [CAUTION for firmware 1.0.1 and lower](#caution-for-firmware-101-and-lower)
1313
- [Simple Python Example](#simple-python-example)
14+
- [Simple Processing Example](#simple-processing-example)
1415

1516
### Making a Connection
1617
The default name for Line-us is `line-us`, although it can be changed using the `M550` Gcode command or using the App. Line-us supports mDNS (Bonjour) so by default the hostname will be `line-us.local` and it listens on port 1337. The connection to Line-us can be tested with a telnet client by using the command `telnet line-us.local 1337`. On a successful connection Line-us will respond with a `hello` message followed by `KEY:value` pairs for `VERSION` (firmware version number) `NAME` (the name of the Line-us) and `SERIAL` (the serial number of the Line-us). The `hello` message (like all messages from Line-us) is terminated with `\r\n\0`. It is **very important that the full `hello` message is read from the socket including the `\0` before any commands are sent**.
@@ -33,3 +34,5 @@ It should not be possible to send a GCode that overstretches the arm, or causes
3334
### Simple Python Example
3435
[Source code](Python/HelloWorld.py#L1) for a very simple example can be downloaded from [here](../../raw/master/Python/HelloWorld.py). The example works with Python 2.7 and Python 3, but Python 3 is preferred.
3536

37+
### Simple Processing Example
38+
[Source code](Processing/HelloWorld/HelloWorld.pde#L1) for a very simple example can be downloaded from [here](../../raw/master/Processing/HelloWorld). The example works with with Processing 3.3.7 (Java).

0 commit comments

Comments
 (0)