Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RXTX和LRC传输程序_Java_Rxtx - Fatal编程技术网

Java RXTX和LRC传输程序

Java RXTX和LRC传输程序,java,rxtx,Java,Rxtx,所以,我有一个卡锁定系统设备,通过RS-232 DB9串行端口连接。 这是我第一次使用它处理外部设备。所以我读了手册 表示传输程序文本格式定义如下: -文本必须由STX和ETC之间最多500个字符组成 -LRC计算的区域范围为从STX到ETX的第一个字符 还有一个控制字符列表(STX、ETX、ACK、NAK)及其十六进制代码 我对此一无所知。请开导我。 哦,还有,我可以检测设备是否连接到特定端口吗 我已使用以下代码成功连接到通信端口: public class TwoWaySerialComm

所以,我有一个卡锁定系统设备,通过RS-232 DB9串行端口连接。 这是我第一次使用它处理外部设备。所以我读了手册 表示传输程序文本格式定义如下:

-文本必须由STX和ETC之间最多500个字符组成

-LRC计算的区域范围为从STX到ETX的第一个字符

还有一个控制字符列表(STX、ETX、ACK、NAK)及其十六进制代码

我对此一无所知。请开导我。 哦,还有,我可以检测设备是否连接到特定端口吗

我已使用以下代码成功连接到通信端口:

public class TwoWaySerialComm
{
    protected InputStream inputStream;
    protected OutputStream outputStream;

    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(9600,SerialPort.DATABITS_7,SerialPort.STOPBITS_1,SerialPort.PARITY_ODD);

                inputStream = serialPort.getInputStream();
                outputStream = serialPort.getOutputStream();

                (new Thread(new SerialReader(inputStream))).start();
                (new Thread(new SerialWriter(outputStream))).start();

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

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    /** */
    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
        {
            TwoWaySerialComm comm = new TwoWaySerialComm();
            comm.connect("COM3");
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
这是我找到的用于从字节数组中获取LRC的代码:

    public byte calculateLRC(byte[] data)
    {
        byte checksum = 0;
        for (int i = 0; i <= data.length - 1; i++) {
            checksum = (byte) ((checksum + data[i]) & 0xFF);
        }

      checksum = (byte) (((checksum ^ 0xFF) + 1) & 0xFF);
        return checksum;
    }
public byte calculateLRC(byte[]数据)
{
字节校验和=0;
对于(int i=0;i
另请参见LIS/RIS消息格式链接

发送确认

public void sendAcknowledgement()throws IOException{
         String ack= ASCIITable.ACK + "";
         outputStream.write(ack.getBytes());
    }

如果您完全没有串行通信的经验,我将首先研究一些现有的、已知的工作实现。John Ellinwood的答案包含XModem协议的Java源代码,这是一种古老、简单的调制解调器文件传输协议。这将向您展示如何处理通信本身(您必须始终为超时和其他错误条件做好准备)和控制字符


不要忘记Java的一个特殊之处是Java中没有无符号类型,如果您可以使用的变量类型都是有符号的,那么这些低级协议的编程几乎总是会变得有点复杂。但是正如XModem代码所示,这是完全可行的。

OutputStream写入字节,因此您必须转换str首先将数据绑定到字节数组:

outputStream.write("CES01".getBytes());

是的,它们也写在手册中,但是现在我如何正确地将“CES01”字符串发送到设备?
outputStream.write("CES01".getBytes());