问题从Java写入串口[重复]

时间:2022-12-15 15:10:44

Duplicate of Implementation of Xmodem Protocol in Java

在Java中实现Xmodem协议的重复


I've got to implement the xmodem protocol to receive a file from a target device. For that, I have to request the file, then for every 128-byte packet received, I have to send an acknowledgment. My problem is when I open an outputstream to request the file, it will write but after that I can't write again to the outputstream. What is the problem I'm not getting?

我必须实现xmodem协议以从目标设​​备接收文件。为此,我必须请求文件,然后对于收到的每个128字节数据包,我必须发送一个确认。我的问题是当我打开一个输出流来请求文件时,它会写,但之后我不能再写入输出流。我没有得到什么问题?

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 Runnable, SerialPortEventListener {
    Enumeration portList;
    CommPortIdentifier portId;
    String messageString = "\r\nFLASH\r\n";
    SerialPort serialPort;
    OutputStream outputStream,outputStream2;
    InputStream inputStream;
    //Thread readThread;
    String one, two;
    String test = "ONLINE";
    String[] dispArray = new String[1];
    int i = 0;

    Thread readThread;

    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")) {
                    // if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort) portId.open("SimpleWriteApp",
                                1000);
                    } catch (PortInUseException e) {
                    }

                    try {
                        outputStream = serialPort.getOutputStream();
                        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 {
                        serialPort.setSerialPortParams(9600,
                                SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
                                SerialPort.PARITY_NONE);
                        serialPort
                                .setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
                        // serialPort.disableReceiveTimeout();

                        // outputStream.write(messageString.getBytes());

                        // sendRequest("/r/n26-02-08.FLS/r/n");
                        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 {
                while (inputStream.available() > 0) {
                    numBytes = inputStream.available();
                    readBufferArray = new byte[numBytes];
                    // int readtheBytes = (int) inputStream.skip(2);
                    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";
                    // JOptionPane.showMessageDialog(null,
                    // "ONLINE",
                    // "Online Dump",
                    // JOptionPane.INFORMATION_MESSAGE);
                    // Toolkit.getDefaultToolkit().beep();
                    // outputStream.write("\r\nONLINEr\n".getBytes());
                    // outputStream.flush();
                    // outputStream.write("/r/n26-02-08.FLS/r/n".getBytes());

                    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;
        }

        // try {
        // int c;
        // while((c = inputStream.read()) != -1) {
        // out.write(c);
        // }
        // } catch (IOException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        // // readBufferArray=null;
        // break;
        // }
        // if (inputStream != null)
        // try {
        // inputStream.close();
        // if (port != null) port.close();
        // } catch (IOException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        //      
        //
        readThread = new Thread(this);
        readThread.start();
    }

    public void run() {
        try {
            System.out.println("In run() function ");
            Thread.sleep(1000);

        } catch (InterruptedException e) {
            System.out.println("Interrupted Exception in run() method");
        }
    }

    public void dispPacket(String packet) {
        if (response == "FLASH_OK") {
            System.out.println("disppacket " + packet);
        } else {
            System.out.println("No resust");
        }
    }

    public void sendRequest(int num) {
        switch (num) {
        case FLASH:
            try {
            //  outputStream = serialPort.getOutputStream();
                outputStream.write("AT".getBytes());
            //  outputStream.write("\r\n26-02-08.FLS\r\n".getBytes());

                System.out.println("Flash switch");
             //   outputStream.close();
                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        case FILENAME:
            try {
                //outputStream =(serialPort.getOutputStream());
                outputStream.write("\r\nSUNSHINE\\06-03-09.FLS\r\n".getBytes());

                System.out.println("File name");
                outputStream.flush();

            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }
}

1 个解决方案

#1


Perhaps the streams are blocking.

也许流是阻塞的。

Java nio has channels that do not block. Try using one of those.

Java nio具有不阻塞的通道。尝试使用其中之一。

Here's a sample of reading a file with nio. I'm not sure if the same applies for you or not.

这是使用nio读取文件的示例。我不确定这是否适用于你。

I hope it helps.

我希望它有所帮助。

#1


Perhaps the streams are blocking.

也许流是阻塞的。

Java nio has channels that do not block. Try using one of those.

Java nio具有不阻塞的通道。尝试使用其中之一。

Here's a sample of reading a file with nio. I'm not sure if the same applies for you or not.

这是使用nio读取文件的示例。我不确定这是否适用于你。

I hope it helps.

我希望它有所帮助。