Java的简单串行连接器

时间:2013-12-05 02:30:00

标签: java serial-port

我正在使用Java Simple Serial Connector与数据卡压花机进行通信。以下是我的程序,它基于作者提供的示例。这是我们遇到的问题。如果我们将一个标签发送到压花器,则没有任何反应。如果我们向压花器发送第二个标签,则打印第一个标签。如果我们发送第三个标签,则打印第二个标签,依此类推。该程序的供应商提供Turbo“C”中的代码。在Turbo“C”程序中,“C”程序在传输结束时执行这两个命令。

clear_serial_queue()
restore_serialint()

在我的Java程序中,我使用的是serialPort.closePort()

还有什么我可以尝试强制压印机立即打印而不是下一个请求吗?我们已经为此工作了好几天没有取得任何成功。

Java程序:

/**
* Construct the class from the name of a serial port; e.g., COM1
*
* @param  Name of the com port to use.
*/
public SerialTester(String name) throws Exception {

   serialPort = new SerialPort(name);
   try {
        serialPort.openPort();//Open serial port
        serialPort.setParams(SerialPort.BAUDRATE_9600, 
                             SerialPort.DATABITS_8,
                             SerialPort.STOPBITS_1,
                             SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0);

   }
   catch (SerialPortException ex) {
        System.out.println(ex);
    }
}

/**
* This method reads the file and sends its contents to the serial
* port.
*
* @param  fileName  Name of the file containing the data to send.
*
* @return  Number of bytes send.
*/
public int sendFile(String fileName) throws IOException {

   // Read until the end of the buffer is reached.
   int byteCount = 0;
   BufferedReader reader =  new BufferedReader(new FileReader(fileName));
   String str = "";
   String str1 = "";

   try {
      while ((str1 = reader.readLine()) != null) {
         byteCount += str1.length();
         str += str1+"\n";
      }
      reader.close();

      // Delay for one second.
      waitForDevice(1000);

      if (serialPort != null) {
         serialPort.writeBytes(str.getBytes());//Write data to port
         serialPort.closePort();//Close serial port
      }
   }
   catch (SerialPortException ex) {
      System.out.println(ex);
    }   

    return byteCount;
}

/**
 * This method adds the specified number of milliseconds as a delay.
 *
 * @param  Time in milliseconds.
 */
 private void waitForDevice(long delay) {

      // Wait the specified delay time.
      try {
         Thread.sleep(delay);
      } catch(InterruptedException ex) {
         Thread.currentThread().interrupt();
      }

 }

/**
* This is the main routine used for testing purposes.
*
* @param   args[0]  Complete path name of the file to send.
* @param   args[1]  Name of the com port to use.
*/
public static void main(String[] args) throws Exception {

   int count = 0;

   // If the file name is null, then report an error.
   if (args.length == 0) {
      System.out.println("First argument must specified the name of the file to send.");
   }
   else if (args.length == 1) {
      SerialTester tester = new SerialTester();
      count = tester.sendFile(args[0]);
      System.out.println("Total bytes read: "+count);

   }
   else if (args.length == 2) {
      SerialTester tester = new SerialTester("COM1");
      count = tester.sendFile(args[0]);
      System.out.println("Total bytes sent: "+count);
   }

1 个答案:

答案 0 :(得分:0)

尝试添加表单Feed作为最后一个字符。您可能还希望使用StringBuilder来提高效率:

public int sendFile(String fileName) throws IOException {
   // Read until the end of the buffer is reached.
   int byteCount = 0;
   BufferedReader reader =  new BufferedReader(new FileReader(fileName));
   StringBuilder sb = new StringBuilder();

   try {
      String line;
      while ((line = reader.readLine()) != null) {
         byteCount += line.length();
         sb.append(line).append('\n');
      }
      reader.close();
      sb.append('\f');       // <<<<-- The form feed

      // Delay for one second.
      waitForDevice(1000);   // <<<<-- why delay here?  try delaying after writeBytes().

      if (serialPort != null) {
         serialPort.writeBytes(str.getBytes());//Write data to port
         serialPort.closePort();//Close serial port
      }
   }
   catch (SerialPortException ex) {
      System.out.println(ex);
   }   

   return byteCount;
}
顺便说一下,你实际上并没有计算字节数(不重要),因为readLine()可能会丢弃CR / LF对而不仅仅是LF或CR,另外你是在计算字符串长度但是然后添加一个{ {1}}到缓冲区,所以发送到设备的实际字节有点不同。正如我所说,并不重要。