Skip to content

Commit

Permalink
M105 is nessissary for initialization
Browse files Browse the repository at this point in the history
To cause the serial coms to start up reliably you need to send a M105
first thing after connection. #26
  • Loading branch information
madhephaestus committed May 13, 2016
1 parent b63c18e commit 9d6b256
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.neuronrobotics.sdk.addons.kinematics.gcodebridge;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -19,8 +21,8 @@ public class GcodeDevice extends NonBowlerDevice implements IGcodeExecuter{

private NRSerialPort serial;

private InputStream ins;
private OutputStream outs;
private DataInputStream ins=null;
private DataOutputStream outs=null;
private int timeoutMs = 1000;
private GCodeDeviceConfiguration config = new GCodeDeviceConfiguration();

Expand All @@ -31,26 +33,33 @@ public GcodeDevice(NRSerialPort serial){

@Override
public void disconnectDeviceImp() {
if(serial.isConnected())
if(serial.isConnected()){
runLine("M84");// Disable motors on exit
if(ins!=null){
getLine();// fluch buffer
}
if(outs!=null){
try {
ins.close();
outs.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ins=null;
}
if(outs!=null){
try {
outs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outs=null;
}
if(ins!=null)
try {
ins.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outs=null;
ins=null;
if(serial.isConnected())
serial.disconnect();
}
Expand All @@ -61,9 +70,9 @@ public boolean connectDeviceImp() {
if(!serial.connect()){
throw new RuntimeException("Failed to connect to the serial device");
}
ins=serial.getInputStream();
outs = serial.getOutputStream();

ins= new DataInputStream(serial.getInputStream());
outs = new DataOutputStream(serial.getOutputStream());
runLine("M105");// initializes the device
return true;
}

Expand All @@ -77,11 +86,8 @@ private String getLine(){

String ret="";
try {
if(ins.available()>0){
java.util.Scanner s = new java.util.Scanner(ins).useDelimiter("\\A");
if(s.hasNext()){
ret =s.next();
}
while(ins.available()>0){
ret+=new String(new byte[] {(byte) ins.read()});
}
} catch (IOException e) {
// TODO Auto-generated catch block
Expand All @@ -90,15 +96,15 @@ private String getLine(){
return ret;
}

//usb.dst contains "1.121.2"
@Override
public String runLine(String line) {
if(!line.endsWith("\r\n"))
line = line+"\r\n";
if(!line.startsWith("\r\n"))
line = "\r\n"+line;
try {
//synchronized(outs){
outs.write(line.getBytes());
outs.flush();
//}
} catch (IOException e) {
// TODO Auto-generated catch block
Expand Down
20 changes: 11 additions & 9 deletions test/java/src/junit/test/neuronrobotics/utilities/GCODETest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ public void M105() {
}
}
@Test
public void linkFactory(){
LinkFactory lf = new LinkFactory();
LinkConfiguration confp = new LinkConfiguration();
confp.setType(LinkType.GCODE_STEPPER_PRISMATIC);
confp.setDeviceScriptingName(GCODE);
AbstractLink link = lf.getLink(confp);
assertNotEquals(link.getClass(), VirtualGenericPIDDevice.class);// checks to see a real device was created
public void linkFactoryPrismatic(){
if (hasPort) {
LinkFactory lf = new LinkFactory();
LinkConfiguration confp = new LinkConfiguration();
confp.setType(LinkType.GCODE_STEPPER_PRISMATIC);
confp.setDeviceScriptingName(GCODE);
AbstractLink link = lf.getLink(confp);
assertEquals(link.getClass(), VirtualGenericPIDDevice.class);// checks to see a real device was created
}
}

@Test
Expand All @@ -88,13 +90,13 @@ public void G1() {
else {
fail("No response");
}
response = device.runLine("G0 X100 Y100 Z100 E100 F22000");
response = device.runLine("G1 X10 Y10 Z10 E10 F3000");
if (response.length() > 0)
System.out.println("Gcode line run: " + response);
else {
fail("No response");
}
response = device.runLine("G0 X0 Y0 Z0 E0 F22000");
response = device.runLine("G1 X0 Y0 Z0 E0 F3000");
if (response.length() > 0)
System.out.println("Gcode line run: " + response);
else {
Expand Down

0 comments on commit 9d6b256

Please sign in to comment.