java netty jt808 jt-808 JT/T808-2001 jt/t808-2001 部标协议 协议解析 java代码实现篇

一款简单JT-808解析项目

之前写过一篇关于jt-808解析博客,https://blog.csdn.net/hncdyj/article/details/89494209

有很多同学很感兴趣,这次给大家开源一款java版本解析jt808协议的工程。

你需要的知识储备

  • 需要了解什么是二进制,十进制,十六进制。
  • 需要了解java的基础数据结构,基本数据类型占用的字节大小。(如:short占用2个字节)
  • 需要会基本的linux操作,会安装redis,rabbitmq,mongodb,mysql。
  • 基础的java环境搭建,JDK8+、Maven、git、IDEA等环境。
  • 项目无vip版本,仅个人爱好开发,实现登录、鉴权、心跳、位置解析,扩展简单。
  • 此架构接入过1W+的设备,以前单机最大4G内存压测过5W+的TCP连接,无接入压力。
  • 有偿接协议解析,也欢迎大家补充协议我会合并到主干分支上。
  • 作者大部分协议都能解析,HJ212-2017、博实结、铁塔换电柜、电池私有BMS协议等。(只要有文档都能搞定)
  • 另外不要想着几千块能搞定一整个平台,不可能的。
  • 如果此项目对你有帮助,请给个star鼓励下作者吧,谢谢。

jzx-jt808项目介绍

JT808全称JT/T808协议,协议共有三个版本(JT/T808-2011,JT/T808-2013,JT/T808-2019)

模块介绍

.
|——jzx-jt808-command        # 服务端指令模块(配置下发等)
|——jzx-jt808-common         # 公共模块(常量、枚举、协议类、工具类等)
|——jzx-jt808-dispatch       # 负载均衡模块(http负载、tcp负载、设备日志查询等)
|——jzx-jt808-hardware       # 设备接入模块(启动即可接入硬件)
|——jzx-jt808-process        # 设备数据消费模块(处理硬件数据)
|——jzx-jt808-service        # 基础服务模块(包含数据库、mongo等)
   |--jzx-jt808-service-db       # 数据库操作公共项目(自己集成mybatis、plus、jpa等)
   |--jzx-jt808-service-mongo    # mongodb操作公共项目
|——jzx-jt808-web            # web服务模块(包含指令下发、自定义web服务[自己实现])

中间件安装

redis     安装参考:https://blog.csdn.net/hncdyj/article/details/136527560
rabbitmq  安装参考:https://blog.csdn.net/hncdyj/article/details/136528495
mongodb   安装参考:https://blog.csdn.net/hncdyj/article/details/136529032
mysql     安装参考:https://blog.csdn.net/hncdyj/article/details/136529343

依赖中间件

redis     version >= 中间件安装版本即可
rabbitmq  version >= 中间件安装版本即可
mongodb   version >= 中间件安装版本即可
mysql     version >= 中间件安装版本即可
maven     version >= 中间件安装版本即可

QuickStart

1.拉取代码

git clone https://gitee.com/hncdyj/jzx-jt808.git
or
git clone https://github.com/hncdyj123/jzx-jt808.git

2.项目打包

2.1 在在项目根路径下执行
mvn clean package
or
mvn clean package -U
2.2 进入到jzx-jt808-hardware/target文件夹下

会看到jzx-jt808-hardware-assembly.zip压缩包

2.3 进入到jzx-jt808-process/target文件夹下

会看到jzx-jt808-process-assembly.zip压缩包

2.4 进入到jzx-jt808-dispatch/target文件夹下

会看到jzx-jt808-dispatch-assembly.zip压缩包

2.5 进入到jzx-jt808-web/target文件夹下

会看到jzx-jt808-web-assembly.zip压缩包

3.启动项目(linux为样例)

3.1 接入服务(jzx-jt808-hardware)

上传项目包后解压jzx-jt808-hardware-assembly.zip压缩包

tar -zxvf jzx-jt808-hardware-assembly.zip
cd jzx-jt808-hardware

会看下如下路径:

