BGS5T RS232与1线传感器通信

时间:2014-11-12 07:33:06

标签: java serial-port midlet gemalto 1wire

我有Gemalto BGS5T java模块和1线温度传感器。我有java midlet使用RS232端口与温度传感器通信。但问题是,无论发送什么,我都没有得到传感器的响应。传感器上有正确的电压,因此连接应该没问题。 我尝试测试我的程序,以便我将rs232端口连接到计算机并观看终端(Termite),如果发送的数据是正确的,一切看起来都应该。 另一个测试是我将温度传感器直接连接到计算机并使用终端发送数据,它的工作方式应该如此。我甚至得到了一些随机输入的响应,如9999.我检查了终端内连接的参数并将它们复制到java midlet,但没有成功。 只有一次我得到了一些回复,但是当我第二天尝试继续工作时,我没有成功。 终端内的参数: 波特率:9600 数据位:8 停止位:1 平价:没有 流量控制:RTS / CTS

这是Java代码:

String strCOM = "comm:COM0;blocking=on;baudrate=9600";
commConn = (CommConnection)Connector.open(strCOM);
System.out.println("CommConnection(" + strCOM + ") opened");
System.out.println("Real baud rate: " + commConn.getBaudRate());
inStream  = commConn.openInputStream();
outStream = commConn.openOutputStream();
System.out.println("InputStream and OutputStream opened");
while(1==1)
    {
        byte bC1 = (byte)Integer.parseInt("11000001",2);
        byte C1 = hexToBin("C1");
        byte bparameter2 = (byte)Integer.parseInt("00010111",2);
        byte bparameter3 = (byte)Integer.parseInt("01000101",2);
        byte bparameter4 = (byte)Integer.parseInt("01011011",2);
        byte bparameter5 = (byte)Integer.parseInt("00001111",2);
        byte bparameter6 = (byte)Integer.parseInt("10010101",2);
        byte[] bArray = {bparameter2,bparameter3,bparameter4,bparameter5,bparameter6};





      int ch;
      try {
        outStream.write(bC1);
        Thread.sleep(50);
        //outStream.write(bArray);
        outStream.write(bparameter2);
        outStream.write(bparameter3);
        outStream.write(bparameter4);
        outStream.write(bparameter5);
        outStream.write(bparameter6);

        System.err.println("inStream bytes:" + inStream.available());
        if(inStream.available() > 0)
        {   
          String msg = "";
          while(inStream.available() > 0)
          {
            ch = inStream.read();
            msg = msg + (char) ch;
          }
          System.out.println("Serial msg: " + msg);
        }
        outStream.write('9');
        outStream.write('9');
        outStream.write('9');
        outStream.write('9');
        Thread.sleep(100);
        outStream.write('9');
        outStream.write('9');
        outStream.write('9');
        outStream.write('9');
        System.err.println("inStream bytes:" + inStream.available());
        if(inStream.available() > 0)
        {   
          String msg = "";
          while(inStream.available() > 0)
          {
            ch = inStream.read();
            msg = msg + (char) ch;
          }
          System.out.println("Serial msg: " + msg);
        }

1 个答案:

答案 0 :(得分:1)

我必须使用零调制解调器电缆才能使其正常工作。