简体   繁体   中英

Trying to write and read from the same com port, successful at writing, but failing at reading. Wat do?

I'm trying to get some data from the com port, the same one I'm writing data on, but it proves hard to read. I managed to find a simple code piece to read it, but now, I only read zeros. What could be the cause?

I'm sending my code below, with explanation of their intended usages.

private void ReadFromComPortActionPerformed(java.awt.event.ActionEvent evt) {                                                
    try {
        String text = EmulatorInput.getText();
        sendData(text, "COM4");
        String out_Text = Arrays.toString(read());
        EmulatorOutput.setText(out_Text);
        System.out.println(out_Text);
    } catch (IOException ex) {
            Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
    }
}  

This is a JButton, method name and parameter has been created by NetBeans. Code firsts gets a text from the input panel as a string, sends it with a com port name, connects to that port, and opens input/output streams by it, then writes the string it took from the input panel to the OutputStream. Then, I create a new string, named out_Text and use read() method to read the data from port.

Here is the read method;

private byte[] read() throws IOException {
    byte[] buffer = new byte[16];
    int total = 0, read = 0;
    while (total <= 16 && (read = input.read(buffer, total, 16-total)) > 0)  {
        total += read;
    }
    return buffer;
}

After the first method I posted uses read() to converge it into a string that I can print as a byte array, I end up with only a byte of zeros.

[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

I'm very confused by this, because I'm sure I write to the port. I even monitored what I write, and can see that as I use the program to write data to the com port, the data it received increases.

If there is something that is unclear or if you need more of the code, please contact via comments.

Thank you kindly.

After several tries, I've tried the same operation with jSSC , and managed to write again. However, I was still not able to read. Then I noticed, that I was creating of com ports. 的COM端口。 So, simply, I started to write to one port, and received the data from the other port of the pair. It worked like a charm. I'm not sure if the same idea is valid for libraries, but because 's documents are descriptive, and because it's easy to use, I switched to it, and the way I did worked. 库是否有效,但是因为的文档具有描述性,并且因为它易于使用,所以我切换到它以及工作方式。 You can find a detailed explanation of how I did below, without the code.

PreStep > I had COM6 and COM7 intact when I started this.

Step1 > Opened both ports via jSSC's methods. I constructed them as SerialPort, and then set their parameters (BaudRate, DataBits, etc.).

Step2 > I started to take inputs from console, and write them to the COM6. When I monitored the port, and the bytes in it, it was changing each time I sent an input.

Step3 > Each time I would press "Enter" to send the input, I'd invoke a reading method from COM7 object, and it would send a byte array to the console (Via Arrays.toString(byte[] array) method).

I don't know why I can't read from the same port as I write, but alas, my problem is solved. Thanks for your time to read this answer. If you have any questions, please post them under this one as a comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM