diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..940794e --- /dev/null +++ b/.gitignore @@ -0,0 +1,288 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c0783ad --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 SmallChi + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第1部分:总则.pdf b/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第1部分:总则.pdf new file mode 100644 index 0000000..d3b4384 Binary files /dev/null and b/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第1部分:总则.pdf differ diff --git a/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第2部分:车载终端.pdf b/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第2部分:车载终端.pdf new file mode 100644 index 0000000..fb07c21 Binary files /dev/null and b/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第2部分:车载终端.pdf differ diff --git a/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第3部分:通信协议及数据格式.pdf b/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第3部分:通信协议及数据格式.pdf new file mode 100644 index 0000000..6a20c44 Binary files /dev/null and b/doc/GBT32960-2016电动汽车远程服务与管理系统技术规范 第3部分:通信协议及数据格式.pdf differ diff --git a/src/GBNewEnergy.Protocol/BufferedEntityBase.cs b/src/GBNewEnergy.Protocol/BufferedEntityBase.cs new file mode 100644 index 0000000..92e4e27 --- /dev/null +++ b/src/GBNewEnergy.Protocol/BufferedEntityBase.cs @@ -0,0 +1,20 @@ +using System; +using System.IO; + +namespace GBNewEnergy.Protocol +{ + public abstract class BufferedEntityBase : IBuffered, IBuffer + { + public byte[] Buffer { get; protected set; } + + public virtual byte[] ToBuffer() + { + return Buffer; + } + + protected BufferedEntityBase(byte[] buffer) + { + Buffer = buffer; + } + } +} diff --git a/src/GBNewEnergy.Protocol/Enums/AskId.cs b/src/GBNewEnergy.Protocol/Enums/AskId.cs new file mode 100644 index 0000000..ae7b62e --- /dev/null +++ b/src/GBNewEnergy.Protocol/Enums/AskId.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol.Enums +{ + /// + /// 应答标志 + /// + public enum AskId:byte + { + /// + /// 接收到的信息正确 + /// + success=0x01, + /// + /// 设置未成功 + /// + error=0x02, + /// + /// VIN重复错误 + /// + vin_repeat_error=0x03, + /// + /// 数据包为命令包,而非应答包 + /// + cmd=0xFE + } +} diff --git a/src/GBNewEnergy.Protocol/Enums/EncryptMethod.cs b/src/GBNewEnergy.Protocol/Enums/EncryptMethod.cs new file mode 100644 index 0000000..b5ee7ff --- /dev/null +++ b/src/GBNewEnergy.Protocol/Enums/EncryptMethod.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol.Enums +{ + /// + /// 数据单元加密方式 + /// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 + /// + public enum EncryptMethod:byte + { + None= 0x01, + RSA= 0x02, + AES128=0x03, + Exception = 0xFE, + Invalid= 0xFF + } +} diff --git a/src/GBNewEnergy.Protocol/Enums/MsgId.cs b/src/GBNewEnergy.Protocol/Enums/MsgId.cs new file mode 100644 index 0000000..17e7ce4 --- /dev/null +++ b/src/GBNewEnergy.Protocol/Enums/MsgId.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol.Enums +{ + public enum MsgId:byte + { + /// + /// 车辆登入 + /// + login = 0x01, + /// + /// 实时信息上传 + /// + uploadim = 0x02, + /// + /// 心跳 + /// + heartbeat = 0x03, + /// + /// 补传信息上传 + /// + uploadsup = 0x04, + /// + /// 车辆登出 + /// + loginout = 0x05, + /// + /// 终端校时 + /// + checktime = 0x08, + /// + /// 查询应答 + /// + query = 0x80, + /// + /// 设置应答 + /// + settings = 0x81, + /// + /// 控制应答 + /// + control = 0x82 + } +} diff --git a/src/GBNewEnergy.Protocol/Extensions/BinaryExtensions.cs b/src/GBNewEnergy.Protocol/Extensions/BinaryExtensions.cs new file mode 100644 index 0000000..23397d8 --- /dev/null +++ b/src/GBNewEnergy.Protocol/Extensions/BinaryExtensions.cs @@ -0,0 +1,49 @@ +using System; +using System.IO; +using System.Linq; + +namespace GBNewEnergy.Protocol.Extensions +{ + public static class BinaryExtensions + { + + /// + /// 从高位到低位转成int + /// + /// + /// + /// + /// + public static int ToIntH2L(this byte[] bytes, int offset, int len) + { + int result = 0; + for (int i = offset; i < offset + len; i++) + { + result += bytes[i] * (int)Math.Pow(256, len + offset - i - 1); + } + return result; + } + + /// + /// 字节数组转16进制字符串 + /// + /// + /// 默认 " " + /// + public static string ToHexString(this byte[] bytes,string separator=" ") + { + return string.Join(separator, bytes.Select(s => s.ToString("X2"))); + } + + /// + /// 16进制字符串转16进制数组 + /// + /// + /// + /// + public static byte[] ToHexBytes(this string hexString, string separator = " ") + { + return hexString.Split(new string[] { separator }, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToByte(s, 16)).ToArray(); + } + } +} diff --git a/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj new file mode 100644 index 0000000..9f5c4f4 --- /dev/null +++ b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/src/GBNewEnergy.Protocol/IBuffer.cs b/src/GBNewEnergy.Protocol/IBuffer.cs new file mode 100644 index 0000000..559a0e0 --- /dev/null +++ b/src/GBNewEnergy.Protocol/IBuffer.cs @@ -0,0 +1,7 @@ +namespace GBNewEnergy.Protocol +{ + public interface IBuffer + { + byte[] Buffer { get; } + } +} diff --git a/src/GBNewEnergy.Protocol/IBuffered.cs b/src/GBNewEnergy.Protocol/IBuffered.cs new file mode 100644 index 0000000..8ea9cd8 --- /dev/null +++ b/src/GBNewEnergy.Protocol/IBuffered.cs @@ -0,0 +1,7 @@ +namespace GBNewEnergy.Protocol +{ + public interface IBuffered + { + byte[] ToBuffer(); + } +} diff --git a/src/GBNewEnergy.Protocol/NEHeader.cs b/src/GBNewEnergy.Protocol/NEHeader.cs new file mode 100644 index 0000000..ba30b64 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEHeader.cs @@ -0,0 +1,48 @@ +using GBNewEnergy.Protocol.Enums; +using GBNewEnergy.Protocol.Extensions; +using System; +using System.Text; + +namespace GBNewEnergy.Protocol +{ + /// + /// 头部 + /// + public class NEHeader: BufferedEntityBase + { + public NEHeader(byte[] buffer):base(buffer) + { + if (buffer[0] != BeginFlag && buffer[1] == BeginFlag) throw new Exception("error"); + MsgId =(MsgId)buffer[2]; + AskId =(AskId)buffer[3]; + VIN = Encoding.ASCII.GetString(buffer, 4, 17).Trim('\0'); + EncryptMethod =(EncryptMethod)buffer[21]; + DataUnitLength = buffer.ToIntH2L(22, 2); + } + /// + /// 起始符 + /// + public const byte BeginFlag = 0x23; + /// + /// 命令标识 + /// + public MsgId MsgId { get; private set; } + /// + /// 应答标志 + /// + public AskId AskId { get; private set; } + /// + /// 车辆识别码 + /// + public string VIN { get; private set; } + /// + /// 数据加密方式 + /// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 + /// + public EncryptMethod EncryptMethod { get; private set; } + /// + /// 数据单元长度是数据单元的总字节数,有效值范围:0-65531 + /// + public int DataUnitLength { get; private set; } + } +} diff --git a/src/GBNewEnergy.Protocol/NEPackage.cs b/src/GBNewEnergy.Protocol/NEPackage.cs new file mode 100644 index 0000000..5542482 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEPackage.cs @@ -0,0 +1,40 @@ +using GBNewEnergy.Protocol.Enums; +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol +{ + /// + /// 新能源包 + /// + public class NEPackage: BufferedEntityBase + { + public NEPackage(byte[] header,byte[] body):base(body) + { + this.NEHeader = new NEHeader(header); + Array.Copy(header, 0, OriginalPackage, 0, header.Length); + Array.Copy(body, 0, OriginalPackage, header.Length, body.Length); + } + /// + /// 固定为24个字节长度 + /// + public const int HeaderFixedByteLength = 24; + /// + /// 头部 + /// + public NEHeader NEHeader { get; private set; } + /// + /// 数据单元 + /// + public byte[] DataUnit { get; private set; } + /// + /// BCC 校验码 + /// + public byte BCCCode { get; private set; } + /// + /// 原始包(包头+包体) + /// + public byte[] OriginalPackage { get; private set; } + } +} diff --git a/src/GBNewEnergy.sln b/src/GBNewEnergy.sln new file mode 100644 index 0000000..b4b7186 --- /dev/null +++ b/src/GBNewEnergy.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2018 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GBNewEnergy.Protocol", "GBNewEnergy.Protocol\GBNewEnergy.Protocol.csproj", "{1934F3A6-1396-46C2-BFD6-1E2DC1A26E3A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1934F3A6-1396-46C2-BFD6-1E2DC1A26E3A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1934F3A6-1396-46C2-BFD6-1E2DC1A26E3A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1934F3A6-1396-46C2-BFD6-1E2DC1A26E3A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1934F3A6-1396-46C2-BFD6-1E2DC1A26E3A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E7AF20B3-4327-4DF7-B503-9FE08AA92A75} + EndGlobalSection +EndGlobal