本文介绍了如何使用java byte []准确发送如此复杂的十六进制二进制协议数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这种东西很困惑,我应该作为最终命令发送什么,总是将8位混淆为1个字节,但是我们该怎么做呢?如屏幕截图所示,仅仅是命令包[hex]吗?还是在两个屏幕快照中显示了标头+命令包[hex]?

I am very confused with this kind of stuffs, What should I send as final command, get always confused 8 bits to 1 byte but how do we make it? Is it only the command packet [hex] as shown in screen shot? or Is it header + command packet[hex] shown in two screen shots?

混乱的详细信息:

  • 在这样的图中,标题块大都显示为"Bit 7,Bit 6,..,Bit 0",而不是"Bit 0,Bit 1,Bit 2,... Bit 7",我一直想知道为什么?

  • In such diagram header block shows mostly like "Bit 7, Bit 6,..,Bit 0" instead of "Bit 0, Bit 1, Bit 2, ... Bit 7", i always wondering why?.

但是当我在代码中应用字节st [0] =位7或位1时,应遵循以下顺序吗?

But when I apply in code what is following order for byte st[0] = Bit 7 or Bit 1?

也根据此图,这是否意味着我发送的每个命令都将始终固定标头?

Also according to this diagram, does it mean every command I send will have Header fixed always?

这是我尝试通过将位1用作st [0],将位2用作st 1 而不是第7位为st [0].进行电源开/关测试.

This is the code I was trying by taking Bit 1 as st[0], Bit 2 as st1 instead of Bit 7 as st[0]. To apply Power off/on test.

import java.io.*;
import java.net.*;
public class test 
{

public static void main(String[] args) throws UnknownHostException, IOException 
{ 

    byte st[]=new byte[256];
    st[0]=0x01; // get    
    st[1]=0x02; // header 2 byte
    st[2]=0x02; // header length            
    st[3]=0;    // command byte
    st[4]=0;    // reserved
    st[5]=0;            
    st[6]=0;             
    st[7]=0;
    st[8]=0x01; // power off
    st[9]=0x30; 
    st[10]=0x01;
    st[11]=0x00; 
    System.out.println(st); // Should this work, am i correct based on diagram?


    Socket s           = new Socket("192.168.1.2", 49137);
    DataInputStream input         = new DataInputStream(s.getInputStream());          
    DataOutputStream outToServer  = new DataOutputStream(s.getOutputStream());
    BufferedReader i   = new BufferedReader(new InputStreamReader(s.getInputStream()));
    outToServer.write(st);
    String get;
    get = i.readLine();
    System.out.println("FROM SERVER: " + get);
    s.close();        
}

}

P.S:您实际上会如何做?每个十六进制命令都是手工编写的,此PDF文件有将近100条命令,这将花费大量时间.还是您以不同的方式管理它们?

P.S: How would you do this really? Every hex command make hand by hand, this PDF file has almost 100 commands, it would take lot of time. Or you manage them in a different way?

推荐答案

  • 在典型的数字中,写0是最低有效位,7是最高有效,而字节则以最高有效至最低有效7-0写入.

    In typical number writing 0 is the least significant bit 7 the most significant and the byte is written in most significant to least significant 7-0.

    byte  11111111
    place 76543210
    

    • 在您的代码中,这不是设置位,而是在字节数组中设置一个字节

      In your code this is not setting bits this is setting a byte in the byte array

      如果您担心设置位,请尝试这样的课程:

      If you are nervous about setting bits try a class like this:

      public class SimpleByteSetter {
      
        /* b is is byte you are setting
           on is if the bit is set to on or 1
           place is the bit place in the range of 0-7
        */
        public static byte set(final byte b, final boolean on, final int place) {
          if (on) { return (byte) (b | ((1 << place) & 0xFF)); }
          return (byte) (b & (~((1 << place) & 0xFF)));
        }
      
        // 1 == on everything else off (but only use 0!)
        public static byte set(final byte b, final int on, final int place) {
          return set(b, 1==on, place);
        }
      
      }
      

      在您的代码中使用它,例如:

      use it in you code like:

      byte header = 0;
      // get = 0, set = 1, place = 0
      header = SimpleByteSetter(header, 0, 0 ); // get
      // header 2 byte = 0, header 2 byte = 1, place = 1
      header = SimpleByteSetter(header, 0, 1 ); // header 1 byte
      ...
      st[0] = header;
      

        我没有足够的信息来实际构建数据包,但是:

        I don't have enough information to actually build the packet but:

        // the header is 1 byte I don't see how to make it two
        // the command is 2 bytes per the table
        // the parameter length is 0 so 1 byte (is there suppose to be a device id?)
        byte[] st = new byte[ 1 + 2 + 1 ];
        
        byte header = 0;
        // get = 0, set = 1, place = 0
        header = SimpleByteSetter(header, 0, 0 ); // get
        // header 2 byte = 0, header 2 byte = 1, place = 1
        header = SimpleByteSetter(header, 0, 1 ); // header 1 byte
        // length 1 byte = 0, length 2 byte = 1
        header = SimpleByteSetter(header, 0, 2 );
        // command 1 byte = 0, command 2 byte = 1; 
        header = SimpleByteSetter(header, 1, 3 );
        
        st[0] = header;
        st[1] = 0x0130 & 0xFF; // poweroff command first byte
        st[2] = 0x0100 & 0xFF; // poweroff second byte
        st[3] = 0;
        

        这篇关于如何使用java byte []准确发送如此复杂的十六进制二进制协议数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:02