日期:2014-05-17 浏览次数:20977 次
namespace WindowsFormsApplication1 { public partial class Form1 : Form { int pix_index; bool recived=false; int[] buff = new int[13]; int sw=1; int index = 0; List<float> AccX = new List<float>(); List<float> AccY = new List<float>(); List<float> AccZ = new List<float>(); List<float> GyroX = new List<float>(); List<float> GyroY = new List<float>(); List<float> GyroZ = new List<float>(); List<float> X = new List<float>(); public Form1() { System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; InitializeComponent(); string[] port = null; try { port = SerialPort.GetPortNames(); } catch (Exception ex) { throw new Exception("获取计算机COM口列表失败!\r\n错误信息:" + ex.Message); } comboBox1.Items.AddRange(port); } private void button1_Click(object sender, EventArgs e) { if (button1.Text == "连接") { serialPort1.PortName = (string)comboBox1.SelectedItem; try { serialPort1.Open(); } catch (Exception ex) { MessageBox.Show("打开出错"); } } else { button1.Text = "连接"; serialPort1.Close(); } if (serialPort1.IsOpen) { button1.Text = "已连接"; zGraph1.f_ClearAllPix(); zGraph1.f_reXY(); zGraph1.f_LoadOnePix(ref X, ref GyroX, Color.Red, 2);//增加一条曲线 zGraph1.f_AddPix(ref X, ref GyroY,Color.Blue,2); zGraph1.f_AddPix(ref X, ref GyroZ, Color.Yellow, 2); zGraph1.f_AddPix(ref X, ref AccX, Color.Green, 2); zGraph1.f_AddPix(ref X, ref AccY, Color.White, 2); zGraph1.f_AddPix(ref X, ref AccZ, Color.Lime, 2); } } private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { int tmp; //tmp = serialPort1.ReadByte(); richTextBox1.AppendText("er "); //switch(sw) //{ // case 1 : // if (tmp == 0x3f) // sw=2; // break; // case 2: // if (tmp == 0xee) // sw = 3; // else // sw = 1; // break; // case 3: // buff[index] = tmp; // index++; // if (index == 12) // { // index = 0; // sw = 1; // recived = true; // } // break; //} // if (recived) // { // X.Add(pix_index++);//X轴增加 // recived = false; // GyroX.Add((short)(buff[0] << 8) + buff[1]); // GyroY.Add((short)(buff[2] << 8) + buff[3]); // GyroZ.Add((short)(buff[4] << 8) + buff[5]); // AccX.Add((short)(buff[6] << 8) + buff[7]); // AccY.Add((sh