Hey again,
Ive been trying to write this java code for days now and I just cannot get it working. Can anyone help please?
I want to be able to enter a number in java and for the servo to move accordingly. I have the arduino coded to accept a 3 integers followed by any 4th key in order to set an angle. for example 090a is 90 degrees 170k is 170 degrees. The 'a' and 'k' could be any key as its effectively an execute command.
I have the RXTX library and have code that successfully communicates with it:
/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package serialswitch;
/**
*
-
@author
*/
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Enumeration;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class SerialSwitch implements ActionListener, Runnable {
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyUSB0", // Linux
"COM3", // Windows
};
private static final int TIME_OUT = 2000;
private static final int DATA_RATE = 57600;
private SerialPort serialPort;
private OutputStream output;
private ByteArrayOutputStream bout;
private OutputStreamWriter writer;
private boolean on;
public SerialSwitch() {
bout = new ByteArrayOutputStream();
writer = new OutputStreamWriter(bout);
}
@SuppressWarnings("CallToThreadDumpStack")
public void initialize() {
CommPortIdentifier portId = findPortId();
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
serialPort = (SerialPort)portId.open(this.getClass().getName(), TIME_OUT);
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
output = serialPort.getOutputStream();
serialPort.notifyOnDataAvailable(true);
} catch (PortInUseException
| UnsupportedCommOperationException
| IOException e) {
e.printStackTrace();
}
}
private CommPortIdentifier findPortId() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
while (portEnum.hasMoreElements()) {
CommPortIdentifier currPortId = (CommPortIdentifier)portEnum.nextElement();
for (String portName : PORT_NAMES) {
if (currPortId.getName().equals(portName)) {
portId = currPortId;
break;
}
}
}
return portId;
}
public synchronized void close() {
if (serialPort == null) {
return;
}
serialPort.close();
}
private void createAndShowGui() {
JFrame frame = createFrame();
frame.setVisible(true);
}
private JFrame createFrame() {
JFrame frame = new JFrame("SerialSwitch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(createPane());
frame.pack();
return frame;
}
private JPanel createPane() {
JPanel pane = new JPanel();
pane.add(createButton());
return pane;
}
private JButton createButton() {
JButton button = new JButton("ON");
button.addActionListener(this);
return button;
}
@Override
public void actionPerformed(ActionEvent event) {
JButton button = (JButton)event.getSource();
onButton(button);
}
@SuppressWarnings("CallToThreadDumpStack")
private void onButton(JButton button) {
try {
String command = getCommand();
writer.write(command, 0, command.length());
writer.flush();
bout.writeTo(output);
button.setText(getButtonText());
on = !on;
bout.reset();
} catch (IOException e) {
e.printStackTrace();
}
}
private String getCommand() {
return on ? "OFF\n" : "ON\n";
}
private String getButtonText() {
return on ? "ON" : "OFF";
}
@Override
public void run() {
createAndShowGui();
}
public static void main(String[] args) throws Exception {
Enumeration port_list = CommPortIdentifier.getPortIdentifiers();
while (port_list.hasMoreElements())
{
CommPortIdentifier port_id = (CommPortIdentifier)port_list.nextElement();
if (port_id.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
System.out.println ("Serial port: " + port_id.getName());
}
else if (port_id.getPortType() == CommPortIdentifier.PORT_PARALLEL)
{
System.out.println ("Parallel port: " + port_id.getName());
}
else
System.out.println ("Other port: " + port_id.getName());
}
SerialSwitch main = new SerialSwitch();
main.initialize();
SwingUtilities.invokeLater(main);
System.out.println("Started");
}
}
This gives the ouput:
Serial port: COM3
Started
It also opens a window in which a on off switch can be pressed.
I now either a input textbox or a slider to change the angle from java but i have no idea how. Is there a send data command? i have search forums and read textbooks but i think maybe its out of my depth currently and for this purpose i simply dont have the time required to learn the foundations.
could someone please help cheers!
Ste