从Java中的串口读取文件 [英] Reading file from serial port in Java

查看:171
本文介绍了从Java中的串口读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm beginner in java technology, I have to read file from port. Frst I'll write "FLASH" to outputstream then I'll get response as a "FLASH_OK" from target device, after getting FLASH_OK as response then again i have to write name of the file which i want,but problem is its not writing file name to outputstream, below is my code. Please help me.

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;

    }
}

}


推荐答案

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

You are erroneously assuming that full messages are always going to be received. Instead, when a serial event is triggered, only part of the message may be available. For example, you may get an event and read "FLAS" and a subsequent event will give "H_OK". You need to adapt your code to something like this:

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

// Receive code

receiveIndex +=
   inputStream.read(receiveBuffer, receiveIndex, BUFFER_LENGTH - receiveIndex);

这篇关于从Java中的串口读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