@ -8,329 +8,379 @@ import com.iflytop.profilometer.core.migration.utils.ZByteUtils;
public class TPMIPacketFactory {
static public TPMIPacket buildDeviceDetectCmd ( ) { return buildCommand ( TPMIPacket . Command . DEVICE_DETECT ) ; }
static final public Integer CMD_OVERTIME = 50 ;
static public TPMIPacket buildPingCmd ( ) { return buildCommand ( TPMIPacket . Command . PING ) ; }
static public TPMIPacket buildDeviceDetectCmd ( ) {
return buildCommand ( TPMIPacket . Command . DEVICE_DETECT ) ;
}
static public TPMIPacket buildGetDeviceInfoCmd ( ) { return buildCommand ( TPMIPacket . Command . GET_DEVICE_INFO ) ; }
static public TPMIPacket buildPingCmd ( ) {
return buildCommand ( TPMIPacket . Command . PING ) ;
}
static public TPMIPacket buildGetDeviceAppearanceCmd ( ) { return buildCommand ( TPMIPacket . Command . GET_DEVICE_APPEARANCE ) ; }
static public TPMIPacket buildGetDeviceInfoCmd ( ) {
return buildCommand ( TPMIPacket . Command . GET_DEVICE_INFO ) ;
}
static public TPMIPacket buildGetDeviceCalibrationInfoCmd ( ) { return buildCommand ( TPMIPacket . Command . GET_DEVICE_CALIBRATION_INFO ) ; }
static public TPMIPacket buildGetDeviceAppearanceCmd ( ) {
return buildCommand ( TPMIPacket . Command . GET_DEVICE_APPEARANCE ) ;
}
static public TPMIPacket buildUnlockEEPROMCmd ( ) { return buildCommand ( TPMIPacket . Command . UNLOCK_EERPOM ) ; }
static public TPMIPacket buildGetStandbyInactiveTime ( ) {
return buildCommand ( TPMIPacket . Command . GET_STANDBY_INACTIVE_TIME ) ;
}
static public TPMIPacket buildLockEEPROMCmd ( ) { return buildCommand ( TPMIPacket . Command . LOCK_EERPOM ) ; }
;
static public TPMIPacket buildResetMCUCmd ( ) { return buildCommand ( TPMIPacket . Command . RESET_MCU ) ; }
static public TPMIPacket buildGetDeviceCalibrationInfoCmd ( ) {
return buildCommand ( TPMIPacket . Command . GET_DEVICE_CALIBRATION_INFO ) ;
}
static public TPMIPacket buildStartPosSampleCmd ( Integer period ) { return buildCommand ( TPMIPacket . Command . START_POS_SAMPLE , period ) ; }
static public TPMIPacket buildUnlockEEPROMCmd ( ) {
return buildCommand ( TPMIPacket . Command . UNLOCK_EERPOM ) ;
}
static public TPMIPacket buildStopPosSampleCmd ( ) { return buildCommand ( TPMIPacket . Command . STOP_POS_SAMPLE ) ; }
static public TPMIPacket buildLockEEPROMCmd ( ) {
return buildCommand ( TPMIPacket . Command . LOCK_EERPOM ) ;
}
static public TPMIPacket buildResetMCUCmd ( ) {
return buildCommand ( TPMIPacket . Command . RESET_MCU ) ;
}
/ / CALIBRATE_ENCODER1 ( 18 ) , / / 校准编码器0位置
/ / CALIBRATE_ENCODER2 ( 19 ) , / / 校准编码器1位置
/ / CALIBRATE_INCLINATOR_X ( 20 ) , / / 校准水平仪_X
/ / CALIBRATE_INCLINATOR_Y ( 21 ) , / / 校准水平仪_Y
/ / CALIBRATE_INCLINATOR_TEMPERATURE ( 22 ) , / / 校准水平仪温度传感器
static public TPMIPacket buildStartPosSampleCmd ( Integer period ) {
return buildCommand ( TPMIPacket . Command . START_POS_SAMPLE , period ) ;
}
/ / static public TPMIPacket buildCalibrateEncoderZeroPos ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_ENCODER_ZERO_POS ) ; }
/ / static public TPMIPacket buildCalibrateInclinatorZeroPos ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_ZERO_POS ) ; }
static public TPMIPacket buildStopPosSampleCmd ( ) {
return buildCommand ( TPMIPacket . Command . STOP_POS_SAMPLE ) ;
}
static public TPMIPacket buildCalibrateEncoder1 ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_ENCODER1 ) ; }
static public TPMIPacket buildCalibrateEncoder2 ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_ENCODER2 ) ; }
/ / CALIBRATE_ENCODER1 ( 18 ) , / / 校准编码器0位置
/ / CALIBRATE_ENCODER2 ( 19 ) , / / 校准编码器1位置
/ / CALIBRATE_INCLINATOR_X ( 20 ) , / / 校准水平仪_X
/ / CALIBRATE_INCLINATOR_Y ( 21 ) , / / 校准水平仪_Y
/ / CALIBRATE_INCLINATOR_TEMPERATURE ( 22 ) , / / 校准水平仪温度传感器
static public TPMIPacket buildCalibrateInclinatorZeroPosX ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_X ) ; }
/ / static public TPMIPacket buildCalibrateEncoderZeroPos ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_ENCODER_ZERO_POS ) ; }
/ / static public TPMIPacket buildCalibrateInclinatorZeroPos ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_ZERO_POS ) ; }
static public TPMIPacket buildCalibrateInclinatorZeroPosY ( ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_Y ) ; }
static public TPMIPacket buildCalibrateEncoder1 ( ) {
return buildCommand ( TPMIPacket . Command . CALIBRATE_ENCODER1 ) ;
}
static public TPMIPacket buildCalibrateInclinatorTemperature ( Integer nowval ) { return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_TEMPERATURE , nowval ) ; }
static public TPMIPacket buildCalibrateEncoder2 ( ) {
return buildCommand ( TPMIPacket . Command . CALIBRATE_ENCODER2 ) ;
}
static public TPMIPacket buildCalibrateInclinatorZeroPosX ( ) {
return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_X ) ;
}
static public TPMIPacket buildSetDeviceAppearanceCmd ( Double pulleyRadius , Double profilometerHight , Double arm1Length , Double arm2Length ) {
return buildCommand ( TPMIPacket . Command . SET_DEVICE_APPEARANCE , ( int ) ( 1000 * pulleyRadius ) , ( int ) ( 1000 * profilometerHight ) , ( int ) ( 1000 * arm1Length ) , ( int ) ( 1000 * arm2Length ) ) ;
}
static public TPMIPacket buildCalibrateInclinatorZeroPosY ( ) {
return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_Y ) ;
}
static public TPMIPacket buildSetCalibrationTimeCmd ( Integer year , Integer month , Integer day ) {
return buildCommand ( TPMIPacket . Command . SET_CALIBRATION_TIME , year , month , day ) ;
}
/ / SET_ENCODER1_OFFSET ( 23 ) , / / 设置编码器1 - 零偏
/ / RESET_ENCODER1_OFFSET ( 24 ) , / / 重置编码器1 - 零偏
/ / SET_ENCODER2_OFFSET ( 25 ) , / / 设置编码器2 - 零偏
/ / RESET_ENCODER2_OFFSET ( 26 ) , / / 重置编码器2 - 零偏
static public TPMIPacket buildCalibrateInclinatorTemperature ( Integer nowval ) {
return buildCommand ( TPMIPacket . Command . CALIBRATE_INCLINATOR_TEMPERATURE , nowval ) ;
}
static public TPMIPacket buildSetStandbyInactiveCmd ( int standbyInactiveTime ) {
return buildCommand ( TPMIPacket . Command . SET_STANDBY_INACTIVE_TIME , standbyInactiveTime ) ;
}
static public TPMIPacket buildSetDeviceAppearanceCmd ( Double pulleyRadius , Double profilometerHight , Double arm1Length , Double arm2Length ) {
return buildCommand ( TPMIPacket . Command . SET_DEVICE_APPEARANCE , ( int ) ( 1000 * pulleyRadius ) , ( int ) ( 1000 * profilometerHight ) , ( int ) ( 1000 * arm1Length ) , ( int ) ( 1000 * arm2Length ) ) ;
}
static public TPMIPacket buildSetEncoder1ffsetCmd ( Integer offset ) { return buildCommand ( TPMIPacket . Command . SET_ENCODER1_OFFSET , offset ) ; }
static public TPMIPacket buildSetCalibrationTimeCmd ( Integer year , Integer month , Integer day ) {
return buildCommand ( TPMIPacket . Command . SET_CALIBRATION_TIME , year , month , day ) ;
}
/ / SET_ENCODER1_OFFSET ( 23 ) , / / 设置编码器1 - 零偏
/ / RESET_ENCODER1_OFFSET ( 24 ) , / / 重置编码器1 - 零偏
/ / SET_ENCODER2_OFFSET ( 25 ) , / / 设置编码器2 - 零偏
/ / RESET_ENCODER2_OFFSET ( 26 ) , / / 重置编码器2 - 零偏
static public TPMIPacket buildGetEncoder1ffsetCmd ( ) { return buildCommand ( TPMIPacket . Command . GET_ENCODER1_OFFSET ) ; }
static public TPMIPacket buildSetEncoder1ffsetCmd ( Integer offset ) {
return buildCommand ( TPMIPacket . Command . SET_ENCODER1_OFFSET , offset ) ;
}
static public TPMIPacket buildGetEncoder1ffsetCmd ( ) {
return buildCommand ( TPMIPacket . Command . GET_ENCODER1_OFFSET ) ;
}
static public TPMIPacket buildSetEncoder2ffsetCmd ( Integer offset ) {
return buildCommand ( TPMIPacket . Command . SET_ENCODER2_OFFSET , offset ) ;
}
static public TPMIPacket buildGetEncoder2ffsetCmd ( ) {
return buildCommand ( TPMIPacket . Command . GET_ENCODER2_OFFSET ) ;
}
/ /
/ / PRIVETE
/ /
static public TPMIPacket buildSetEncoder2ffsetCmd ( Integer offset ) { return buildCommand ( TPMIPacket . Command . SET_ENCODER2_OFFSET , offset ) ; }
static public TPMIPacket buildGetEncoder2ffsetCmd ( ) { return buildCommand ( TPMIPacket . Command . GET_ENCODER2_OFFSET ) ; }
/ /
/ / PRIVETE
/ /
public enum PacketAnalysisResult {
SUC ,
TAIL_ERROR ,
HEADER_ERROR ,
CHECKSUM_ERROR ,
PACKE_LEN_ERROR ,
PACKE_NOT_FULLY_RECEIVED ,
}
static public Byte Index = 0 ;
static public Byte reportIndex = 0 ;
static private synchronized Byte getIndex ( ) {
Index + + ;
return Index ;
}
static private synchronized Byte getReportIndex ( ) {
reportIndex + + ;
return reportIndex ;
}
static public TPMIPacket buildCommand ( TPMIPacket . Command command , TPMIPacket . DataType dataType , byte [ ] data ) {
byte [ ] rawpacket = new byte [ data . length + TPMIPacket . FIX_PACKET_LEN ] ;
rawpacket [ TPMIPacket . P_HEADER_OFF ] = TPMIPacket . P_HEADER ;
rawpacket [ TPMIPacket . P_DATA_LEN_OFF ] = ( byte ) ( data . length ) ;
rawpacket [ TPMIPacket . P_INDEX_OFF ] = getIndex ( ) ;
rawpacket [ TPMIPacket . P_TYPE_OFF ] = ( byte ) TPMIPacket . PacketType . REQUEST . ordinal ( ) ;
rawpacket [ TPMIPacket . P_CMD_OFF ] = ( byte ) command . getIndex ( ) ;
rawpacket [ TPMIPacket . P_DATA_TYPE_OFF ] = ( byte ) dataType . ordinal ( ) ;
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
rawpacket [ rawpacket . length - 1 ] = TPMIPacket . P_TAIL ;
if ( ! dataType . equals ( TPMIPacket . DataType . NONE ) ) {
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < rawpacket . length - 2 ; i + + ) {
checksum + = rawpacket [ i ] ;
}
rawpacket [ rawpacket . length - 2 ] = checksum ;
return new TPMIPacket ( rawpacket ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , TPMIPacket . DataType dataType , byte [ ] data ) {
byte [ ] rawpacket = new byte [ data . length + TPMIPacket . FIX_PACKET_LEN ] ;
rawpacket [ TPMIPacket . P_HEADER_OFF ] = TPMIPacket . P_HEADER ;
rawpacket [ TPMIPacket . P_DATA_LEN_OFF ] = ( byte ) ( data . length ) ;
rawpacket [ TPMIPacket . P_INDEX_OFF ] = getReportIndex ( ) ;
rawpacket [ TPMIPacket . P_TYPE_OFF ] = ( byte ) TPMIPacket . PacketType . REPORT . ordinal ( ) ;
rawpacket [ TPMIPacket . P_CMD_OFF ] = ( byte ) command . ordinal ( ) ;
rawpacket [ TPMIPacket . P_DATA_TYPE_OFF ] = ( byte ) dataType . ordinal ( ) ;
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
rawpacket [ rawpacket . length - 1 ] = TPMIPacket . P_TAIL ;
if ( ! dataType . equals ( TPMIPacket . DataType . NONE ) ) {
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < rawpacket . length - 2 ; i + + ) {
checksum + = rawpacket [ i ] ;
}
rawpacket [ rawpacket . length - 2 ] = checksum ;
return new TPMIPacket ( rawpacket ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , TPMIPacket . DataType dataType , byte [ ] data ) {
byte [ ] rawpacket = new byte [ data . length + TPMIPacket . FIX_PACKET_LEN ] ;
rawpacket [ TPMIPacket . P_HEADER_OFF ] = TPMIPacket . P_HEADER ;
rawpacket [ TPMIPacket . P_DATA_LEN_OFF ] = ( byte ) ( data . length ) ;
rawpacket [ TPMIPacket . P_INDEX_OFF ] = rx . getIndex ( ) ;
rawpacket [ TPMIPacket . P_TYPE_OFF ] = ( byte ) TPMIPacket . PacketType . RESPONSE . ordinal ( ) ;
rawpacket [ TPMIPacket . P_CMD_OFF ] = ( byte ) rx . getCommand ( ) . getIndex ( ) ;
rawpacket [ TPMIPacket . P_DATA_TYPE_OFF ] = ( byte ) dataType . ordinal ( ) ;
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
rawpacket [ rawpacket . length - 1 ] = TPMIPacket . P_TAIL ;
if ( ! dataType . equals ( TPMIPacket . DataType . NONE ) ) {
System . arraycopy ( data , 0 , rawpacket , 6 , data . length ) ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < rawpacket . length - 2 ; i + + ) {
checksum + = rawpacket [ i ] ;
}
rawpacket [ rawpacket . length - 2 ] = checksum ;
return new TPMIPacket ( rawpacket ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command , Integer . . . data ) {
byte [ ] intdata = new byte [ data . length * 4 ] ;
for ( int i = 0 ; i < data . length ; i + + ) {
intdata [ i * 4 + 3 ] = ( byte ) ( data [ i ] > > 24 ) ;
intdata [ i * 4 + 2 ] = ( byte ) ( data [ i ] > > 16 ) ;
intdata [ i * 4 + 1 ] = ( byte ) ( data [ i ] > > 8 ) ;
intdata [ i * 4 + 0 ] = ( byte ) ( data [ i ] . intValue ( ) ) ;
}
return buildCommand ( command , TPMIPacket . DataType . INT , intdata ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command , byte [ ] rawdata ) {
return buildCommand ( command , TPMIPacket . DataType . BYTE , rawdata ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command ) {
return buildCommand ( command , TPMIPacket . DataType . NONE , new byte [ 0 ] ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command , String data ) {
return buildCommand ( command , TPMIPacket . DataType . STR , data . getBytes ( ) ) ;
}
static public TPMIPacket buildSetSNCmd ( Integer year , Integer week , Integer index ) {
DeviceSN sn = new DeviceSN ( year , week , index ) ;
return buildCommand ( TPMIPacket . Command . SET_SN , sn . toInteger ( ) ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , String data ) {
return buildReport ( command , TPMIPacket . DataType . STR , data . getBytes ( ) ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , byte [ ] rawdata ) {
return buildReport ( command , TPMIPacket . DataType . BYTE , rawdata ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , Integer . . . data ) {
byte [ ] intdata = new byte [ data . length * 4 ] ;
for ( int i = 0 ; i < data . length ; i + + ) {
intdata [ i * 4 + 3 ] = ( byte ) ( data [ i ] > > 24 ) ;
intdata [ i * 4 + 2 ] = ( byte ) ( data [ i ] > > 16 ) ;
intdata [ i * 4 + 1 ] = ( byte ) ( data [ i ] > > 8 ) ;
intdata [ i * 4 ] = ( byte ) ( data [ i ] . intValue ( ) ) ;
}
return buildReport ( command , TPMIPacket . DataType . INT , intdata ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command ) {
return buildReport ( command , TPMIPacket . DataType . NONE , new byte [ 0 ] ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , String data ) {
return buildReceipt ( rx , TPMIPacket . DataType . STR , data . getBytes ( ) ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , byte [ ] rawdata ) {
return buildReceipt ( rx , TPMIPacket . DataType . BYTE , rawdata ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , Integer . . . data ) {
return buildReceipt ( rx , TPMIPacket . DataType . INT , ZByteUtils . integerToLittleEndianByte ( data ) ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx ) {
return buildReceipt ( rx , TPMIPacket . DataType . NONE , new byte [ 0 ] ) ;
}
synchronized static public TPMIPacket parseRawRxPacketToResponse ( byte [ ] rawpacket , int length ) {
/ / SubDevicePacketUtils
if ( isValidatePacket ( rawpacket , length , 0 ) . equals ( PacketAnalysisResult . SUC ) ) {
var newPacket = new TPMIPacket ( rawpacket , 0 , length ) ;
if ( newPacket . getPacketType ( ) . equals ( TPMIPacket . PacketType . RESPONSE ) ) {
return newPacket ;
}
}
return null ;
}
public static class PacketAnalysisReport {
public PacketAnalysisResult result ; / / 分析结果
public TPMIPacket packet ; / / 分析出的数据包
public byte [ ] rawdata ; / / 分析出的数据包
}
/ * *
* 解析接收到的数据包
* @param indata 接收到的数据包
* @param indatalen 接收到数据总长
* @param startoff 分析的起始位置
* @return 分析结果
* /
public static PacketAnalysisReport analysisRawRxData ( byte [ ] indata , int indatalen , int startoff ) {
PacketAnalysisReport report = new PacketAnalysisReport ( ) ;
report . result = isValidatePacket ( indata , indatalen , startoff ) ;
if ( report . result . equals ( PacketAnalysisResult . SUC ) ) {
byte packetLen = ( byte ) ( indata [ startoff + TPMIPacket . P_DATA_LEN_OFF ] + TPMIPacket . FIX_PACKET_LEN ) ;
report . rawdata = ZByteUtils . subBytes ( indata , startoff , packetLen ) ;
TPMIPacket . Command cmd = TPMIPacket . getCommand ( report . rawdata ) ;
switch ( cmd ) {
case SAMPLE_VAL_REPORT :
report . packet = new TPMIPosReport ( report . rawdata ) ;
break ;
case BASIC_REPORT :
report . packet = new TPMIBasicReport ( report . rawdata ) ;
break ;
default :
report . packet = new TPMIPacket ( report . rawdata ) ;
break ;
}
} else if ( report . result . equals ( PacketAnalysisResult . CHECKSUM_ERROR ) ) {
byte packetLen = ( byte ) ( indata [ startoff + TPMIPacket . P_DATA_LEN_OFF ] + TPMIPacket . FIX_PACKET_LEN ) ;
report . rawdata = ZByteUtils . subBytes ( indata , startoff , packetLen ) ;
}
return report ;
}
public static PacketAnalysisResult isValidatePacket ( byte [ ] indata , int indatalen , int startoff ) {
if ( indata [ startoff ] ! = TPMIPacket . P_HEADER ) {
return PacketAnalysisResult . HEADER_ERROR ;
}
if ( ( indatalen - startoff ) < TPMIPacket . FIX_PACKET_LEN ) {
return PacketAnalysisResult . PACKE_NOT_FULLY_RECEIVED ;
}
int packetLen = indata [ startoff + TPMIPacket . P_DATA_LEN_OFF ] + TPMIPacket . FIX_PACKET_LEN ;
if ( packetLen > ( indatalen - startoff ) ) {
return PacketAnalysisResult . PACKE_NOT_FULLY_RECEIVED ;
}
byte tail = indata [ startoff + packetLen - 1 ] ;
if ( tail ! = TPMIPacket . P_TAIL ) {
return PacketAnalysisResult . TAIL_ERROR ;
}
if ( packetLen < TPMIPacket . FIX_PACKET_LEN ) {
return PacketAnalysisResult . PACKE_LEN_ERROR ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < packetLen - 2 ; i + + ) {
if ( i = = ( packetLen - 2 ) ) {
continue ;
}
checksum + = indata [ startoff + i ] ;
}
if ( checksum ! = indata [ startoff + packetLen - 2 ] ) {
return PacketAnalysisResult . CHECKSUM_ERROR ;
}
return PacketAnalysisResult . SUC ;
}
public enum PacketAnalysisResult {
SUC ,
TAIL_ERROR ,
HEADER_ERROR ,
CHECKSUM_ERROR ,
PACKE_LEN_ERROR ,
PACKE_NOT_FULLY_RECEIVED ,
}
static public Byte Index = 0 ;
static public Byte reportIndex = 0 ;
static private synchronized Byte getIndex ( ) {
Index + + ;
return Index ;
}
static private synchronized Byte getReportIndex ( ) {
reportIndex + + ;
return reportIndex ;
}
static public TPMIPacket buildCommand ( TPMIPacket . Command command , TPMIPacket . DataType dataType , byte [ ] data ) {
byte [ ] rawpacket = new byte [ data . length + TPMIPacket . FIX_PACKET_LEN ] ;
rawpacket [ TPMIPacket . P_HEADER_OFF ] = TPMIPacket . P_HEADER ;
rawpacket [ TPMIPacket . P_DATA_LEN_OFF ] = ( byte ) ( data . length ) ;
rawpacket [ TPMIPacket . P_INDEX_OFF ] = getIndex ( ) ;
rawpacket [ TPMIPacket . P_TYPE_OFF ] = ( byte ) TPMIPacket . PacketType . REQUEST . ordinal ( ) ;
rawpacket [ TPMIPacket . P_CMD_OFF ] = ( byte ) command . getIndex ( ) ;
rawpacket [ TPMIPacket . P_DATA_TYPE_OFF ] = ( byte ) dataType . ordinal ( ) ;
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
rawpacket [ rawpacket . length - 1 ] = TPMIPacket . P_TAIL ;
if ( ! dataType . equals ( TPMIPacket . DataType . NONE ) ) {
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < rawpacket . length - 2 ; i + + ) {
checksum + = rawpacket [ i ] ;
}
rawpacket [ rawpacket . length - 2 ] = checksum ;
return new TPMIPacket ( rawpacket ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , TPMIPacket . DataType dataType , byte [ ] data ) {
byte [ ] rawpacket = new byte [ data . length + TPMIPacket . FIX_PACKET_LEN ] ;
rawpacket [ TPMIPacket . P_HEADER_OFF ] = TPMIPacket . P_HEADER ;
rawpacket [ TPMIPacket . P_DATA_LEN_OFF ] = ( byte ) ( data . length ) ;
rawpacket [ TPMIPacket . P_INDEX_OFF ] = getReportIndex ( ) ;
rawpacket [ TPMIPacket . P_TYPE_OFF ] = ( byte ) TPMIPacket . PacketType . REPORT . ordinal ( ) ;
rawpacket [ TPMIPacket . P_CMD_OFF ] = ( byte ) command . ordinal ( ) ;
rawpacket [ TPMIPacket . P_DATA_TYPE_OFF ] = ( byte ) dataType . ordinal ( ) ;
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
rawpacket [ rawpacket . length - 1 ] = TPMIPacket . P_TAIL ;
if ( ! dataType . equals ( TPMIPacket . DataType . NONE ) ) {
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < rawpacket . length - 2 ; i + + ) {
checksum + = rawpacket [ i ] ;
}
rawpacket [ rawpacket . length - 2 ] = checksum ;
return new TPMIPacket ( rawpacket ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , TPMIPacket . DataType dataType , byte [ ] data ) {
byte [ ] rawpacket = new byte [ data . length + TPMIPacket . FIX_PACKET_LEN ] ;
rawpacket [ TPMIPacket . P_HEADER_OFF ] = TPMIPacket . P_HEADER ;
rawpacket [ TPMIPacket . P_DATA_LEN_OFF ] = ( byte ) ( data . length ) ;
rawpacket [ TPMIPacket . P_INDEX_OFF ] = rx . getIndex ( ) ;
rawpacket [ TPMIPacket . P_TYPE_OFF ] = ( byte ) TPMIPacket . PacketType . RESPONSE . ordinal ( ) ;
rawpacket [ TPMIPacket . P_CMD_OFF ] = ( byte ) rx . getCommand ( ) . getIndex ( ) ;
rawpacket [ TPMIPacket . P_DATA_TYPE_OFF ] = ( byte ) dataType . ordinal ( ) ;
System . arraycopy ( data , 0 , rawpacket , TPMIPacket . P_DATA_OFF , data . length ) ;
rawpacket [ rawpacket . length - 1 ] = TPMIPacket . P_TAIL ;
if ( ! dataType . equals ( TPMIPacket . DataType . NONE ) ) {
System . arraycopy ( data , 0 , rawpacket , 6 , data . length ) ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < rawpacket . length - 2 ; i + + ) {
checksum + = rawpacket [ i ] ;
}
rawpacket [ rawpacket . length - 2 ] = checksum ;
return new TPMIPacket ( rawpacket ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command , Integer . . . data ) {
byte [ ] intdata = new byte [ data . length * 4 ] ;
for ( int i = 0 ; i < data . length ; i + + ) {
intdata [ i * 4 + 3 ] = ( byte ) ( data [ i ] > > 24 ) ;
intdata [ i * 4 + 2 ] = ( byte ) ( data [ i ] > > 16 ) ;
intdata [ i * 4 + 1 ] = ( byte ) ( data [ i ] > > 8 ) ;
intdata [ i * 4 + 0 ] = ( byte ) ( data [ i ] . intValue ( ) ) ;
}
return buildCommand ( command , TPMIPacket . DataType . INT , intdata ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command , byte [ ] rawdata ) {
return buildCommand ( command , TPMIPacket . DataType . BYTE , rawdata ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command ) {
return buildCommand ( command , TPMIPacket . DataType . NONE , new byte [ 0 ] ) ;
}
static private TPMIPacket buildCommand ( TPMIPacket . Command command , String data ) {
return buildCommand ( command , TPMIPacket . DataType . STR , data . getBytes ( ) ) ;
}
static public TPMIPacket buildSetSNCmd ( Integer year , Integer week , Integer index ) {
DeviceSN sn = new DeviceSN ( year , week , index ) ;
return buildCommand ( TPMIPacket . Command . SET_SN , sn . toInteger ( ) ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , String data ) {
return buildReport ( command , TPMIPacket . DataType . STR , data . getBytes ( ) ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , byte [ ] rawdata ) {
return buildReport ( command , TPMIPacket . DataType . BYTE , rawdata ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command , Integer . . . data ) {
byte [ ] intdata = new byte [ data . length * 4 ] ;
for ( int i = 0 ; i < data . length ; i + + ) {
intdata [ i * 4 + 3 ] = ( byte ) ( data [ i ] > > 24 ) ;
intdata [ i * 4 + 2 ] = ( byte ) ( data [ i ] > > 16 ) ;
intdata [ i * 4 + 1 ] = ( byte ) ( data [ i ] > > 8 ) ;
intdata [ i * 4 ] = ( byte ) ( data [ i ] . intValue ( ) ) ;
}
return buildReport ( command , TPMIPacket . DataType . INT , intdata ) ;
}
static public TPMIPacket buildReport ( TPMIPacket . Command command ) {
return buildReport ( command , TPMIPacket . DataType . NONE , new byte [ 0 ] ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , String data ) {
return buildReceipt ( rx , TPMIPacket . DataType . STR , data . getBytes ( ) ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , byte [ ] rawdata ) {
return buildReceipt ( rx , TPMIPacket . DataType . BYTE , rawdata ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx , Integer . . . data ) {
return buildReceipt ( rx , TPMIPacket . DataType . INT , ZByteUtils . integerToLittleEndianByte ( data ) ) ;
}
static public TPMIPacket buildReceipt ( TPMIPacket rx ) {
return buildReceipt ( rx , TPMIPacket . DataType . NONE , new byte [ 0 ] ) ;
}
synchronized static public TPMIPacket parseRawRxPacketToResponse ( byte [ ] rawpacket , int length ) {
/ / SubDevicePacketUtils
if ( isValidatePacket ( rawpacket , length , 0 ) . equals ( PacketAnalysisResult . SUC ) ) {
var newPacket = new TPMIPacket ( rawpacket , 0 , length ) ;
if ( newPacket . getPacketType ( ) . equals ( TPMIPacket . PacketType . RESPONSE ) ) {
return newPacket ;
}
}
return null ;
}
public static class PacketAnalysisReport {
public PacketAnalysisResult result ; / / 分析结果
public TPMIPacket packet ; / / 分析出的数据包
public byte [ ] rawdata ; / / 分析出的数据包
}
/ * *
* 解析接收到的数据包
*
* @param indata 接收到的数据包
* @param indatalen 接收到数据总长
* @param startoff 分析的起始位置
* @return 分析结果
* /
public static PacketAnalysisReport analysisRawRxData ( byte [ ] indata , int indatalen , int startoff ) {
PacketAnalysisReport report = new PacketAnalysisReport ( ) ;
report . result = isValidatePacket ( indata , indatalen , startoff ) ;
if ( report . result . equals ( PacketAnalysisResult . SUC ) ) {
byte packetLen = ( byte ) ( indata [ startoff + TPMIPacket . P_DATA_LEN_OFF ] + TPMIPacket . FIX_PACKET_LEN ) ;
report . rawdata = ZByteUtils . subBytes ( indata , startoff , packetLen ) ;
TPMIPacket . Command cmd = TPMIPacket . getCommand ( report . rawdata ) ;
switch ( cmd ) {
case SAMPLE_VAL_REPORT :
report . packet = new TPMIPosReport ( report . rawdata ) ;
break ;
case BASIC_REPORT :
report . packet = new TPMIBasicReport ( report . rawdata ) ;
break ;
default :
report . packet = new TPMIPacket ( report . rawdata ) ;
break ;
}
} else if ( report . result . equals ( PacketAnalysisResult . CHECKSUM_ERROR ) ) {
byte packetLen = ( byte ) ( indata [ startoff + TPMIPacket . P_DATA_LEN_OFF ] + TPMIPacket . FIX_PACKET_LEN ) ;
report . rawdata = ZByteUtils . subBytes ( indata , startoff , packetLen ) ;
}
return report ;
}
public static PacketAnalysisResult isValidatePacket ( byte [ ] indata , int indatalen , int startoff ) {
if ( indata [ startoff ] ! = TPMIPacket . P_HEADER ) {
return PacketAnalysisResult . HEADER_ERROR ;
}
if ( ( indatalen - startoff ) < TPMIPacket . FIX_PACKET_LEN ) {
return PacketAnalysisResult . PACKE_NOT_FULLY_RECEIVED ;
}
int packetLen = indata [ startoff + TPMIPacket . P_DATA_LEN_OFF ] + TPMIPacket . FIX_PACKET_LEN ;
if ( packetLen > ( indatalen - startoff ) ) {
return PacketAnalysisResult . PACKE_NOT_FULLY_RECEIVED ;
}
byte tail = indata [ startoff + packetLen - 1 ] ;
if ( tail ! = TPMIPacket . P_TAIL ) {
return PacketAnalysisResult . TAIL_ERROR ;
}
if ( packetLen < TPMIPacket . FIX_PACKET_LEN ) {
return PacketAnalysisResult . PACKE_LEN_ERROR ;
}
byte checksum = 0 ;
for ( int i = 0 ; i < packetLen - 2 ; i + + ) {
if ( i = = ( packetLen - 2 ) ) {
continue ;
}
checksum + = indata [ startoff + i ] ;
}
if ( checksum ! = indata [ startoff + packetLen - 2 ] ) {
return PacketAnalysisResult . CHECKSUM_ERROR ;
}
return PacketAnalysisResult . SUC ;
}
}