3

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

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;

    }
}

}
4

3 回答 3

1

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

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

在搜索谷歌时,您可以找到一些这样做的软件。我不记得我们当时使用的确切型号,但效果很好。

我的意思是这样的产品:Eterlogic - Virtual Serial Ports Emulator,但这只是一个例子(我还没有测试过这个软件,我只是用谷歌搜索了它)

于 2009-02-18T11:43:38.537 回答
1

您错误地假设总是会收到完整的消息。相反,当触发串行事件时,可能只有部分消息可用。例如,您可能会收到一个事件并读取“FLAS”,随后的事件将给出“H_OK”。您需要将代码调整为以下内容:

// member variables
byte [] receiveBuffer = new byte[BUFFER_LENGTH];
int receiveIndex = 0;

// Receive code

receiveIndex +=
   inputStream.read(receiveBuffer, receiveIndex, BUFFER_LENGTH - receiveIndex);
于 2009-06-02T06:13:59.043 回答
0

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

十分之九的问题不是软件问题,而是硬件问题,通常是由于握手引脚连接不正确。

祝你好运。

于 2009-02-18T11:19:29.797 回答