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