从Java中的串口读取文件

我是java技术的初学者,我必须从端口读取文件。 首先,我将“FLASH”写入输出流,然后我将从目标设备获得响应作为“FLASH_OK”,在获得FLASH_OK作为响应后再次我必须写出我想要的文件名,但问题是它不写文件名为outputstream,下面是我的代码。 请帮帮我。

package writeToPort; import java.awt.Toolkit; import java.io.*; import java.util.*; import javax.comm.*; import javax.swing.JOptionPane; import constants.Constants; public class Flashwriter implements SerialPortEventListener { Enumeration portList; CommPortIdentifier portId; String messageString = "\r\nFLASH\r\n"; SerialPort serialPort; OutputStream outputStream; InputStream inputStream; Thread readThread; String one, two; String test = "ONLINE"; String[] dispArray = new String[1]; int i = 0; byte[] readBufferArray; int numBytes; String response; FileOutputStream out; final int FLASH = 1, FILENAME = 2; int number; File winFile; public static void main(String[] args) throws IOException { Flashwriter sm = new Flashwriter(); sm.FlashWriteMethod(); } public void FlashWriteMethod() throws IOException { portList = CommPortIdentifier.getPortIdentifiers(); winFile = new File("D:\\testing\\out.FLS"); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM2")) { try { serialPort = (SerialPort) portId.open("SimpleWriteApp", 1000); } catch (PortInUseException e) { } try { inputStream = serialPort.getInputStream(); System.out.println(" Input Stream... " + inputStream); } catch (IOException e) { System.out.println("IO Exception"); } try { serialPort.addEventListener(this); } catch (TooManyListenersException e) { System.out.println("Tooo many Listener exception"); } serialPort.notifyOnDataAvailable(true); try { outputStream = serialPort.getOutputStream(); inputStream = serialPort.getInputStream(); } catch (IOException e) { } try { serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); serialPort .setFlowControlMode(SerialPort.FLOWCONTROL_NONE); number = FLASH; sendRequest(number); } catch (UnsupportedCommOperationException e) { } } } } } public void serialEvent(SerialPortEvent event) { SerialPort port = (SerialPort) event.getSource(); switch (event.getEventType()) { case SerialPortEvent.DATA_AVAILABLE: try { if (inputStream.available() > 0) { numBytes = inputStream.available(); readBufferArray = new byte[numBytes]; int readBytes = inputStream.read(readBufferArray); one = new String(readBufferArray); System.out.println("readBytes " + one); } if (one.indexOf("FLASH_") > -1 & !(one.indexOf("FLASH_F") > -1)) { System.out.println("got message"); response = "FLASH_OK"; number = FILENAME; sendRequest(number); } out = new FileOutputStream(winFile, true); out.write(readBufferArray); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } readBufferArray = null; // break; } } public void sendRequest(int num) { switch (num) { case FLASH: try { outputStream.write(messageString.getBytes()); outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } break; case FILENAME: try { outputStream.write("\r\n26-02-08.FLS\r\n".getBytes()); outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } break; } } } 

您是否使用串行端口仿真器软件进行了测试?

当我为大学做这种应用程序时,我们的教授告诉我们构建应用程序并使用模拟器进行测试,因为它更便宜且不易出错。

在搜索谷歌时,你可以找到一些软件来做到这一点。 我不记得我们当时使用的那个,但它运作得很好。

我的意思是这样的产品: Eterlogic – 虚拟串口仿真器 ,但这仅仅是一个例子(我没有测试过这个软件,我只是用谷歌搜索)

您错误地认为将始终收到完整的消息。 相反,当触发串行事件时,只有部分消息可用。 例如,您可能会收到一个事件并读取“FLAS”,后续事件将显示“H_OK”。 您需要使代码适应以下内容:

 // member variables byte [] receiveBuffer = new byte[BUFFER_LENGTH]; int receiveIndex = 0; // Receive code receiveIndex += inputStream.read(receiveBuffer, receiveIndex, BUFFER_LENGTH - receiveIndex); 

抱歉,我无法帮助您使用Java代码,但您确定数据“FLASH”实际上是在串口上发送的吗? 当我遇到这种问题时,我通常使用示波器查看串行端口上的TX引脚,并检查是否可以“看到”正在发送的数据(数据突发将是短暂的,但您将能够看到它)。 如果您可以看到它使用示波器查看串行端口的RX引脚,看看您是否可以看到实际发送的“FLASH_OK”响应。

十分之九的问题不是软件,而是硬件问题,这通常是由于握手引脚连接错误造成的。

祝你好运。