package tbDataModel_Dell; import java.util.Vector; public class Read_Write_Anchor_Message { /**¶ÁÈ¡»ùÕ¾ÐÅÏ¢ * @param ref ÆðʼµØÖ· * @param lenth ¶ÁÈ¡³¤¶È*/ public static byte[] ReadData(byte ref ,byte lenth) { byte[] buf =new byte[10]; byte[] buf1 =new byte[6]; //°üÍ· buf[0]=(byte) 0x55; //°üÍ· buf[1]=(byte) 0xAA; //Ö¸ÁîÀàÐÍ(²ÎÊý¶Áдģʽ) buf1[0]=(byte) 0x03; //Êý¾Ý³¤¶È buf1[1]=(byte) 0x06; //¶Áģʽ(¶Áģʽ) buf1[2]=(byte) 0x01; //ÆðʼµØÖ· buf1[3]=ref; //Êý¾Ý³¤¶È buf1[4]=lenth; //¹Ì¶¨Öµ(¶ÁģʽÏÂΪ0x00) buf1[5]=(byte) 0x00; for(int i=0;i vec,byte adress) { byte[] buf =null; //×ܹ²ÓжàÉÙ×é±êÇ© int zu=vec.size()/2; if(zu<10) { int lenth=zu*6; String hexzu=Integer.toHexString(zu); if(zu<17) { hexzu="0"+Integer.toHexString(zu); } buf =new byte[8+lenth]; byte[] buf1 =new byte[4+lenth]; //°üÍ· buf[0]=(byte) 0x55; //°üÍ· buf[1]=(byte) 0xAA; //Ö¸ÁîÀàÐÍ buf1[0]=(byte) 0x08; //Êý¾Ý³¤¶È buf1[1]=(byte) ((byte)4+zu*6); //ID×éÊý×î´óΪ20×é buf1[2]=toByteArray(hexzu)[0]; //²ÎÊýµØÖ· buf1[3]=adress; for(int i=0;i> 24)); registers[1] = (byte) (0xff & (v >> 16)); registers[2] = (byte) (0xff & (v >> 8)); registers[3] = (byte) (0xff & v); return registers; } /** * 16½øÖƵÄ×Ö·û´®±íʾת³É×Ö½ÚÊý×é * * @param hexString 16½øÖƸñʽµÄ×Ö·û´® * @return ת»»ºóµÄ×Ö½ÚÊý×é **/ public static byte[] toByteArray(String hexString) { byte[] byteArray=null; if(hexString !=null || hexString !="") { hexString = hexString.replaceAll(" ", ""); byteArray = new byte[hexString.length() / 2]; int k = 0; for (int i = 0; i < byteArray.length; i++) {//ÒòΪÊÇ16½øÖÆ£¬×î¶àÖ»»áÕ¼ÓÃ4λ£¬×ª»»³É×Ö½ÚÐèÒªÁ½¸ö16½øÖƵÄ×Ö·û£¬¸ßλÔÚÏÈ byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff); byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff); byteArray[i] = (byte) (high << 4 | low); k += 2; } } return byteArray; } /**д´®¿ÚÊý¾Ý°ü * @param ref ÆðʼµØÖ· * @param lenth Êý¾Ý³¤¶È * @param value дÈëÖµ*/ public static byte[] WriteData(byte ref ,byte lenth,int value) { byte[] buf =new byte[9+lenth]; byte[] buf1 =new byte[5+lenth]; //°üÍ· buf[0]=(byte) 0x55; //°üÍ· buf[1]=(byte) 0xAA; //Ö¸ÁîÀàÐÍ buf1[0]=(byte) 0x03; //Êý¾Ý³¤¶È buf1[1]=(byte) ((byte) 5+lenth); //¶Áģʽ buf1[2]=(byte) 0x02; //ÆðʼµØÖ· buf1[3]=(byte) ref; //Êý¾Ý³¤¶È buf1[4]=(byte) lenth; //дÈëÖµ if(lenth==2) { buf1[5]=ModbusUtil.intToRegisters(value)[3]; buf1[6]=ModbusUtil.intToRegisters(value)[2]; } for(int i=0;i