我已经使用这个示例代码在客户显示器上写字,但没有任何内容:
public class Display2 {
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static boolean outputBufferEmptyFlag = false;
@SuppressWarnings("SleepWhileInLoop")
public void StartDisplay() {
boolean portFound = false;
String defaultPort = "COM3";
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals(defaultPort)) {
System.out.println("Found port " + defaultPort);
portFound = true;
try {
System.out.println("current owner : "+portId.getCurrentOwner());
serialPort = (SerialPort) portId.open("SimpleWrite", 2000);
System.out.println("current owner : "+portId.getCurrentOwner());
} catch (PortInUseException e) {
System.out.println("Port is offline now.");
e.printStackTrace();
continue;
}
try {
outputStream = serialPort.getOutputStream();
} catch (IOException e) {
}
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
System.out.println("Display is online now");
} catch (UnsupportedCommOperationException e) {
}
try {
serialPort.notifyOnOutputEmpty(true);
} catch (Exception e) {
System.out.println("Error setting event notification");
System.out.println(e.toString());
System.exit(-1);
}
try {
Thread.sleep(2000); // Be sure data is xferred before closing
} catch (Exception e) {}
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}
}
public void ClearDisplay(){
try{
outputStream.write(ESCPOS.SELECT_DISPLAY);
outputStream.write(ESCPOS.VISOR_CLEAR);
outputStream.write(ESCPOS.VISOR_HOME);
outputStream.flush();
}
catch(IOException e){
}
}
public void PrintFirstLine(String text){
try{
ClearDisplay();
if(text.length()>20) //Display can hold only 20 characters per line.Most of displays have 2 lines.
text=text.substring(0,20);
outputStream.write(text.getBytes());
outputStream.flush();
}
catch(IOException r){
}
}
public void PrintSecondLine(String text){
try{
outputStream.write(ESCPOS.SELECT_DISPLAY);
outputStream.write(ESCPOS.Down_Line);
outputStream.write(ESCPOS.Left_Line);
if(text.length()>20)
text=text.substring(0,20);
outputStream.write(text.getBytes());
outputStream.flush();
}
catch(IOException y){
System.out.println("Failed to print second line because of :"+y);
}
}
public void ShowGreeting(){
String text1="*****Thank You******"; // 20 characters
String text2=" Come Again "; //20 characters
ClearDisplay();
PrintFirstLine(text1);
PrintSecondLine(text2);
try {
Thread.sleep(5000); //Greeting will dislpay 5 seconds.
} catch (InterruptedException ex) {
Logger.getLogger(Display2.class.getName()).log(Level.SEVERE, null, ex);
}
ClearDisplay();
}
public void init(){
try{
outputStream.write(ESCPOS.Anim);
}
catch(IOException i){}
}
public void close(){
serialPort.close();
System.exit(1);
}
public static void main(String[] args) {
////////////////////// THIS IS HOW USE THIS CLASS //////////////////////////
Display2 display=new Display2();
display.StartDisplay(); //optimal choice is start when system start.
display.PrintFirstLine(" Java Open Source ");
display.PrintSecondLine(" Say Hello TO World ");
try {
Thread.sleep(5000); //wait 5 seconds.otherwise unable to see above outputs.
} catch (InterruptedException ex) {
Logger.getLogger(Display2.class.getName()).log(Level.SEVERE, null, ex);
}
display.ShowGreeting();
display.close(); //optimal choice is close when exit from system.
////////////////////// JOIN TO SHARE KNOWLADGE ////////////////////////////////
}
}
我有一个带有嵌入式客户显示器的wintec pos 我有日志
Found PORT 3
current owner null
current owner SimpleWrite
Display is online now
我已将rxtxParallel.dll和rxtxSerial.dll添加到java bin文件夹,将RXTXcomm.jar添加到lib / ext文件夹
无论如何要写在上面