Finish serial JSON decoding

This commit is contained in:
JackCarterSmith 2021-06-27 18:23:33 +02:00
parent c398bc2df8
commit b78033ff79
Signed by: JackCarterSmith
GPG Key ID: DB362B740828F843
4 changed files with 328 additions and 277 deletions

View File

@ -77,7 +77,7 @@ namespace TinyFPGA_VisualProgrammer
if (sController.GetPortOpened()) if (sController.GetPortOpened())
TProg.CheckTinyPresence(); TProg.CheckTinyPresence();
if (TProg.TinyFPGAIsConnected) if (TProg.TinyFPGAIsConnected)
statusLabel.Text = TProg.GetBoardData().name + " v" + TProg.GetBoardData().hver + " found."; statusLabel.Text = TProg.GetBoardData().Name + " v" + TProg.GetBoardData().HVer + " found.";
else else
statusLabel.Text = "No TinyFPGA found."; statusLabel.Text = "No TinyFPGA found.";
connectBtn.Text = "Disconnect"; connectBtn.Text = "Disconnect";

View File

@ -21,7 +21,7 @@ namespace TinyFPGA_VisualProgrammer
mainSerial.ReceivedBytesThreshold = 1; mainSerial.ReceivedBytesThreshold = 1;
mainSerial.ReadBufferSize = 4096; mainSerial.ReadBufferSize = 4096;
mainSerial.WriteBufferSize = 4096; mainSerial.WriteBufferSize = 4096;
mainSerial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); //mainSerial.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
List<string> usbPorts = ComPortNames("1D50", "6130"); List<string> usbPorts = ComPortNames("1D50", "6130");
if (usbPorts.Count > 0) if (usbPorts.Count > 0)
{ {
@ -34,6 +34,7 @@ namespace TinyFPGA_VisualProgrammer
} }
} }
/*
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{ {
SerialPort sp = (SerialPort)sender; SerialPort sp = (SerialPort)sender;
@ -41,6 +42,7 @@ namespace TinyFPGA_VisualProgrammer
Console.WriteLine("Data Received:"); Console.WriteLine("Data Received:");
Console.Write(indata); Console.Write(indata);
} }
*/
public void OpenPort() public void OpenPort()
{ {
@ -79,24 +81,32 @@ namespace TinyFPGA_VisualProgrammer
{ {
mainSerial.Write(db, 0, dc); mainSerial.Write(db, 0, dc);
} }
catch { } catch (Exception ex)
{
Console.WriteLine("Failed to push new data into serial bus. (" + ex.Message + ")");
}
//while (mainSerial.BytesToWrite >= 0) { } mainSerial.DiscardOutBuffer();
//mainSerial.DiscardOutBuffer();
} }
public byte[] WaitForBytes(int dc) public byte[] WaitForBytes(int dc)
{ {
byte[] result = new byte[dc]; byte[] result = new byte[dc];
int countdown = 0;
do
{
try try
{ {
mainSerial.Read(result, 0, dc); countdown += mainSerial.Read(result, countdown, dc - countdown);
} }
catch { } catch (Exception ex)
{
//while (mainSerial.BytesToRead >= 0) { } Console.WriteLine("Failed to fetch new data from serial bus. (" + ex.Message + ")");
//mainSerial.DiscardOutBuffer(); }
}
while (countdown < dc);
mainSerial.DiscardInBuffer();
return result; return result;
} }

View File

@ -11,25 +11,44 @@ namespace TinyFPGA_VisualProgrammer
{ {
public class JSON_BoardMeta public class JSON_BoardMeta
{ {
public string name { get; set; } [JsonProperty("name")]
public string fpga { get; set; } public string Name { get; set; }
public string hver { get; set; }
public string uuid { get; set; } [JsonProperty("fpga")]
public string FPGA { get; set; }
[JsonProperty("hver")]
public string HVer { get; set; }
[JsonProperty("uuid")]
public string UUID { get; set; }
} }
public class JSON_BootMeta public class JSON_BootMeta
{ {
public string bootloader { get; set; } [JsonProperty("bootloader")]
public string bver { get; set; } public string BootLoader { get; set; }
public string update { get; set; }
public JSON_AddrMap addrmap { get; set; } [JsonProperty("bver")]
public string BVer { get; set; }
[JsonProperty("update")]
public string Update { get; set; }
[JsonProperty("addrmap")]
public JSON_AddrMap AddrMap { get; set; }
} }
public class JSON_AddrMap public class JSON_AddrMap
{ {
public string bootloader { get; set; } [JsonProperty("bootloader")]
public string userimage { get; set; } public string BootLoader { get; set; }
public string userdata { get; set; }
[JsonProperty("userimage")]
public string UserImage { get; set; }
[JsonProperty("userdata")]
public string UserData { get; set; }
} }
public class TinyProg public class TinyProg
@ -126,7 +145,7 @@ namespace TinyFPGA_VisualProgrammer
/* /*
* Send datas container through serial line * Send datas container through serial line
*/ */
//pPort.clearBuffer(); pPort.clearBuffer();
pPort.SendBytes(cmdSequence, cmdSequence.Length); pPort.SendBytes(cmdSequence, cmdSequence.Length);
if (read_len > 0) if (read_len > 0)
@ -181,13 +200,17 @@ namespace TinyFPGA_VisualProgrammer
public string ReadSecureRegPage(uint page) public string ReadSecureRegPage(uint page)
{ {
return Encoding.UTF8.GetString(SendCommand(SecurePage_readCmd, BitConverter.GetBytes((page & 0xFFFF) << 8 + SecurePage_bitOffset), new byte[] { 0x00 }, 255)).Insert(0, "{"); string extractedData;
byte[] cmdResult = SendCommand(SecurePage_readCmd, BitConverter.GetBytes((page & 0xFFFF) << 8 + SecurePage_bitOffset), new byte[] { 0x00 }, 255);
extractedData = Encoding.UTF8.GetString(cmdResult).Trim(); // Need trimming to clear string from dirty bytes from the casting process.
return extractedData;
} }
} }
public class TinyMeta public class TinyMeta
{ {
TinyProg tProg; readonly TinyProg tProg;
//JSONRoot JSON_root; //JSONRoot JSON_root;
public JSON_BoardMeta BoardMeta { get; private set; } public JSON_BoardMeta BoardMeta { get; private set; }
public JSON_BootMeta BootMeta { get; private set; } public JSON_BootMeta BootMeta { get; private set; }
@ -203,12 +226,30 @@ namespace TinyFPGA_VisualProgrammer
{ {
string rawJSON; string rawJSON;
rawJSON = tProg.ReadSecureRegPage(1).Substring(13, 107); rawJSON = tProg.ReadSecureRegPage(1).Substring(12);
rawJSON = rawJSON.Remove(rawJSON.Length - 2);
try
{
BoardMeta = JsonConvert.DeserializeObject<JSON_BoardMeta>(rawJSON); // Read 1st register, contain boardmeta BoardMeta = JsonConvert.DeserializeObject<JSON_BoardMeta>(rawJSON); // Read 1st register, contain boardmeta
}
catch (JsonReaderException _jex)
{
Console.WriteLine(_jex.Message);
Console.WriteLine(rawJSON);
}
Thread.Sleep(200); Thread.Sleep(200);
rawJSON = tProg.ReadSecureRegPage(2).Substring(12, 209); rawJSON = tProg.ReadSecureRegPage(2).Substring(11);
rawJSON = rawJSON.Remove(rawJSON.Length - 2);
try
{
BootMeta = JsonConvert.DeserializeObject<JSON_BootMeta>(rawJSON); // Read 2nd register, contain bootmeta BootMeta = JsonConvert.DeserializeObject<JSON_BootMeta>(rawJSON); // Read 2nd register, contain bootmeta
}
catch (JsonReaderException _jex)
{
Console.WriteLine(_jex.Message);
Console.WriteLine(rawJSON);
}
Thread.Sleep(200); Thread.Sleep(200);
//tProg.ReadSecureRegPage(3); // Read 3rd register, contain nothing //tProg.ReadSecureRegPage(3); // Read 3rd register, contain nothing
} }