首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >串口双向通信Java通信API

串口双向通信Java通信API
EN

Stack Overflow用户
提问于 2015-02-20 22:18:06
回答 1查看 687关注 0票数 1

大家好,我有以下问题。我有一个扫描仪,我正在用命令寻址它,并接收一些行。到目前为止,我一直在尝试超级终端,它工作得很好。但现在我需要在我的程序中使用这些行,所以我设置了Java Comm API和RXTX (只是因为我不能将它与Comm API一起使用)。

我已经在论坛上读了很多,但我不能把它带到工作中去。

我的意思是只有3个部分。首先,我设置了端口、InputStream和OutputStream,它们工作得很好。

代码语言:javascript
复制
    portList = CommPortIdentifier.getPortIdentifiers();
    int i;
    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM3")) {
           // if (portId.getName().equals("/dev/term/a")) {

    try {
                    serialPort = (SerialPort)
                        portId.open("SimpleWriteApp", 2000);
                } catch (PortInUseException e) {System.out.println(e);}
                try {
                    outputStream = serialPort.getOutputStream();
                    inputStream = serialPort.getInputStream();
                } catch (IOException e) {System.out.println(e);}
                try {
                    serialPort.setSerialPortParams(9600,
                        SerialPort.DATABITS_8,
                        SerialPort.STOPBITS_1,
                        SerialPort.PARITY_NONE);
                    serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN);

                } catch (UnsupportedCommOperationException e) {System.out.println(e);}

然后我想用outputStream.write(“xYZ”.getByteS())发送一个命令;我想这也应该可以。但是我得不到回复。

响应应该看起来像这个02349234235883挪威克朗,但它只是卡住了。代码如下所示

代码语言:javascript
复制
                            try {               
                    System.out.println(messageString);
                    outputStream.write(messageString.getBytes());
                    BufferedReader portReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
                    String line = portReader.readLine();
                    System.out.println(line);


                    if (inputStream != null) inputStream.close();
                    if (outputStream != null) outputStream.close();
                    if (serialPort != null) serialPort.close();
                } catch (IOException e) {System.out.println(e);}    

有人能帮帮我吗?非常感谢您的努力

EN

回答 1

Stack Overflow用户

发布于 2015-02-24 04:49:37

谢谢你的帮助。问题是在添加串行事件和发送命令之间没有足够的时间。在我将线程设置为休眠几秒钟之后,我得到了没有问题的响应。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28631108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档