|
@@ -1,11 +1,10 @@
|
|
-using Simano.Service.Adapter;
|
|
|
|
|
|
+using Simano.Model;
|
|
|
|
+using Simano.Service.Adapter;
|
|
using Simano.Service.CubeProgrammerAPI;
|
|
using Simano.Service.CubeProgrammerAPI;
|
|
using System;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
using System.IO.Ports;
|
|
using System.IO.Ports;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
-using System.Runtime.CompilerServices;
|
|
|
|
-using System.Text;
|
|
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace Simano.Service
|
|
namespace Simano.Service
|
|
@@ -45,20 +44,42 @@ namespace Simano.Service
|
|
|
|
|
|
private SerialPort serialPort;
|
|
private SerialPort serialPort;
|
|
private string portName = string.Empty;
|
|
private string portName = string.Empty;
|
|
- private byte cmdCNT = 0;
|
|
|
|
private bool isApiMode = true;
|
|
private bool isApiMode = true;
|
|
|
|
|
|
|
|
+ private byte _cmdCNT = 0;
|
|
|
|
+ private byte cmdCNT
|
|
|
|
+ {
|
|
|
|
+ get
|
|
|
|
+ {
|
|
|
|
+ var record = _cmdCNT;
|
|
|
|
+ _cmdCNT++;
|
|
|
|
+ if (_cmdCNT >= 4)
|
|
|
|
+ {
|
|
|
|
+ _cmdCNT = 0;
|
|
|
|
+ }
|
|
|
|
+ return record;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
public async Task<bool> Connect(string portName)
|
|
public async Task<bool> Connect(string portName)
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- cmdCNT = 0;
|
|
|
|
|
|
+ _cmdCNT = 0;
|
|
serialPort = new SerialPort(portName, baudRate:115200);
|
|
serialPort = new SerialPort(portName, baudRate:115200);
|
|
serialPort.Open();
|
|
serialPort.Open();
|
|
this.portName = portName;
|
|
this.portName = portName;
|
|
|
|
|
|
ClearReadBuffer();
|
|
ClearReadBuffer();
|
|
var getStatusSuccess = await GetBoardStatus();
|
|
var getStatusSuccess = await GetBoardStatus();
|
|
|
|
+ if (!getStatusSuccess)
|
|
|
|
+ {
|
|
|
|
+ serialPort.Close();
|
|
|
|
+ }
|
|
|
|
+ if (getStatusSuccess && isApiMode)
|
|
|
|
+ {
|
|
|
|
+ var aesVerifySuccess = await AesVerify();
|
|
|
|
+ }
|
|
return getStatusSuccess;
|
|
return getStatusSuccess;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
catch (Exception ex)
|
|
@@ -67,6 +88,19 @@ namespace Simano.Service
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public bool Disconnect()
|
|
|
|
+ {
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ serialPort?.Close();
|
|
|
|
+ }
|
|
|
|
+ catch (Exception ex)
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
private async Task<bool> GetBoardStatus()
|
|
private async Task<bool> GetBoardStatus()
|
|
{
|
|
{
|
|
if (serialPort is null ||
|
|
if (serialPort is null ||
|
|
@@ -75,87 +109,100 @@ namespace Simano.Service
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- var test = serialPort.BytesToRead;
|
|
|
|
-
|
|
|
|
WriteLog(null, "Detecting Board status...");
|
|
WriteLog(null, "Detecting Board status...");
|
|
|
|
|
|
- await Task.Delay(2500);
|
|
|
|
- var canreceivedData = serialPort.BytesToRead > 0;
|
|
|
|
-
|
|
|
|
- if (canreceivedData)
|
|
|
|
|
|
+ SendGetStatusCmd();
|
|
|
|
+ var result = await GetGetStatusRespond();
|
|
|
|
+ if (result is null)
|
|
{
|
|
{
|
|
- var readBuffer = new byte[20];
|
|
|
|
- var readByteCnt = serialPort.Read(readBuffer, 0, 20);
|
|
|
|
-
|
|
|
|
- try
|
|
|
|
- {
|
|
|
|
- var reseivedString = Encoding.UTF8.GetString(readBuffer);
|
|
|
|
- WriteLog(null, reseivedString);
|
|
|
|
- if (reseivedString.Contains("OPEN"))
|
|
|
|
- {
|
|
|
|
- WriteLog(null, "ApiMode");
|
|
|
|
- isApiMode = true;
|
|
|
|
- return true;
|
|
|
|
- }
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- catch
|
|
|
|
- {
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
-
|
|
|
|
- if (!canreceivedData)
|
|
|
|
|
|
+ if (result == 0x89)
|
|
{
|
|
{
|
|
- serialPort.Close();
|
|
|
|
- var openPortResult = await CubeProgrammerAPIWrapper.OpenPort(portName);
|
|
|
|
- if (!openPortResult)
|
|
|
|
- return false;
|
|
|
|
- CubeProgrammerAPIAdapter.ClosePort();
|
|
|
|
- serialPort.Open();
|
|
|
|
|
|
+ WriteLog(null, "ApiMode Detected");
|
|
|
|
+ isApiMode = true;
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ if (result == 0x79 || result == 0x1F)
|
|
|
|
+ {
|
|
|
|
+ WriteLog(null, "BootloadMode Detected");
|
|
isApiMode = false;
|
|
isApiMode = false;
|
|
- WriteLog(null, "BootloaderMode");
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ WriteLog(null, "Not Supported Board!");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
- public bool Disconnect()
|
|
|
|
|
|
+ private async Task<bool> AesVerify()
|
|
{
|
|
{
|
|
- try
|
|
|
|
|
|
+ if (serialPort is null ||
|
|
|
|
+ !serialPort.IsOpen)
|
|
{
|
|
{
|
|
- serialPort?.Close();
|
|
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
- catch (Exception ex)
|
|
|
|
|
|
+
|
|
|
|
+ WriteLog(null, "Starting verify by aes...");
|
|
|
|
+
|
|
|
|
+ var challenge = await GetChallenge();
|
|
|
|
+ if (challenge is null)
|
|
{
|
|
{
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
- return true;
|
|
|
|
|
|
+
|
|
|
|
+ var aesEncodedChallenge = AesEncoder.GetAesEncoded(challenge);
|
|
|
|
+ bool isChallengeSuccess = await RespondChallenge(aesEncodedChallenge);
|
|
|
|
+ if (isChallengeSuccess)
|
|
|
|
+ {
|
|
|
|
+ WriteLog(null, "AES verify success");
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ WriteLog(null, "AES verify FAILED");
|
|
|
|
+ }
|
|
|
|
+ return isChallengeSuccess;
|
|
}
|
|
}
|
|
|
|
|
|
public async Task<bool> FlashFirmware(string filePath)
|
|
public async Task<bool> FlashFirmware(string filePath)
|
|
{
|
|
{
|
|
|
|
+ if (serialPort is null ||
|
|
|
|
+ !serialPort.IsOpen)
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ WriteLog(this, "Starting FlashFirmware");
|
|
|
|
+
|
|
await GetBoardStatus();
|
|
await GetBoardStatus();
|
|
if (isApiMode)
|
|
if (isApiMode)
|
|
{
|
|
{
|
|
- SendFlashFirmwareCmd();
|
|
|
|
- await Task.Delay(3000);
|
|
|
|
- ClearReadBuffer();
|
|
|
|
|
|
+ var changeToBootloadModeResult = await ChangeToBootloadMode();
|
|
|
|
+ if (!changeToBootloadModeResult)
|
|
|
|
+ {
|
|
|
|
+ WriteLog(this, "Change to Bootload FAILED");
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
serialPort.Close();
|
|
serialPort.Close();
|
|
try
|
|
try
|
|
{
|
|
{
|
|
var openPortResult = await CubeProgrammerAPIWrapper.OpenPort(portName);
|
|
var openPortResult = await CubeProgrammerAPIWrapper.OpenPort(portName);
|
|
if (!openPortResult)
|
|
if (!openPortResult)
|
|
|
|
+ {
|
|
|
|
+ WriteLog(this, "CubeProgrammer open port FAILED");
|
|
return false;
|
|
return false;
|
|
|
|
+ }
|
|
//var flshResult = CubeProgrammerAPIAdapter.DownloadFile(filePath, 0x08008000u);
|
|
//var flshResult = CubeProgrammerAPIAdapter.DownloadFile(filePath, 0x08008000u);
|
|
var flshResult = await CubeProgrammerAPIWrapper.DownloadFile(filePath, 0x08008000u);
|
|
var flshResult = await CubeProgrammerAPIWrapper.DownloadFile(filePath, 0x08008000u);
|
|
if (!flshResult)
|
|
if (!flshResult)
|
|
|
|
+ {
|
|
|
|
+ WriteLog(this, "CubeProgrammer download file FAILED");
|
|
return false;
|
|
return false;
|
|
|
|
+ }
|
|
var runResult = await CubeProgrammerAPIWrapper.Run(0x08008000u);
|
|
var runResult = await CubeProgrammerAPIWrapper.Run(0x08008000u);
|
|
if (!runResult)
|
|
if (!runResult)
|
|
|
|
+ {
|
|
|
|
+ WriteLog(this, "CubeProgrammer start run FAILED");
|
|
return false;
|
|
return false;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
finally
|
|
finally
|
|
{
|
|
{
|
|
@@ -165,6 +212,66 @@ namespace Simano.Service
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public Task<byte?> GetFirmwareVersion()
|
|
|
|
+ {
|
|
|
|
+ if (serialPort is null ||
|
|
|
|
+ !serialPort.IsOpen)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ WriteLog(this, "Starting GetFirmwareVersion");
|
|
|
|
+
|
|
|
|
+ SendGetFirmwareVersionCmd();
|
|
|
|
+ return GetGetFirmwareVersionRespond();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Task<byte?> GetEeprom(int address)
|
|
|
|
+ {
|
|
|
|
+ if (serialPort is null ||
|
|
|
|
+ !serialPort.IsOpen)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ WriteLog(this, $"Starting GetEeprom at {address}");
|
|
|
|
+
|
|
|
|
+ SendGetEepromCmd((byte)address);
|
|
|
|
+ return GetGetEepromRespond((byte)address);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Task<bool?> EraseEeprom(int address)
|
|
|
|
+ {
|
|
|
|
+ if (serialPort is null ||
|
|
|
|
+ !serialPort.IsOpen)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ WriteLog(this, $"Starting EraseEeprom at {address}");
|
|
|
|
+
|
|
|
|
+ SendEraseEepromCmd((byte)address);
|
|
|
|
+ return GetEraseEepromRespond((byte)address);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private async Task<byte[]> GetChallenge()
|
|
|
|
+ {
|
|
|
|
+ SendGetChallengeCmd();
|
|
|
|
+ var result = await GetGetChallengeRespond();
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private async Task<bool> RespondChallenge(byte[] aesEncodedChallenge)
|
|
|
|
+ {
|
|
|
|
+ SendChallengedDataCmd(aesEncodedChallenge);
|
|
|
|
+ var result = await GetRespondChallengeRespond();
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private Task<bool> ChangeToBootloadMode()
|
|
|
|
+ {
|
|
|
|
+ SendFlashFirmwareCmd();
|
|
|
|
+ return GetFlashFirmwareCmdRespond();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
private void CubeProgrammerAPIAdapter_OnLogReceived(object sender, string e)
|
|
private void CubeProgrammerAPIAdapter_OnLogReceived(object sender, string e)
|
|
{
|
|
{
|
|
//Console.WriteLine(e);
|
|
//Console.WriteLine(e);
|
|
@@ -176,33 +283,307 @@ namespace Simano.Service
|
|
OnLogReceived?.Invoke(sender, e);
|
|
OnLogReceived?.Invoke(sender, e);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void SendGetStatusCmd()
|
|
|
|
+ {
|
|
|
|
+ ClearReadBuffer();
|
|
|
|
+ var cmd = GetGetStatusCmd();
|
|
|
|
+ serialPort.Write(cmd, 0, cmd.Length);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ private void SendGetChallengeCmd()
|
|
|
|
+ {
|
|
|
|
+ ClearReadBuffer();
|
|
|
|
+ var cmd = GetGetChallengeCmd();
|
|
|
|
+ serialPort.Write(cmd, 0, cmd.Length);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ private void SendChallengedDataCmd(byte[] data)
|
|
|
|
+ {
|
|
|
|
+ ClearReadBuffer();
|
|
|
|
+ serialPort.Write(data, 0, data.Length);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
private void SendFlashFirmwareCmd()
|
|
private void SendFlashFirmwareCmd()
|
|
{
|
|
{
|
|
ClearReadBuffer();
|
|
ClearReadBuffer();
|
|
var cmd = GetFlashFirmwareCmd();
|
|
var cmd = GetFlashFirmwareCmd();
|
|
serialPort.Write(cmd, 0, cmd.Length);
|
|
serialPort.Write(cmd, 0, cmd.Length);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ private void SendGetFirmwareVersionCmd()
|
|
|
|
+ {
|
|
|
|
+ ClearReadBuffer();
|
|
|
|
+ var cmd = GetGetFirmwareVersionCmd();
|
|
|
|
+ serialPort.Write(cmd, 0, cmd.Length);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ private void SendGetEepromCmd(byte address)
|
|
|
|
+ {
|
|
|
|
+ ClearReadBuffer();
|
|
|
|
+ var cmd = GetGetEepromCmd(address);
|
|
|
|
+ serialPort.Write(cmd, 0, cmd.Length);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ private void SendEraseEepromCmd(byte address)
|
|
|
|
+ {
|
|
|
|
+ ClearReadBuffer();
|
|
|
|
+ var cmd = GetEraseEepromCmd(address);
|
|
|
|
+ serialPort.Write(cmd, 0, cmd.Length);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
|
|
- //var readBuffer = new byte[10];
|
|
|
|
- //var readByteCnt = serialPort.Read(readBuffer, 0 , 10);
|
|
|
|
|
|
+ private async Task<byte?> GetGetStatusRespond()
|
|
|
|
+ {
|
|
|
|
+ await Task.Delay(1000);
|
|
|
|
+ if (serialPort.BytesToRead <= 0)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ var dumpBuffer = new byte[serialPort.BytesToRead];
|
|
|
|
+ serialPort.Read(dumpBuffer, 0, dumpBuffer.Length);
|
|
|
|
+ if (dumpBuffer.Length != 1)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return dumpBuffer[0];
|
|
|
|
+ }
|
|
|
|
+ private async Task<byte[]> GetGetChallengeRespond()
|
|
|
|
+ {
|
|
|
|
+ await Task.Delay(1000);
|
|
|
|
+ if (serialPort.BytesToRead <= 0)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ var dumpBuffer = new byte[serialPort.BytesToRead];
|
|
|
|
+ serialPort.Read(dumpBuffer, 0, dumpBuffer.Length);
|
|
|
|
+ if (dumpBuffer.Length != 16)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ return dumpBuffer;
|
|
|
|
+ }
|
|
|
|
+ private async Task<bool> GetRespondChallengeRespond()
|
|
|
|
+ {
|
|
|
|
+ if (serialPort.BytesToRead <= 0)
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ await Task.Delay(1000);
|
|
|
|
+ var dumpBuffer = new byte[serialPort.BytesToRead];
|
|
|
|
+ serialPort.Read(dumpBuffer, 0, dumpBuffer.Length);
|
|
|
|
+ if (dumpBuffer.Length == 9)
|
|
|
|
+ {
|
|
|
|
+ //AES_ERROR
|
|
|
|
+ if (dumpBuffer[0] == 0x41 &&
|
|
|
|
+ dumpBuffer[1] == 0x45 &&
|
|
|
|
+ dumpBuffer[2] == 0x53 &&
|
|
|
|
+ dumpBuffer[3] == 0x2D &&
|
|
|
|
+ dumpBuffer[4] == 0x45 &&
|
|
|
|
+ dumpBuffer[5] == 0x72 &&
|
|
|
|
+ dumpBuffer[6] == 0x72 &&
|
|
|
|
+ dumpBuffer[7] == 0x6F &&
|
|
|
|
+ dumpBuffer[8] == 0x72 )
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
- //try
|
|
|
|
- //{
|
|
|
|
- // var reseivedString = Encoding.UTF8.GetString(readBuffer);
|
|
|
|
- // Console.WriteLine(reseivedString);
|
|
|
|
- //}
|
|
|
|
- //catch
|
|
|
|
- //{
|
|
|
|
|
|
+ if (dumpBuffer.Length == 6)
|
|
|
|
+ {
|
|
|
|
+ //AES_OK
|
|
|
|
+ if (dumpBuffer[0] == 0x41 &&
|
|
|
|
+ dumpBuffer[1] == 0x45 &&
|
|
|
|
+ dumpBuffer[2] == 0x53 &&
|
|
|
|
+ dumpBuffer[3] == 0x2D &&
|
|
|
|
+ dumpBuffer[4] == 0x4F &&
|
|
|
|
+ dumpBuffer[5] == 0x4B )
|
|
|
|
+ {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ private async Task<bool> GetFlashFirmwareCmdRespond()
|
|
|
|
+ {
|
|
|
|
+ if (serialPort.BytesToRead <= 0)
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ await Task.Delay(1000);
|
|
|
|
+ var dumpBuffer = new byte[serialPort.BytesToRead];
|
|
|
|
+ serialPort.Read(dumpBuffer, 0, dumpBuffer.Length);
|
|
|
|
|
|
- //}
|
|
|
|
|
|
+ if (dumpBuffer.Length != 8)
|
|
|
|
+ {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ if (dumpBuffer[0] == 0x00 && //0x00
|
|
|
|
+ //dumpBuffer[1] == 0x45 && //0x80
|
|
|
|
+ //dumpBuffer[2] == 0x53 && //0x03
|
|
|
|
+ dumpBuffer[3] == 0xD1 && //0xd1
|
|
|
|
+ dumpBuffer[4] == 0x00 && //0x00
|
|
|
|
+ dumpBuffer[5] == 0x01 //0x01
|
|
|
|
+ //dumpBuffer[6] == 0x72 &&
|
|
|
|
+ //dumpBuffer[7] == 0x6F
|
|
|
|
+ )
|
|
|
|
+ {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
|
|
- return;
|
|
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ private async Task<byte?> GetGetFirmwareVersionRespond()
|
|
|
|
+ {
|
|
|
|
+ if (serialPort.BytesToRead <= 0)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ await Task.Delay(1000);
|
|
|
|
+
|
|
|
|
+ var dumpBuffer = new byte[serialPort.BytesToRead];
|
|
|
|
+ serialPort.Read(dumpBuffer, 0, dumpBuffer.Length);
|
|
|
|
+
|
|
|
|
+ if (dumpBuffer.Length != 8)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (dumpBuffer[0] == 0x00 && //0x00
|
|
|
|
+ //dumpBuffer[1] == 0x45 && //0x8x
|
|
|
|
+ //dumpBuffer[2] == 0x53 && //0x03
|
|
|
|
+ dumpBuffer[3] == 0xD2 && //0xd2
|
|
|
|
+ dumpBuffer[4] == 0x00 //0x00
|
|
|
|
+ //dumpBuffer[5] == 0x01 //0xXX
|
|
|
|
+ //dumpBuffer[6] == 0x72
|
|
|
|
+ //dumpBuffer[7] == 0x6F
|
|
|
|
+ )
|
|
|
|
+ {
|
|
|
|
+ return dumpBuffer[5];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ private async Task<byte?> GetGetEepromRespond(byte expectedAddress)
|
|
|
|
+ {
|
|
|
|
+ if (serialPort.BytesToRead <= 0)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ await Task.Delay(1000);
|
|
|
|
+
|
|
|
|
+ var dumpBuffer = new byte[serialPort.BytesToRead];
|
|
|
|
+ serialPort.Read(dumpBuffer, 0, dumpBuffer.Length);
|
|
|
|
+
|
|
|
|
+ if (dumpBuffer.Length != 10)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (dumpBuffer[0] == 0x00 && //0x00
|
|
|
|
+ //dumpBuffer[1] == 0x45 && //0x8x
|
|
|
|
+ //dumpBuffer[2] == 0x53 && //0x03
|
|
|
|
+ dumpBuffer[3] == 0xD0 && //0xd0
|
|
|
|
+ dumpBuffer[4] == 0x00 && //0x00
|
|
|
|
+ dumpBuffer[5] == 0x01 && //0x01
|
|
|
|
+ dumpBuffer[6] == expectedAddress //0x10
|
|
|
|
+ //dumpBuffer[7] == 0x6F //data
|
|
|
|
+ //dumpBuffer[8] == 0x6F
|
|
|
|
+ //dumpBuffer[9] == 0x6F
|
|
|
|
+ )
|
|
|
|
+ {
|
|
|
|
+ return dumpBuffer[7];
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ private async Task<bool?> GetEraseEepromRespond(byte expectedAddress)
|
|
|
|
+ {
|
|
|
|
+ if (serialPort.BytesToRead <= 0)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ await Task.Delay(1000);
|
|
|
|
+
|
|
|
|
+ var dumpBuffer = new byte[serialPort.BytesToRead];
|
|
|
|
+ serialPort.Read(dumpBuffer, 0, dumpBuffer.Length);
|
|
|
|
+
|
|
|
|
+ if (dumpBuffer.Length != 8)
|
|
|
|
+ {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (dumpBuffer[0] == 0x00 && //0x00
|
|
|
|
+ //dumpBuffer[1] == 0x45 && //0x8x
|
|
|
|
+ //dumpBuffer[2] == 0x53 && //0x03
|
|
|
|
+ dumpBuffer[3] == 0xD0 && //0xD0
|
|
|
|
+ dumpBuffer[4] == 0x00 && //0x00
|
|
|
|
+ dumpBuffer[5] == expectedAddress //0x01
|
|
|
|
+ //dumpBuffer[6] == 0x6F
|
|
|
|
+ //dumpBuffer[7] == 0x6F
|
|
|
|
+ )
|
|
|
|
+ {
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
}
|
|
}
|
|
|
|
|
|
private byte[] GetFlashFirmwareCmd()
|
|
private byte[] GetFlashFirmwareCmd()
|
|
{
|
|
{
|
|
- byte[] baseCmd = new byte[] { 0x00, 0x00, 0x02, 0xD1, 0x01, 0x26, 0xD4 };
|
|
|
|
|
|
+ byte[] baseCmd = new byte[] { 0x00, 0x00, 0x02, 0xD1, 0x01 };
|
|
|
|
+ baseCmd[1] = cmdCNT;
|
|
|
|
+ ushort crc16 = CrcCalculator.CMN_YmodemGenCrc16(baseCmd);
|
|
|
|
+ byte[] result = new byte[baseCmd.Length + 2];
|
|
|
|
+ Array.Copy(baseCmd, result, baseCmd.Length);
|
|
|
|
+ result[result.Length - 2] = (byte)(crc16 / 256);
|
|
|
|
+ result[result.Length - 1] = (byte)(crc16 % 256);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ private byte[] GetGetFirmwareVersionCmd()
|
|
|
|
+ {
|
|
|
|
+ byte[] baseCmd = new byte[] { 0x00, 0x00, 0x02, 0xD2, 0x01 };
|
|
baseCmd[1] = cmdCNT;
|
|
baseCmd[1] = cmdCNT;
|
|
- cmdCNT++;
|
|
|
|
|
|
+ ushort crc16 = CrcCalculator.CMN_YmodemGenCrc16(baseCmd);
|
|
|
|
+ byte[] result = new byte[baseCmd.Length + 2];
|
|
|
|
+ Array.Copy(baseCmd, result, baseCmd.Length);
|
|
|
|
+ result[result.Length - 2] = (byte)(crc16 / 256);
|
|
|
|
+ result[result.Length - 1] = (byte)(crc16 % 256);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+ private byte[] GetGetEepromCmd(byte address)
|
|
|
|
+ {
|
|
|
|
+ byte[] baseCmd = new byte[] { 0x00, 0x00, 0x02, 0xD0, 0x01, 0x00 };
|
|
|
|
+ baseCmd[1] = cmdCNT;
|
|
|
|
+ baseCmd[5] = address;
|
|
|
|
+ ushort crc16 = CrcCalculator.CMN_YmodemGenCrc16(baseCmd);
|
|
|
|
+ byte[] result = new byte[baseCmd.Length + 2];
|
|
|
|
+ Array.Copy(baseCmd, result, baseCmd.Length);
|
|
|
|
+ result[result.Length - 2] = (byte)(crc16 / 256);
|
|
|
|
+ result[result.Length - 1] = (byte)(crc16 % 256);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private byte[] GetEraseEepromCmd(byte address)
|
|
|
|
+ {
|
|
|
|
+ byte[] baseCmd = new byte[] { 0x00, 0x00, 0x02, 0xD0, 0x00, 0x01 };
|
|
|
|
+ baseCmd[1] = cmdCNT;
|
|
|
|
+ baseCmd[5] = address;
|
|
|
|
+ ushort crc16 = CrcCalculator.CMN_YmodemGenCrc16(baseCmd);
|
|
|
|
+ byte[] result = new byte[baseCmd.Length + 2];
|
|
|
|
+ Array.Copy(baseCmd, result, baseCmd.Length);
|
|
|
|
+ result[result.Length - 2] = (byte)(crc16 / 256);
|
|
|
|
+ result[result.Length - 1] = (byte)(crc16 % 256);
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private byte[] GetGetStatusCmd()
|
|
|
|
+ {
|
|
|
|
+ byte[] baseCmd = new byte[] { 0x7F, 0x7F };
|
|
|
|
+ return baseCmd;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private byte[] GetGetChallengeCmd()
|
|
|
|
+ {
|
|
|
|
+ byte[] baseCmd = new byte[] { 0x7A, 0x7A };
|
|
return baseCmd;
|
|
return baseCmd;
|
|
}
|
|
}
|
|
|
|
|