Java Development Issues
Java Development Issues
I am trying to start a development project with Java. The problem is that given the code you've supplied, I am unable to connect to the com2 port. My app throws an exception becuase no such port exists. Anyone have a clue? Thanks
hi bbca,
Thanks for posting on the forum.
Unfortunately, I'm not very knowledgable with Java, but I have found another example for you. Also, you may need to makes sure the com port isn't in use when you run the application. Usually when the message "unable to connect to the com port" comes up, the com port is in use.
/**
* LCDControl.java
*
* Author: Owen Langman (langman@cs.wisc.edu)
*
* Description:
* Basic commands to control a Matrix Orbital (we love MO!)
* LCD screen.
*
* NOTE: This requires the javax.comm package.
* Check java.sun.com
**/
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
* Collection of methods to control a Matrix Orbital LCD.
*
* <p> Bugs: None known.
*
* @author Owen Langman (langman@cs.wisc.edu)
*
**/
public class LCDControl {
private CommPortIdentifier portId;
private SerialPort serialPort;
private OutputStream outputStream;
public static final char COMMAND_START = (char)254;
public static final char COMMAND_NULL = (char)0;
public static final char COMMAND_ETX = (char)3;
public static final char COMMAND_DEL = (char)253;
/**
* Constructor. Uses COM1 as the default port.
*
**/
public LCDControl() {
this(1); //uses com1 with default const.
}
/**
* Constructor. Initializes COM port and resets the LCD.
*
* @param port Interger specifying the COM port to output to.
**/
public LCDControl(int port) {
String portname = new String("COM");
portname += port;
try {
portId = CommPortIdentifier.getPortIdentifier(portname);
} catch (NoSuchPortException ex) {
System.out.println(portname + " does not exist.");
return;
}
try {
serialPort = (SerialPort)portId.open("LCDControl",2000);
} catch (PortInUseException ex) {
System.out.println("Could not open " + portname);
return;
}
try {
serialPort.setSerialPortParams(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException ex) {
System.out.println("Could not set serial stuff");
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
resetLCD();
}
/**
* Resets the lcd in every way possible.
*/
public void resetLCD() {
String resetString;
resetString = new String(COMMAND_START + "B" + COMMAND_NULL +
COMMAND_START + "~" + COMMAND_NULL + COMMAND_START +
"T" + COMMAND_START + "Y" + COMMAND_ETX +
COMMAND_START + "X");
try {
outputStream.write(resetString.getBytes());
} catch (Exception ex) {
System.out.println("Could not reset lcd");
}
}
/**
* Clears the LCD screen.
*/
public void clear() {
String clearString;
clearString = new String(COMMAND_START + "X");
try {
outputStream.write(clearString.getBytes());
} catch (Exception ex) {
System.out.println("Could not clear lcd");
}
}
/**
* Writes a string to the screen at the cursor's current
* position.
*
* @param str String to write to the screen.
*/
public void display_write(String str) {
try {
outputStream.write(str.getBytes());
} catch (Exception ex) {
System.out.println("Could not write to lcd");
}
}
/**
* Changes position of the lcd's cursor.
*
* @param x Horizontal index to move the cursor to
* @param y Line number to move the cursor to
*/
public void display_position(int x, int y) {
String displayString;
displayString = new String(COMMAND_START + "G");
try {
outputStream.write(displayString.getBytes());
outputStream.write(y);
outputStream.write(x);
} catch (Exception ex) {
System.out.println("Could not change cursor position on lcd");
}
}
/**
* Full clear then shutdown of the LCD
*/
public void display_close() {
String clearString, closeString;
clearString = new String(COMMAND_START + "X");
closeString = new String(COMMAND_START + "F");
try {
outputStream.write(clearString.getBytes());
outputStream.write(closeString.getBytes());
} catch (Exception ex) {
System.out.println("Could not close lcd");
}
}
}
Best Regards,
Thanks for posting on the forum.
Unfortunately, I'm not very knowledgable with Java, but I have found another example for you. Also, you may need to makes sure the com port isn't in use when you run the application. Usually when the message "unable to connect to the com port" comes up, the com port is in use.
/**
* LCDControl.java
*
* Author: Owen Langman (langman@cs.wisc.edu)
*
* Description:
* Basic commands to control a Matrix Orbital (we love MO!)
* LCD screen.
*
* NOTE: This requires the javax.comm package.
* Check java.sun.com
**/
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
* Collection of methods to control a Matrix Orbital LCD.
*
* <p> Bugs: None known.
*
* @author Owen Langman (langman@cs.wisc.edu)
*
**/
public class LCDControl {
private CommPortIdentifier portId;
private SerialPort serialPort;
private OutputStream outputStream;
public static final char COMMAND_START = (char)254;
public static final char COMMAND_NULL = (char)0;
public static final char COMMAND_ETX = (char)3;
public static final char COMMAND_DEL = (char)253;
/**
* Constructor. Uses COM1 as the default port.
*
**/
public LCDControl() {
this(1); //uses com1 with default const.
}
/**
* Constructor. Initializes COM port and resets the LCD.
*
* @param port Interger specifying the COM port to output to.
**/
public LCDControl(int port) {
String portname = new String("COM");
portname += port;
try {
portId = CommPortIdentifier.getPortIdentifier(portname);
} catch (NoSuchPortException ex) {
System.out.println(portname + " does not exist.");
return;
}
try {
serialPort = (SerialPort)portId.open("LCDControl",2000);
} catch (PortInUseException ex) {
System.out.println("Could not open " + portname);
return;
}
try {
serialPort.setSerialPortParams(19200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException ex) {
System.out.println("Could not set serial stuff");
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {}
resetLCD();
}
/**
* Resets the lcd in every way possible.
*/
public void resetLCD() {
String resetString;
resetString = new String(COMMAND_START + "B" + COMMAND_NULL +
COMMAND_START + "~" + COMMAND_NULL + COMMAND_START +
"T" + COMMAND_START + "Y" + COMMAND_ETX +
COMMAND_START + "X");
try {
outputStream.write(resetString.getBytes());
} catch (Exception ex) {
System.out.println("Could not reset lcd");
}
}
/**
* Clears the LCD screen.
*/
public void clear() {
String clearString;
clearString = new String(COMMAND_START + "X");
try {
outputStream.write(clearString.getBytes());
} catch (Exception ex) {
System.out.println("Could not clear lcd");
}
}
/**
* Writes a string to the screen at the cursor's current
* position.
*
* @param str String to write to the screen.
*/
public void display_write(String str) {
try {
outputStream.write(str.getBytes());
} catch (Exception ex) {
System.out.println("Could not write to lcd");
}
}
/**
* Changes position of the lcd's cursor.
*
* @param x Horizontal index to move the cursor to
* @param y Line number to move the cursor to
*/
public void display_position(int x, int y) {
String displayString;
displayString = new String(COMMAND_START + "G");
try {
outputStream.write(displayString.getBytes());
outputStream.write(y);
outputStream.write(x);
} catch (Exception ex) {
System.out.println("Could not change cursor position on lcd");
}
}
/**
* Full clear then shutdown of the LCD
*/
public void display_close() {
String clearString, closeString;
clearString = new String(COMMAND_START + "X");
closeString = new String(COMMAND_START + "F");
try {
outputStream.write(clearString.getBytes());
outputStream.write(closeString.getBytes());
} catch (Exception ex) {
System.out.println("Could not close lcd");
}
}
}
Best Regards,
bbcac,
You can preload all the graphics into an application called mogd. Mogd should have come with the ecd that came with your display. You can also download mogd.exe at http://www.matrixorbital.ca/software/software_glk/
Once you have predownloaded the fonts and graphics, you just need to send commands to the com port that are found in the manual. The manual can be found at http://www.matrixorbital.ca/manuals/GLK_series/
If you have anymore questions or concerns, please feel free to post them.
Best Regards,
You can preload all the graphics into an application called mogd. Mogd should have come with the ecd that came with your display. You can also download mogd.exe at http://www.matrixorbital.ca/software/software_glk/
Once you have predownloaded the fonts and graphics, you just need to send commands to the com port that are found in the manual. The manual can be found at http://www.matrixorbital.ca/manuals/GLK_series/
If you have anymore questions or concerns, please feel free to post them.
Best Regards,