简体   繁体   English

尝试从同一com口进行写入和读取,写入成功,但读取失败。 吗

[英]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. 我试图从com端口获取一些数据,就像我在上面写数据一样,但是事实证明它很难阅读。 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. 这是一个JButton,方法名称和参数已由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. 代码首先从输入面板以字符串形式获取文本,以com端口名称发送该文本,连接至该端口,并由此打开输入/输出流,然后将从输入面板获取的字符串写入到OutputStream。 Then, I create a new string, named out_Text and use read() method to read the data from port. 然后,我创建一个名为out_Text的新字符串,并使用read()方法从端口读取数据。

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. 在我发布的第一个方法使用read()将其收敛为可以打印为字节数组的字符串之后,我最终仅得到一个零字节。

[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. 我什至监视了我所写的内容,并且可以看到,当我使用该程序将数据写入com端口时,收到的数据增加了。

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. 经过几次尝试,我尝试使用jSSC进行相同的操作,并设法再次写入。 However, I was still not able to read. 但是,我仍然无法阅读。 Then I noticed, that I was creating pairs 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 rxtx libraries, but because jSSC 's documents are descriptive, and because it's easy to use, I switched to it, and the way I did worked. 我不确定同一想法对rxtx库是否有效,但是因为jSSC的文档具有描述性,并且因为它易于使用,所以我切换到它以及工作方式。 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. PreStep >启动此程序时,我的COM6和COM7完好无损。

Step1 > Opened both ports via jSSC's methods. 步骤1 >通过jSSC的方法打开两个端口。 I constructed them as SerialPort, and then set their parameters (BaudRate, DataBits, etc.). 我将它们构造为SerialPort,然后设置其参数(BaudRate,DataBits等)。

Step2 > I started to take inputs from console, and write them to the COM6. 步骤2 >我开始从控制台获取输入,并将其写入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). 步骤3 >每次我按“ Enter”键发送输入时,我都会从COM7对象调用一个读取方法,它将向控制台发送一个字节数组(通过Arrays.toString(byte [] array)方法)。

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. 如有任何疑问,请将其张贴在此评论下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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