java - 如何在rs232连接上写入

标签 java serial-port rxtx

我正在开发一个小软件,需要与外围设备连接 rs232...

我已经下载了最新版本的 RXTX 库,并从同一站点使用 TwoWaySerialComm 类通过串行电缆进行通信

http://rxtx.qbang.org/wiki/index.php/Event_based_two_way_Communication

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TwoWaySerialComm
{
    public TwoWaySerialComm()
    {
        super();
    }

    void connect ( String portName ) throws Exception
    {
        CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
        if ( portIdentifier.isCurrentlyOwned() )
        {
            System.out.println("Error: Port is currently in use");
        }
        else
        {
            CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

            if ( commPort instanceof SerialPort )
            {
                SerialPort serialPort = (SerialPort) commPort;
                serialPort.setSerialPortParams(57600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

                InputStream in = serialPort.getInputStream();
                OutputStream out = serialPort.getOutputStream();

                (new Thread(new SerialReader(in))).start();
                (new Thread(new SerialWriter(out))).start();

            }
            else
            {
                System.out.println("Error: Only serial ports are handled by this example.");
            }
        }     
    }

    /** */
    public static class SerialReader implements Runnable 
    {
        InputStream in;

        public SerialReader ( InputStream in )
        {
            this.in = in;
        }

        public void run ()
        {
            byte[] buffer = new byte[1024];
            int len = -1;
            try
            {
                while ( ( len = this.in.read(buffer)) > -1 )
                {
                    System.out.print(new String(buffer,0,len));
                }
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    /** */
    public static class SerialWriter implements Runnable 
    {
        OutputStream out;

        public SerialWriter ( OutputStream out )
        {
            this.out = out;
        }

        public void run ()
        {
            try
            {                
                int c = 0;
                while ( ( c = System.in.read()) > -1 )
                {
                    this.out.write(c);
                }                
            }
            catch ( IOException e )
            {
                e.printStackTrace();
            }            
        }
    }

    public static void main ( String[] args )
    {
        try
        {
            (new TwoWaySerialComm()).connect("COM3");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

现在我必须向设备发送文本“STX 1000 T ETX”,我该怎么做?

谢谢

最佳答案

您遵循的示例是将您输入的所有内容写入控制台 (System.in),并输出到串行端口。您将需要修改您的程序,以便可以输出特定的字符串,而不是您输入的任何内容。可能的解决方案多种多样,无法在此处提供答案,并且它要求您至少具有一些 Java 编程背景。

作为开始,请考虑创建一个如下所示的新类:

public class MyCustomSerialWriter
{
    OutputStream out;

    public SerialWriter ( OutputStream out )
    {
        this.out = out;
    }

    public void writeString (String str)
    {
        try {
            this.out.write(str.getBytes(Charset.forName("ASCII"));
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }            
    }
}

然后您可以从主方法中调用它来输出您想要的任何字符串。

关于java - 如何在rs232连接上写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20476798/

相关文章:

Java 3D 代码给出 java.lang.UnsatisfiedLinkError

Java:字符串模式:如何为所有具有特殊字符的字母字符指定正则表达式

java - Java 左子树中最右边的节点

android - eclipse无法解析android native 代码

创建串口设备文件

来自 Com 端口的 Java readLine

该类的java junit测试用例

java - 覆盖Android上的删除键?

Mac 上的 Java rxtx lib segv 错误

java - rxtx 给我 NoSuchPortException