Welcome! Log In Create A New Profile

Advanced

Sending single Gcode

Posted by 0pascal0 
Sending single Gcode
April 11, 2013 07:14AM
hello guys,

My name is Pascal and Im 20 years old. For my internship I have prove some concepts. The following concept is to prove the connection between aan CNC machine and my computer via USB serial port.

The code:
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import gnu.io.*;


public class EnkeleGcode implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;


static String messageString = "G0 X20 Y20";

static OutputStream outputStream;

InputStream inputStream;
SerialPort serialPort;
Thread readThread;

public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();

while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM9")) {
//if (portId.getName().equals("/dev/term/a")) {
EnkeleGcode reader = new EnkeleGcode();
}
}
}
}

public EnkeleGcode() {
try {
serialPort = (SerialPort) portId.open("Mantis", 23);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(57600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}

public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];

try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {}
try {

outputStream.write(messageString.getBytes());
System.out.println(messageString.getBytes());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

break;
}
}
}


The code opens a USB port. It gives it a name and waits for a response. I already have a CNC mantis wich got a interfaceboard that understands GCode. Via the arduino seriel monitor it is possible to send a single Gcode to the board and the CNC moves to that position. The only thing whats holding me back is when I use my program and send the message string to the Mantis it doesnt do anything.

Does anyone have an idee of what it could possibly be?

Thanks in advance
Greetings
Pascal
Re: Sending single Gcode
April 22, 2013 01:42PM
Have you made any progress on this problem yet?

You may want to check your end of line command. The machine may be recieving the commands but not exicuting it since it doesn't know if the end of the command has been reached yet or not. Depending on the machine's controller, it may be expecting an ASCII 10 (LF) or ASCII 13 + ASCII 10 (CR + LF).

You may need to change your line to append \n to the end of the string:

static String messageString = "G0 X20 Y20\n";

I'm not very strong in Java, so this is probably wrong but hopefully points you in the right direction.
Re: Sending single Gcode
September 26, 2013 08:16AM
As Partier_SP suggests, you need a line ending. The write method does not add one. Your firmware should probably work with \n added as suggested or if not \r\n (CR + LF).

You may also find that you don't reliably get serial port events for input buffer empty on some OS combinations. You may need an option to run it on a timer and check for overflow.

I checked the G code reference on the wiki here and oddly it doesn't make mention of the requirement for termination in the protocol. Every command has to be terminated by a line ending, which may be machine specific but mostly seems to be LF.
Sorry, only registered users may post in this forum.

Click here to login