private void DataReceiveHandler(object sender, SerialDataReceivedEventArgs args) { SerialPort port = (SerialPort)sender; string all_str = port.ReadExisting(); port.DiscardInBuffer(); string rx_data_str = "-------"; int start_index = all_str.IndexOf('D'); if (start_index != -1 && start_index + word_length <= all_str.Length) { rx_data_str = all_str.Substring(start_index, word_length); } else { start_index = all_str.IndexOf('T'); if (start_index != -1 && start_index + word_length <= all_str.Length) { rx_data_str = all_str.Substring(start_index, word_length); } } RXData board_data = RXDataParser(rx_data_str); if (board_data.valid) { UpdateBoard(board_data); } }
public void RefreshPoint() { int data; while (true) { // 关闭窗体时结束线程 if (leave == true) { break; } // 按钮控制运行或暂停 if (pause == false) { // 获取最新的数据添加到链表中 data = RXData.ReadData(); DataList.Add(data); if (DataList.Count > SIZE) { DataList.RemoveAt(0); } // 由链表生成点集 if (DataList.Count != 0) { DataPoint = new Point[DataList.Count]; } for (int i = 0; i < DataList.Count; i++) { // 描点 (x,y) DataPoint[i] = new Point(i * 10, pictureBox1.Height - DataList[i] - SPACE); } // 刷新显示 pictureBox1.Refresh(); // 显示百分比 if (DataList.Count > 0) { label1.Text = DataList[DataList.Count - 1].ToString() + "%"; } } Thread.Sleep(400); } }
private void UpdateBoard(RXData board_data) { // verificar si el id esta en el array de ids; int index = Array.IndexOf(ids, board_data.id); bool valid = true; if (index > -1) { if (board_data.timeout) { board_array[index].Enabled = false; } else { board_array[index].Enabled = true; switch (board_data.angle) { case AngleType.ROLL_DATA: board_array[index].Roll = board_data.sign * board_data.value; break; case AngleType.PITCH_DATA: board_array[index].Pitch = board_data.sign * board_data.value; break; case AngleType.YAW_DATA: board_array[index].Yaw = board_data.sign * board_data.value; break; default: valid = false; break; } } must_redraw = valid; index_to_draw = index; } }
private RXData RXDataParser(string rx_data) { // verifiar que tenga length adecuado: RXData ret = new RXData(); bool valid = true; valid = (rx_data.Length == word_length); ret.value = 0; ret.angle = AngleType.ROLL_DATA; ret.valid = false; if (valid) { if (rx_data[0] == data_char) { if (rx_data[1] >= '0' && rx_data[1] <= '7') { ret.id = rx_data[1]; int value = 0; try { value = Convert.ToInt32(rx_data.Substring(4, 3)); } catch { valid = false; } if ((value <= 180 && value >= -180) && valid) { ret.value = value; switch (rx_data[2]) { case 'R': ret.angle = AngleType.ROLL_DATA; break; case 'C': ret.angle = AngleType.PITCH_DATA; break; case 'O': ret.angle = AngleType.YAW_DATA; break; default: valid = false; break; } if (rx_data[3] == '+' || rx_data[3] == '-') { ret.sign = (rx_data[3] == '+') ? 1 : -1; ret.valid = valid; } else { valid = false; } } else { valid = false; } } else { valid = false; } } else if (rx_data[0] == timeout_char) { ret.timeout = true; if (rx_data[1] >= '0' && rx_data[1] <= '7') { ret.id = rx_data[1]; } else { valid = false; } } else { valid = false; } } else { valid = false; } ret.valid = valid; return(ret); }