如何在rs232连接上写

时间:2013-12-09 17:29:04

标签: java serial-port rxtx

我正在开发一个需要连接rs232和外设的小软件......

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

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”,我该怎么做?

由于

1 个答案:

答案 0 :(得分:0)

您遵循的示例是将您键入的所有内容写入控制台(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();
        }            
    }
}

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