private void parsePacket(string idata1) { if (idata1[0] == '1') { MessageBox.Show("Checksum fail"); } if (idata1[0] == '4') { //Splitting string into two parts 30 bytes and 2 bytes for checksum char[] result1 = idata1.ToCharArray(); // char[] rec2 = receive_data.Substring(30, 31).ToCharArray(); char[] new_result2 = new char[2]; //Again getting the checksum of received string uint chkval = checksum(result1); chkval = chkval % 256; new_result2[0] = int2hex(chkval / 16); new_result2[1] = int2hex(chkval % 16); if ((new_result2[0] == result1[30] && new_result2[1] == result1[31])) //checksum validation { float theta = float.Parse(idata1.Substring(1, 4)) / 1000; float Epsilon = float.Parse(idata1.Substring(7, 4)) / 1000; th_Field.Text = theta.ToString(); bige_Field.Text = Epsilon.ToString(); } else { MessageBox.Show("Checksum failed"); //if checksum char(s) validation fails, show fail } string feedback2 = "20000000000000000000000000000027"; char[] fee = feedback2.ToCharArray(); try { Thread.Sleep(300); COMport.Write(fee, 0, 32); } catch (Exception e) { MessageBox.Show(e.ToString()); } } }
public bool writeport(char[] data, int len) { try { COMport.Write(data, 0, len); } catch (Exception E) { MessageBox.Show(E.ToString()); return(false); } return(true); } // writeport
private void Health_Button_Click(object sender, EventArgs e) { char[] feedbackchar = new char[32]; if (COMport.IsOpen == true) { string healthreq = "500000000000000000000000000000"; char[] reqh = new char[32]; for (int i = 0; i < 30; i++) { reqh[i] = healthreq[i]; } char[] ckval = new char[2]; //Again getting the checksum of received string uint chkval = checksum(reqh); chkval = chkval % 256; ckval[0] = int2hex(chkval / 16); ckval[1] = int2hex(chkval % 16); reqh[30] = ckval[0]; reqh[31] = ckval[1]; try { Thread.Sleep(300); COMport.Write(reqh, 0, 32); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } var CurDate = DateTime.Now; Time_Field.Text = CurDate.Hour.ToString() + " : " + CurDate.Minute.ToString() + " : " + CurDate.Millisecond.ToString(); } else { Log_Box.Text = "Unable to write to PORT"; } }
private void Health_Button_Click(object sender, EventArgs e) { char[] feedbackchar = new char[32]; if (COMport.IsOpen == true) { char[] reqh = new char[32]; reqh = "500000000000000000000000000000".ToCharArray(); char[] ckval = new char[2]; //Again getting the checksum of received string uint chkval = checksum(reqh); chkval = chkval % 256; ckval[0] = int2hex(chkval / 16); ckval[1] = int2hex(chkval % 16); reqh[30] = ckval[0]; reqh[31] = ckval[1]; try { Thread.Sleep(300); COMport.Write(reqh, 0, 32); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } COMport.ReadTimeout = 3500; } else { Log_Box.Text = "Unable to write to PORT"; } }