Как сделать так, чтобы Java rxtx читал со скоростью 115200? - PullRequest
0 голосов
/ 20 апреля 2019

Я установил скорость передачи данных 115200 в моем файле чтения Java, и он просто не будет читать с такой скоростью.Я использую библиотеку rxtx.Он читает намного медленнее, чем 115200, и не перехватывает все данные, которые я отправляю.Я пытаюсь отправить непрерывные данные на Java со скоростью 115200. Я хочу читать данные по 1 байту за раз.Код, с которым я столкнулся, находится ниже.Надеюсь, кто-то может помочь

Я просмотрел некоторые форумы по поиску и устранению неисправностей без удачи.Я также включил свои функции записи, которые работают отлично.

import gnu.io.*;
import java.awt.AWTException;
import java.util.*;
import java.io.*;
import java.util.logging.*;

public class SimpleWreadWrite implements Runnable, SerialPortEventListener{
    static CommPortIdentifier portId;
    static Enumeration portList;
    static SerialPort serialPort;
    static OutputStream outputStream;
    static String inputbuffer;//change to String for other way
    static String previnputbuffer;//also change to string for array way
    Keyboard keyboard;
    InputStream inputStream;
    Thread readThread;
    boolean check = true;
    static int xvalue, yvalue;
    public static void main(String[] args) throws InterruptedException {
        SimpleWreadWrite writer = new SimpleWreadWrite();
        Thread thread1 = new Thread();
        writer.reading();
        writer.connectstring("Hello");
        writer.connectint(5);
        writer.connectchar('c');
    }
    public SimpleWreadWrite(){
    }

    public void reading() {
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM12")) {
            //                if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
                    } catch (PortInUseException e) {System.out.println(e);}
                    try {
                        inputStream = serialPort.getInputStream();
                    } catch (IOException e) {System.out.println(e);}
                    try {
                        serialPort.addEventListener(this);
                    } catch (TooManyListenersException e) {System.out.println(e);}
                        serialPort.notifyOnDataAvailable(true);
                    try {
                        serialPort.setSerialPortParams(115200,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {System.out.println(e);}
                    readThread = new Thread(this);
                    readThread.start();
                }
            }
        }
    }

    public void run() {
    }

    public void serialEvent(SerialPortEvent event) {
        switch(event.getEventType()) {
        case SerialPortEvent.BI:
        case SerialPortEvent.OE:
        case SerialPortEvent.FE:
        case SerialPortEvent.PE:
        case SerialPortEvent.CD:
        case SerialPortEvent.CTS:
        case SerialPortEvent.DSR:
        case SerialPortEvent.RI:
        case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
            break;
        case SerialPortEvent.DATA_AVAILABLE:
            byte[] readBuffer = new byte[1];
            byte prevreadbuffer = '0';
            try {
                while (inputStream.available() > 0) {
                    int numBytes = inputStream.read(readBuffer);
                }
                inputbuffer = new String(readBuffer);
                System.out.println(inputbuffer);
            } catch (IOException e) {System.out.println(e);}
            break;
        }
    }
        public void connectstring(String writestring){
                portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM12")) {
                //if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(115200,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try {
                        outputStream.write(writestring.getBytes());
                    } catch (IOException e) {}
                }
            }
        }
    }

    public void connectint(int writeint){
        String convertint = Integer.toString(writeint);
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM12")) {
                //if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(115200,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try {
                        outputStream.write(convertint.getBytes());
                    } catch (IOException e) {}
                }
            }
        }
    }

    public void connectchar(char writechar){
                portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM12")) {
                //if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(115200,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try {
                        outputStream.write(writechar);
                    } catch (IOException e) {}
                }
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...