# jcstruct **Repository Path**: yhIlc/jcstruct ## Basic Information - **Project Name**: jcstruct - **Description**: 为了解决java与C结构通信过程中结构体解析问题 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://github.com/yanghao0518/cstruct-parser - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 5 - **Created**: 2017-05-23 - **Last Updated**: 2022-10-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 介绍   描述:为了解决java与C结构通信过程中结构体解析问题。   主要功能:能友好的用java处理任何发送的C结构体对象,并且能发送java对象转换成C结构体接收的二进制。
# 功能说明 1、基于spring框架开发
2、对于结构体定义采用可视化XML配置,便于维护与调试,防止协议多次修改带来的麻烦。
3、所有关健解析操作都基于接口开发,有默认实现,也可以用于自定义实现,能满足所有特殊业务解析需求。
4、配置简单,使用方便、易上手。
# 使用实例
1、全局配置 ```xml               ``` 说明: spring头文件需要加入 xmlns:cjava="http://www.haoyu.com/schema/cjava" xsi:schemaLocation中需要加入 http://www.haoyu.com/schema/cjava http://www.haoyu.com/schema/cjava/cjava-1.0.xsd 对自定义cjava标签支持。 2、结构体协议配置(举例) ```xml ``` 注:返回给C结构的协议类同参考包下面的spring-cjava-tc.xml 3、自定义业务实现(举例,对特征值解析以后的处理) ```java package com.haoyu.module.jcstruct.test.handle; import java.util.Date; import org.apache.log4j.Logger; import org.springframework.stereotype.Service; import com.alibaba.fastjson.JSONObject; import com.haoyu.module.jcstruct.annotation.HandleMessageProtocol; import com.haoyu.module.jcstruct.handle.HandleMessageService; import com.haoyu.module.jcstruct.model.DefaultResponseResult; /** * 处理特征值显示 * * @author haoyu * */ //spring注解 @Service("handleCharacterMessage") //待处理的协议注解,解析出数据后的主键KEY(协议号标识)与根据此注解id匹配,如果匹配成功,在此做协议的处理并返回处理结果 @HandleMessageProtocol(id = GatewayServerCodeType.Gateway_Server_Character) //处理类必须要实现HandleMessageService接口 public class HandleCharacterMessage extends AbstractHandleMessageService implements HandleMessageService { Logger LOG = Logger.getLogger(this.getClass()); // @Autowired // private ReceiveDataService receiveDataService; @Override public void handle(JSONObject message, DefaultResponseResult responseResult) { //LOG.info("接收特征值信息:" + message); System.out.println("接收特征值信息:" + message); // 往趋势图加入数据 try { String Sensor_Id = message.getLong("Sensor_Id").toString(); // 获取温度 float Temperature = message.getFloatValue("Temperature"); // 获取电量 int Battery = message.getIntValue("Battery"); // 接收端数据= 数据/Data_coefficient float Data_coefficient = message.getIntValue("Data_coefficient"); float Data_x_Rms = message.getIntValue("Data_x_Rms") / Data_coefficient; float Data_x_PP = message.getIntValue("Data_x_PP") / Data_coefficient; float Data_x_P = message.getIntValue("Data_x_P") / Data_coefficient; float Data_y_Rms = message.getIntValue("Data_y_Rms") / Data_coefficient; float Data_y_PP = message.getIntValue("Data_y_PP") / Data_coefficient; float Data_y_P = message.getIntValue("Data_y_P") / Data_coefficient; float Data_z_Rms = message.getIntValue("Data_z_Rms") / Data_coefficient; float Data_z_PP = message.getIntValue("Data_z_PP") / Data_coefficient; float Data_z_P = message.getIntValue("Data_z_P") / Data_coefficient; JSONObject receiveData = new JSONObject(); receiveData.put("temperature", Temperature); receiveData.put("battery", Battery); receiveData.put("datatime", System.currentTimeMillis()); LOG.info("特征值上传时间:mills:" + receiveData.get("datatime") + "->times:" + new Date()); // 用于判断是否为速度、加速度 int type = message.getIntValue("Character_Attribute"); receiveData.put("dataType", type); if (type == 1) { // 加速度 receiveData.put("xValue", Data_x_P); receiveData.put("yValue", Data_y_P); receiveData.put("zValue", Data_z_P); } else { // 速度 receiveData.put("xValue", Data_x_Rms); receiveData.put("yValue", Data_y_Rms); receiveData.put("zValue", Data_z_Rms); } // 调用接口 //receiveDataService.receiveFromSimulative(ReceiveDataService.HIS_DATA, Sensor_Id, receiveData); System.out.println("处理解析特征值上传信息成功,待上传的数据信息为" + receiveData); buildDefaultSuccess(message, responseResult); } catch (Exception e) { LOG.error("处理接收后的特征值信息接口异常:" + e.getMessage(), e); buildDefaultFail(message, responseResult); } } } ``` 4、测试代码 ```java package com.haoyu.module.jcstruct; import java.io.IOException; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.alibaba.fastjson.JSONObject; import com.haoyu.module.jcstruct.common.BaseDataType; import com.haoyu.module.jcstruct.dispatch.DispatchCenterService; import com.haoyu.module.jcstruct.exception.DtuMessageException; import com.haoyu.module.jcstruct.model.ResolveResult; import com.haoyu.module.jcstruct.model.ResponseResult; import com.haoyu.module.jcstruct.opt.JFieldOpt; import com.haoyu.module.jcstruct.opt.OptManager; import com.haoyu.module.jcstruct.resolve.DefaultResolve; import com.haoyu.module.jcstruct.template.TemplateContainer; import com.haoyu.module.jcstruct.test.handle.GatewayServerCodeType; import com.haoyu.module.jcstruct.utils.HexUtils; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-cjava.xml") public class AppTest { @Autowired private TemplateContainer templateContainer; @Autowired private OptManager optManager; @Autowired private DispatchCenterService dispatchCenterService; @Autowired private DefaultResolve defaultResolve; // 测试解析特征值,协议号0x02 private void testCharacterResolve() throws DtuMessageException, IOException { byte[] data = new byte[] { -86, -86, -3, 79, -6, -119, 2, 18, -85, -51, -17, 1, 0, 31, 101, -61, 3, 83, 0, 10, -26, 2, 0, 10, 1, 87, 6, 69, 3, -80, 0, -18, 3, 98, 1, -59, 1, -114, 7, 66, 4, 104, 31, -86, 85 }; ResolveResult json = defaultResolve.resolve(data, null); // 处理特征值 ResponseResult responseResult = dispatchCenterService.handle(json.getId(), json.getResult()); byte[] rtnData = defaultResolve.unresolve(responseResult.getId(), responseResult.getData()); System.out.println("处理特征值以后返回给C结构数据为:" + HexUtils.bytesToHexString(rtnData)); } // 测试解析波形,协议号0x04 private void testWaveResolve() throws DtuMessageException, IOException { // 申请上传波形 byte[] data = new byte[] { -86, -86, -3, 79, -6, -119, 4, 18, -85, -51, -17, 1, 0, 14, 102, 72, 4, 2, 1, 10, -26, 0, 0, 0, 100, -16, -86, 85 }; ResolveResult json = defaultResolve.resolve(data, null); System.out.println(json.getId() + "->" + json.getResult()); // 中间波形 data = new byte[] { -86, -86, -3, 79, -6, -119, 4, 18, -85, -51, -17, 1, 4, 6, 102, 73, 5, -17, -61, -18, -5, -10, -71, 4, -94, 16, 30, 22, 14, 22, 110, 19, 119, 15, 67, 6, 111, -1, 85, -4, 72, -4, 89, 5, 64, 13, -15, 17, -89, 13, -29, 6, 66, -2, -55, -10, 59, -17, -103, -30, 108, -38, -22, -33, 29, -19, -62, -3, 42, 8, 94, 6, -13, 5, -79, 4, -78, 2, -50, -6, -127, -14, -21, -16, 114, -6, 46, 7, 56, 16, 62, 18, 93, 13, -3, 8, -15, 1, 66, -6, -76, 0, 46, -3, -57, -5, -17, 2, 40, 13, -116, 21, -63, 23, -82, 18, 19, 12, 49, 6, 57, -4, 30, -6, 13, -10, 35, -8, -37, 3, 97, 11, 118, 14, -83, 9, 54, 2, 122, -6, -83, -13, 64, -20, -8, -27, -7, -28, 14, -22, -64, -7, 83, 5, 114, 13, 75, 13, -11, 12, -39, 10, 1, 3, 121, -7, 74, -11, -37, -11, -20, -1, -35, 10, -96, 16, 123, 14, -8, 8, -117, 2, 3, -7, -24, -13, -54, -25, -37, -34, 73, -32, 72, -19, 69, -3, 88, 8, -16, 8, -75, 5, 110, 4, 56, 2, -83, -5, 1, -15, 89, -19, 56, -11, 117, 3, -96, 13, -25, 18, 114, 14, -39, 10, -91, 3, -121, -4, -78, -6, -22, -8, -118, -10, 101, -3, 39, 9, -96, 19, -75, 24, 35, 21, -46, 17, 15, 11, -84, 2, 47, 0, -125, -3, 91, -3, -90, 6, 58, 14, 76, 17, 68, 12, -102, 4, -78, -4, -53, -12, -87, -19, -30, -29, -50, -33, 50, -28, 91, -14, -34, 0, 88, 9, -51, 8, 63, 7, 49, 5, 103, 2, 20, -8, -4, -13, 40, -14, 21, -5, -57, 7, -9, 15, -5, 17, 6, 12, 15, 6, -125, -2, -88, -8, 91, -3, -97, -7, 12, -9, -79, -2, -40, 10, -84, 18, -29, 19, -19, 13, -79, 7, -12, 2, -94, -8, -28, -11, 70, -15, 11, -11, 31, 0, -20, 9, -49, 13, -32, 8, -61, 2, -15, -5, 98, -12, 78, -18, 80, -22, -105, -23, -123, -16, -128, -2, -113, 10, 21, 17, 11, 17, -112, 15, -65, 12, 78, 4, -104, -7, -12, -10, 53, -10, 110, 0, 43, 10, 109, 15, -39, 13, -26, 7, 55, 0, -108, -8, 125, -14, 70, -27, -47, -36, -95, -33, 52, -20, -29, -3, 3, 8, -106, 8, 19, 4, -2, 3, -82, 2, 35, -6, 103, -15, 22, -19, 34, -11, -71, 3, -63, 13, -12, 18, 102, 14, -54, 10, -107, 3, -94, -3, 0, -1, -93, -3, -121, -5, 1, 0, -59, 12, 78, 21, -91, 25, 24, 21, -77, 16, 55, 10, 121, 0, -97, -2, -69, -5, -127, -4, 49, 4, -21, 12, -2, 15, -10, 11, 75, 3, 114, -5, -18, -13, -13, -19, 80, -29, 31, -34, -58, -28, 32, -14, -37, 0, 123, 9, -22, 8, 51, 6, -56, 4, -65, 1, 104, -8, -113, -14, -44, -15, -57, -5, -86, 8, 2, 16, 47, 17, 38, 11, -15, 6, 44, -2, 125, -8, 31, -4, 55, -9, 10, -11, -62, -3, 100, 9, -85, 18, 85, 19, -100, 13, 94, 7, -8, 2, -63, -7, 84, -14, -65, -19, 127, -15, -62, -2, 102, 8, 98, 13, 66, 8, -13, 3, -122, -4, 87, -11, -92, -17, -46, -20, 105, -21, 69, -14, 67, 0, 21, 11, -112, 18, 97, 18, -89, 16, -90, 13, 9, 5, -124, -6, -50, -10, -23, -10, -27, 0, 121, 10, -35, 16, 116, 14, -70, 8, 8, 1, 102, -7, 50, -14, -41, -25, 116, -34, 59, -32, 50, -19, 59, -3, 62, 8, -81, 8, -82, 5, 1, 3, 53, 1, -111, -7, -16, -16, -12, -20, 106, -12, 106, 2, -115, 12, -27, 17, -81, 14, 41, 9, -9, 3, 28, -4, -77, -5, 118, -8, -9, -10, -38, -3, 54, 9, -84, 19, -22, 24, -128, 22, -39, 18, 109, 13, 37, 3, -93, -3, 2, -6, 12, -6, -65, 3, -64, 12, 97, 15, -40, 11, -118, 4, 12, -4, -8, -11, 55, -18, -42, -30, 13, -37, -115, -32, 86, -17, 98, -2, 59, 8, -46, 7, 10, 5, 90, 4, 57, 2, 8, -7, -20, -14, 49, -16, 7, -7, -13, 7, 20, 15, -7, 18, 33, 13, -107, 8, 79, 0, -26, -6, -110, 0, 69, -4, 50, -6, 124, 1, 39, 12, -46, 21, 111, 23, 81, 17, 106, 11, -116, 5, -93, -5, -104, -6, 25, -10, -109, -7, 56, 3, -66, 12, 41, 15, 108, 9, -6, 3, 1, -5, 53, -13, -76, -19, 89, -25, -81, -26, 20, -20, 120, -6, -71, 6, -104, 14, 55, 13, -10, 12, -121, 9, 109, 3, 30, -8, -14, -12, 111, -12, 83, -2, 74, 9, 97, 15, -7, 15, 24, 8, -43, 2, 118, -6, 69, -12, 19, -23, 24, -33, -99, -31, 53, -19, -20, -3, -26, 9, 103, 10, 3, 6, 29, 3, -5, 1, -40, -6, 57, -15, 1, -20, 8, -13, -122, 1, -64, 12, -121, 17, -67, 13, -24, 9, -110, 2, -74, -4, 27, -6, -27, -8, -128, -10, 43, -4, 86, 8, -104, 19, 45, 24, 65, 23, 45, 19, 26, 14, 12, 4, -102, -3, 98, -6, 4, -6, 108, 3, 102, 12, 75, 15, -17, 12, 46, 4, -85, -3, -120, -11, -81, -17, 99, -31, -16, -38, -85, -33, 60, -18, 43, -3, 117, 8, -121, 6, -28, 5, 49, 4, 58, 2, 95, -6, 124, -14, 36, -17, -115, -7, 55, 6, -112, 15, -57, 18, 76, 14, 54, 9, 83, 1, -53, -5, 89, 1, 71, -3, 120, -5, 109, 1, -57, 13, 97, 21, -17, 23, -46, 18, 56, 12, 95, 6, 126, -4, 29, -5, 5, -8, 9, -6, -111, 4, -59, 12, -112, 15, 125, 9, -50, 2, -56, -6, -21, -13, 58, -20, -33, -27, -117, -29, 103, -23, -42, -8, 82, 4, -77, 12, -50, 12, -52, 11, -88, 9, 4, 2, -23, -8, -41, -12, 1, -12, 4, -2, 49, 9, 124, 16, 8, 15, 21, 8, -26, 2, -87, -6, 76, -37, -86, 85 }; json = defaultResolve.resolve(data, null); System.out.println(json.getId() + "->" + json.getResult()); // 最后一组上传波形 data = new byte[] { -86, -86, -3, 79, -6, -119, 4, 18, -85, -51, -17, 1, 0, 6, 102, 83, 6, 106, -86, 85 }; json = defaultResolve.resolve(data, null); System.out.println(json.getId() + "->" + json.getResult()); } // 测试解析确认信息协议,协议号0x01 private void testACKResolve() throws DtuMessageException, IOException { byte[] data = new byte[] { -86, -86, -3, 79, -6, -119, 2, 18, -85, -51, -17, 1, 0, 31, 101, -61, 3, 83, 0, 10, -26, 2, 0, 10, 1, 87, 6, 69, 3, -80, 0, -18, 3, 98, 1, -59, 1, -114, 7, 66, 4, 104, 31, -86, 85 }; ResolveResult json = defaultResolve.resolve(data, null); System.out.println(json.getId() + "->" + json.getResult()); } // 测试解析心跳信息协议,协议号0x05 private void testHeartBeatResolve() throws DtuMessageException, IOException { byte[] data = new byte[] { -86, -86, -3, 79, -6, -119, 2, 18, -85, -51, -17, 1, 0, 31, 101, -61, 3, 83, 0, 10, -26, 2, 0, 10, 1, 87, 6, 69, 3, -80, 0, -18, 3, 98, 1, -59, 1, -114, 7, 66, 4, 104, 31, -86, 85 }; ResolveResult json = defaultResolve.resolve(data, null); System.out.println(json.getId() + "->" + json.getResult()); } // 测试解析组网上传协议,协议号0x03 private void testSensorResolve() throws DtuMessageException, IOException { byte[] data = new byte[] { -86, -86, -3, 79, -6, -119, 2, 18, -85, -51, -17, 1, 0, 31, 101, -61, 3, 83, 0, 10, -26, 2, 0, 10, 1, 87, 6, 69, 3, -80, 0, -18, 3, 98, 1, -59, 1, -114, 7, 66, 4, 104, 31, -86, 85 }; ResolveResult json = defaultResolve.resolve(data, null); System.out.println(json.getId() + "->" + json.getResult()); } // 测试反解析 private void testUnResolve() throws DtuMessageException, IOException { // 反解析 JSONObject ack = new JSONObject(); ack.put("Package_Type", 1); ack.put("Gateway_Id", 4249877129L); ack.put("Package_Number", 0); ack.put("command_properties", 2); ack.put("BCC", 1); byte[] data = defaultResolve.unresolve("1", ack); // [85, 85, -3, 79, -6, -119, 1, 0, 6, 0, 0, 2, 1, 85, -86] // 打印16进制 System.out.println(HexUtils.bytesToHexString(data)); } // 测试反解析下发配置 private void testUnResolveSensorConfig() throws DtuMessageException, IOException { String s = "{\"Gateway_Id\":\"4249877129\",\"Upload_Sensor_Type\":\"9\",\"Sensor_Id\":\"313249263\"," + "\"HP_Filter\":\"2\",\"X_Angle\":\"0\",\"Sample_Character_Time\":\"1\",\"Upload_Wave_Type\":\"113\"," + "\"Upload_Character_Time\":\"1\",\"Upload_Wave_Time\":\"2\",\"Wave_long\":\"1\",\"Sample_Frequency\":\"800\"," + "\"Temperature_alarm_diff\":\"35\",\"Temperature_alarm_HH\":\"80\",\"Temperature_alarm_Change\":\"5\"," + "\"Acc_X_alarm_HH\":\"1\",\"Acc_X_alarm_H\":\"0.8\",\"Acc_Y_alarm_HH\":\"1\",\"Acc_Y_alarm_H\":\"0.8\"," + "\"Acc_Z_alarm_HH\":\"1\",\"Acc_Z_alarm_H\":\"0.8\",\"Speed_X_alarm_HH\":\"1\",\"Speed_X_alarm_H\":\"0.8\"," + "\"Speed_Y_alarm_HH\":\"1\",\"Speed_Y_alarm_H\":\"0.8\",\"Speed_Z_alarm_HH\":\"1\",\"Speed_Z_alarm_H\":\"0.8\"}"; JSONObject sensorConfig = JSONObject.parseObject(s); sensorConfig.put("Package_Type", 6); sensorConfig.put("Package_Number", 0); sensorConfig.put("BCC", 1); byte[] data = defaultResolve.unresolve("6", sensorConfig); System.out.println(HexUtils.bytesToHexString(data)); } @Test public void test() { try { testCharacterResolve(); testWaveResolve(); testUnResolve(); testUnResolveSensorConfig(); JSONObject content = new JSONObject(); content.put("Constant_Up", 43690); content.put("Gateway_Id", 35526545); content.put("Package_Type", 5); content.put("Package_Number", 13); content.put("BCC", 1); content.put("Constant_Up_Stop", 43605); // 处理心跳业务 ResponseResult result = dispatchCenterService.handle(GatewayServerCodeType.Gateway_Server_HeartBeat, content); // 返回下发C结构体二进制 byte[] resultData = this.defaultResolve.unresolve(result.getId(), result.getData()); System.out.println(HexUtils.bytesToHexString(resultData)); } catch (Exception e) { e.printStackTrace(); } } private void testother() { // 获取读取BYTEARRAY类的操作类 JFieldOpt jReadArray = optManager.getJFieldOpt(BaseDataType.BYTEARRAY); try { // 调用read方法 jReadArray.read(null, null, 512); } catch (IOException e) { e.printStackTrace(); } // 获取读取SHORTARRAY类的操作类 JFieldOpt jReadShortArray = optManager.getJFieldOpt(BaseDataType.SHORTARRAY); try { jReadShortArray.read(null, null, 520); } catch (IOException e) { e.printStackTrace(); } // 根据返回C协议号ID获取模板类 Template tm = templateContainer.getFCTemplate("1"); System.out.println(tm.toString()); // 测试处理业务类 JSONObject content = new JSONObject(); dispatchCenterService.handle("5", content); } } ```