ESC / POS打印机Java

时间:2015-08-16 17:15:53

标签: java c printing

我有一个DateCS MP55 cashregister。我想用Java开发一个代码来打印它。文档非常差,我可以使用一些帮助。我已经读过某个地方,我需要向它的端口发送命令,但我不知道什么命令以及我的方式发送它,只是如何获得它使用的端口。到目前为止我发现了什么:

1.1. PrnOpen Opens the serial port and performs initalization of the printer 
Syntax int __stdcall PrnOpen(int port, int speed, bool hardware); 
Parameters port com port number speed com port speed hardware if true, turns on RTS/CTS handshaking, false goes back to XON/OFF 
Return value ERR_OK - printer is ready ERR_TIMEOUT - communication error

所以这看起来像它的C代码。我已经为测试创建了示例代码,但C没有bool类型......:

#include <stdio.h>
#include <stdlib.h>

void helloFromC(){
    printf("Hello from C!");
}
int __stdcall PrnOpen(int port, int speed, bool hardware);

int main(){
 helloFromC();
 return 0;
}

所以没有int __stdcall PrnOpen(int port, int speed, bool hardware);我可以用Java运行代码。 另外,我在Java中调用这个C代码的方式是:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[] args) {
        try {
            String filename = "D:\\eclipse\\workspace\\Testing\\TestFile.exe";
            Runtime rTime = Runtime.getRuntime();
            Process p = rTime.exec(filename);
            BufferedReader inputReader = new BufferedReader(new  InputStreamReader(p.getInputStream()));
            String line = "";
            while((line = inputReader.readLine()) != null){
                System.out.println(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

以下是我与港口建立沟通的方式:

import java.util.*;

import gnu.io.CommPortIdentifier;

/** List all the ports available on the local machine. **/
public class ListPorts
{
public static void main (String args[])
{

Enumeration port_list = CommPortIdentifier.getPortIdentifiers();
System.out.println("START");
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());

}
System.out.println("END");
} // main
} // class PortList

0 个答案:

没有答案