.
|——conf                     # 配置文件目录
|——lib                      # 依赖jar包
|——jzx-jt808-hardware.jar   # 项目启动jar包
|——springboot-start.sh      # linux|unix|macos启动脚本
|——springboot-start.cmd     # window启动脚本

修改配置文件

# 自定义业务配置相关
business:
  netty:
    # netty启动监听端口
    port: 3096
  # 集群配置,单点请置空
  # 集群配置说明xx1(接入点1外网地址) yy1(接入点1别名) zz1(接入点域名或者ip)
  # 以下配置中ip为虚构,如有雷同纯属巧合
  # 示例:domainMap: "{'hd01':'52.12.45.85|hd01.example.com','hd02':'52.12.45.84|hd02.example.com'}"
  domainMap: "{'xx1':'yy1|zz1','xx2':'yy2|zz2'}"

spring:
  datasource:
    # 数据库用户名 需要修改
    username: "xx"
    # 数据库密码 需要修改
    password: "xx"
    # 数据库驱动类 需要修改
    driver-class-name: "org.mariadb.jdbc.Driver"
    # 数据库连接URI 需要修改
    url: "jdbc:mysql://ip:port/db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"
    type: "com.alibaba.druid.pool.DruidDataSource"
    initial-size: 5
    max-active: 20
    min-idle: 5
    max-wait: 60000
    min-evictable-idle-time-millis: 300000
    max-evictable-idle-time-millis: 300000
    time-between-eviction-runs-millis: 60000
    validation-query: select 1
    validation-query-timeout: -1
    test-on-borrow: false
    test-on-return: false
    test-while-idle: true
    pool-prepared-statements: true
    filters: stat,wall,log4j
    share-prepared-statements: true
  # 主redis配置相关(用于本地session缓存、指令下发等)
  redis:
    primary:
      # 连接地址 需要修改
      host: "xx"
      # 连接端口
      port: 6379
      # 连接密码(生产环境必须开启,防止被攻击) 需要修改
      # password: ""
      # 连接超时时间
      timeout: 2000
      # 使用数据库
      database: 2
      # 连接池最大连接数(使用负值表示没有限制)
      pool-max-active: 10
      # 连接池中的最大空闲连接
      pool-max-idle: 5
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      pool-max-wait: 2000
      # 连接池中的最小空闲连接
      pool-min-idle: 5
    # 从redis配置相关
    secondary:
      # 连接地址 需要修改
      host: "xx"
      # 连接端口
      port: 6379
      # 连接密码(生产环境必须开启,防止被攻击) 需要修改
      # password: ""
      # 连接超时时间
      timeout: 2000
      # 使用数据库
      database: 2
      # 连接池最大连接数(使用负值表示没有限制)
      pool-max-active: 10
      # 连接池中的最大空闲连接
      pool-max-idle: 5
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      pool-max-wait: 2000
      # 连接池中的最小空闲连接
      pool-min-idle: 5

  # rabbitmq config
  rabbitmq:
    # 业务rabbitmq配置
    business:
      # ip地址 需要修改
      host: "xx"
      # 用户名 需要修改
      username: "xx"
      # 密码 需要修改
      password: "xx"
      port: 5672
      virtual-host: /
    # 日志收集rabbitmq配置
    logs:
      # ip地址 需要修改
      host: "xx"
      # 用户名 需要修改
      username: "xx"
      # 密码 需要修改
      password: "xx"
      port: 5672
      virtual-host: /
      
  # mongodb多数据源配置
  mongodb:
    # 主mongo配置
    primary:
      # mongo连接URI 需要修改
      uri: "mongodb://ip:port/Business"
      # 
      database: "dbname"
    secondary:
      # mongo连接URI 需要修改
      uri: "mongodb://ip:port/Logs"
      database: "dbname"

启动项目

# 转换脚本 && 添加脚本权限
dos2unix *.sh && chmod +x springboot-start.sh
# 启动项目并指定配置环境(根据conf目录下的xml后缀)
### 启动默认环境
./springboot-start.sh start default
### 启动开发环境
./springboot-start.sh start dev
### 启动测试环境
./springboot-start.sh start test
### 启动测试环境
./springboot-start.sh start prd
3.2 数据消费服务(jzx-jt808-process)

上传项目包后解压jzx-jt808-process-assembly.zip压缩包

tar -zxvf jzx-jt808-process-assembly.zip
cd jzx-jt808-process

会看下如下路径:

.
|——conf                     # 配置文件目录
|——lib                      # 依赖jar包
|——jzx-jt808-process.jar   # 项目启动jar包
|——springboot-start.sh      # linux|unix|macos启动脚本
|——springboot-start.cmd     # window启动脚本

修改配置文件

spring:
  datasource:
    # 数据库用户名 需要修改
    username: "xx"
    # 数据库密码 需要修改
    password: "xx"
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 数据库地址 需要修改
    url: "jdbc:mysql://ip:port/db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"
    type: com.alibaba.druid.pool.DruidDataSource
    initial-size: 5
    max-active: 20
    min-idle: 5
    max-wait: 60000
    min-evictable-idle-time-millis: 300000
    max-evictable-idle-time-millis: 300000
    time-between-eviction-runs-millis: 60000
    validation-query: select 1
    validation-query-timeout: -1
    test-on-borrow: false
    test-on-return: false
    test-while-idle: true
    pool-prepared-statements: true
    filters: stat,wall,log4j
    share-prepared-statements: true

  # rabbitmq config
  rabbitmq:
    business:
      # ip地址 需要修改
      host: "xx"
      # 密码 需要修改
      password: "xx"
      port: 5672
      username: root
      virtual-host: /
    logs:
      # ip地址 需要修改
      host: "xx"
      # 密码 需要修改
      password: "xx"
      port: 5672
      username: root
      virtual-host: /

  # mongodb多数据源配置
  mongodb:
    # 主mongo配置
    primary:
      # mongo连接URI 需要修改
      uri: "mongodb://ip:port/Business"
      # 
      database: "dbname"
    secondary:
      # mongo连接URI 需要修改
      uri: "mongodb://ip:port/Logs"
      database: "dbname"

启动项目

# 转换脚本 && 添加脚本权限
dos2unix *.sh && chmod +x springboot-start.sh
# 启动项目并指定配置环境(根据conf目录下的xml后缀)
### 启动默认环境
./springboot-start.sh start default
### 启动开发环境
./springboot-start.sh start dev
### 启动测试环境
./springboot-start.sh start test
### 启动测试环境
./springboot-start.sh start prd
3.3 指令下发服务(jzx-jt808-web)

上传项目包后解压jzx-jt808-web-assembly.zip压缩包

tar -zxvf jzx-jt808-web-assembly.zip
cd jzx-jt808-web

会看下如下路径:

.
|——conf                     # 配置文件目录
|——lib                      # 依赖jar包
|——jzx-jt808-web.jar   # 项目启动jar包
|——springboot-start.sh      # linux|unix|macos启动脚本
|——springboot-start.cmd     # window启动脚本

修改配置文件

spring:
  datasource:
    # 数据库用户名 需要修改
    username: "xx"
    # 数据库密码 需要修改
    password: "xx"
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 数据库地址 需要修改
    url: "jdbc:mysql://ip:port/db?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"
    type: com.alibaba.druid.pool.DruidDataSource
    initial-size: 5
    max-active: 20
    min-idle: 5
    max-wait: 60000
    min-evictable-idle-time-millis: 300000
    max-evictable-idle-time-millis: 300000
    time-between-eviction-runs-millis: 60000
    validation-query: select 1
    validation-query-timeout: -1
    test-on-borrow: false
    test-on-return: false
    test-while-idle: true
    pool-prepared-statements: true
    filters: stat,wall,log4j
    share-prepared-statements: true
    
  # 主redis配置相关(用于本地session缓存、指令下发等)
  redis:
    primary:
      # 连接地址 需要修改
      host: "xx"
      # 连接端口
      port: 6379
      # 连接密码(生产环境必须开启,防止被攻击) 需要修改
      # password: ""
      # 连接超时时间
      timeout: 2000
      # 使用数据库
      database: 2
      # 连接池最大连接数(使用负值表示没有限制)
      pool-max-active: 10
      # 连接池中的最大空闲连接
      pool-max-idle: 5
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      pool-max-wait: 2000
      # 连接池中的最小空闲连接
      pool-min-idle: 5
    # 从redis配置相关
    secondary:
      # 连接地址 需要修改
      host: "xx"
      # 连接端口
      port: 6379
      # 连接密码(生产环境必须开启,防止被攻击) 需要修改
      # password: ""
      # 连接超时时间
      timeout: 2000
      # 使用数据库
      database: 2
      # 连接池最大连接数(使用负值表示没有限制)
      pool-max-active: 10
      # 连接池中的最大空闲连接
      pool-max-idle: 5
      # 连接池最大阻塞等待时间(使用负值表示没有限制)
      pool-max-wait: 2000
      # 连接池中的最小空闲连接
      pool-min-idle: 5

  # rabbitmq config
  rabbitmq:
    business:
      # ip地址 需要修改
      host: "xx"
      # 密码 需要修改
      password: "xx"
      port: 5672
      username: root
      virtual-host: /
    logs:
      # ip地址 需要修改
      host: "xx"
      # 密码 需要修改
      password: "xx"
      port: 5672
      username: root
      virtual-host: /

  # mongodb多数据源配置
  mongodb:
    # 主mongo配置
    primary:
      # mongo连接URI 需要修改
      uri: "mongodb://ip:port/Business"
      # 
      database: "dbname"
    secondary:
      # mongo连接URI 需要修改
      uri: "mongodb://ip:port/Logs"
      database: "dbname"

启动项目

# 转换脚本 && 添加脚本权限
dos2unix *.sh && chmod +x springboot-start.sh
# 启动项目并指定配置环境(根据conf目录下的xml后缀)
### 启动默认环境
./springboot-start.sh start default
### 启动开发环境
./springboot-start.sh start dev
### 启动测试环境
./springboot-start.sh start test
### 启动测试环境
./springboot-start.sh start prd

流程参考

请拉取项目后查看项目中doc描述

有偿可接各种硬件协议开发
协议描述相关:https://blog.csdn.net/hncdyj/article/details/89494209
转载请标明出处:https://blog.csdn.net/hncdyj/

  • 22
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JT808 是一种车载终端通信协议,下面是一个简单的 Java 实现。 ```java import java.nio.ByteBuffer; public class JT808Decoder { public static void main(String[] args) { byte[] data = new byte[]{ 0x7e, 0x02, 0x00, 0x23, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x3b, 0x7e }; JT808Decoder decoder = new JT808Decoder(); decoder.decode(data); } public void decode(byte[] data) { ByteBuffer buffer = ByteBuffer.wrap(data); byte start = buffer.get(); byte msgId1 = buffer.get(); byte msgId2 = buffer.get(); int msgId = ((msgId1 & 0xff) << 8) | (msgId2 & 0xff); int msgBodyProps = buffer.getShort() & 0xffff; int msgBodyLength = msgBodyProps & 0x3ff; boolean hasSubPackage = ((msgBodyProps >> 13) & 0x1) == 1; int packageInfoField = ((msgBodyProps >> 10) & 0x7); int msgBodyStartIndex = buffer.position(); byte[] msgBody = new byte[msgBodyLength]; buffer.get(msgBody); int checkSum = buffer.get() & 0xff; byte end = buffer.get(); if (end != 0x7e) { throw new IllegalArgumentException("Invalid message format"); } System.out.println("msgId: " + msgId); System.out.println("msgBodyProps: " + msgBodyProps); System.out.println("hasSubPackage: " + hasSubPackage); System.out.println("packageInfoField: " + packageInfoField); System.out.println("msgBody: " + new String(msgBody)); System.out.println("checkSum: " + checkSum); } } ``` 这个实现只是一个简单的示例,实际解析 JT808 协议需要更多的细节处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值