java 解析c结构体_GitHub - yanghao0518/cstruct-parser: 提供java与C结构通信过程中结构体解析的方案...

4、测试代码

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 };

Resolve

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言结构体是一种用户定义的数据类型,可以将多个不同类型的变量组合在一起,形成一个单独的数据单元。结构体的定义使用关键字`struct`,并使用大括号`{}`来包围成员变量。 下面是一个示例的结构体定义: ```c struct Person { char name[20]; int age; float height; }; ``` 上述代码定义了一个名为`Person`的结构体,它包含了一个字符数组`name`、一个整型变量`age`和一个浮点型变量`height`。 在使用结构体时,可以通过以下方式声明结构体变量并进行初始化: ```c struct Person p1; // 声明一个Person类型的结构体变量p1 p1.age = 25; // 对结构体成员进行赋值 strcpy(p1.name, "John"); p1.height = 175.5; // 或者可以使用以下方式声明结构体变量并进行初始化 struct Person p2 = {"Alice",30, 162.0}; ``` 通过使用箭头操作符`->`,可以方便地访问结构体指针所指向的成员。例如,如果有一个指向结构体的指针变量`ptr`,可以使用`ptr->member`来访问指针所指向的结构体的成员。 下面是一个使用箭头操作符的示例: ```c struct Person *ptr = &p1; // 将结构体变量p1的地址赋给指针ptr printf("Name: %s\n", ptr->name); // 访问结构体指针ptr所指向的name成员 printf("Age: %d\n", ptr->age); // 访问结构体指针ptr所指向的age成员 printf("Height: %.1f\n", ptr->height); // 访问结构体指针ptr所指向的height成员 ``` 通过箭头操作符`->`可以更方便地操作结构体指针的成员。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值