private cmdStruct decodeReceivedCmd(string str) { // Protocoll // | ID | CMD | val1 |val2 | \n | // | 2B | 1B | 4B | 4B | 1B | // 9 11 12 16 20 // 0 2 3 7 11 cmdStruct cmdBack = new cmdStruct(); byte[] bytes = new byte[str.Length * sizeof(char)]; System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length); if (wireless_Flag == true && bytes[0] != (byte)CMDs.CMD_VERS) { cmdBack.id = Int16.Parse(str.Substring(0, 2)); cmdBack.cmd = bytes[2 * sizeof(char)]; if (str.Length >= 7) cmdBack.val1 = Int16.Parse(str.Substring(3, 4)); //TODO: length is somehow 11 but no conntent if (str.Length >= 11) cmdBack.val2 = Int16.Parse(str.Substring(7, 4)); return cmdBack; } else { cmdBack.cmd = bytes[0]; if (str.Length >= 5) cmdBack.val1 = Int16.Parse(str.Substring(1, 4)); if (str.Length >= 9) cmdBack.val2 = Int16.Parse(str.Substring(5, 4)); return cmdBack; } }
private bool processReceivedCmd(cmdStruct c) { bool back = true; if (txtReceiveBox.TextLength >= 20000) txtReceiveBox.Text = txtReceiveBox.Text.Substring(17000, 3000); switch (c.cmd) { case (byte)CMDs.CMD_MOIS: /* moisLab.Text = c.val1.ToString() + "%"; progressBar1.Value = c.val1; // Timer for moist sensing / sensor connected? pictureBox2.Visible = false; timer2.Stop(); timer2.Start(); */ Node[c.id].set_mois(c.val1); Node[c.id].redraw(); Node[c.id].lastping_sensor = 0; break; case (byte)CMDs.CMD_VOLT: break; case (byte)CMDs.CMD_MIN: minMoisLab.Text = c.val1.ToString(); break; case (byte)CMDs.CMD_MAX: maxMoisLab.Text = c.val1.ToString(); break; case (byte)CMDs.CMD_CALI: if(c.val1 == 1) CaliForm.changeContext((byte)FormComModes.CaliDry); else CaliForm.changeContext((byte)FormComModes.CaliError); break; case (byte)CMDs.CMD_DRY: if ((c.val1 >= 0) && (c.val1 <= 1024)) CaliForm.changeContext((byte)FormComModes.CaliWet); else CaliForm.changeContext((byte)FormComModes.CaliError); break; case (byte)CMDs.CMD_WET: if ((c.val1 >= 0) && (c.val1 <= 1024)) CaliForm.changeContext((byte)FormComModes.CaliFin); else CaliForm.changeContext((byte)FormComModes.CaliError); break; case (byte)CMDs.CMD_TEST: moisLab.Text = c.val1.ToString(); txtReceiveBox.AppendText("[" + get_dtn_time() + "] " + "received TEST" + "\n"); _backgroundPause = false; break; /* case (byte)CMDs.CMD_FIN: if ((byte)c.val1 == 1) CaliForm.changeContext((byte)FormComModes.CaliFin); else CaliForm.changeContext((byte)FormComModes.CaliError); break; */ case(byte)CMDs.CMD_VERS: initFlag = 1; txtReceiveBox.AppendText("[" + get_dtn_time() + "] " + "Software VERSION : " + c.val1.ToString() + "\n"); txtReceiveBox.AppendText("[" + get_dtn_time() + "] " + " BUILD : " + c.val2.ToString() + "\n"); versionLab.Text = "v" + Convert.ToString(c.val1/100) + "." + Convert.ToString(c.val1%100); versionLab.Text += " build:" + Convert.ToString(c.val2); if (c.val2 >= 1000) { //Veriosn with Wireless if build no. > 1000 pictureBoxRssi.Visible = true; wireless_Flag = true; } Node[c.id].set_active(true); break; case(byte)CMDs.CMD_RSSI: txtReceiveBox.AppendText("[" + get_dtn_time() + "] " + "received RSSI :" + c.val1.ToString() +"\n"); Node[c.id].set_rssi(c.val1); Node[c.id].lastping_rssi = 0; Node[c.id].redraw(); /* rssiLab.Text = "-" + c.val1.ToString(); rssiLab.Show(); progressBarRSSI.Minimum = 0; progressBarRSSI.Maximum = 40; if((c.val1 <= 120) && (c.val1 >= 80)) progressBarRSSI.Value = 120 - c.val1; if (c.val1 <= 94) pictureBox1.Image = Soil_moisture_App.Properties.Resources.wifi3; else if(c.val1 <= 105) pictureBox1.Image = Soil_moisture_App.Properties.Resources.wifi_2; else if(c.val1 <= 110) pictureBox1.Image = Soil_moisture_App.Properties.Resources.wifi_1; else pictureBox1.Image = Soil_moisture_App.Properties.Resources.wifi_0; */ //start timout timer break; default: back = false; break; } return back; }
private void saveToCSV(cmdStruct cmd){ String newLine = string.Format("{0};{1};{2};{3};{4}", get_dtn_date() + " " + get_dtn_time(), cmd.id, CMD_array[Convert.ToByte(cmd.cmd) - 'A'], cmd.val1.ToString(), cmd.val2.ToString()); csv.AppendLine(newLine); if (csv.Length > 2000) { File.AppendAllText(filePath, csv.ToString()); csv.Remove(0, csv.Length); } }