2017-08-02 29 views
0

我試圖在c#中使用圖表來顯示Myo數據。我從Myo收到數據並將其發送到圖表,但不會顯示任何內容。網上的例子並沒有幫助我!這是代碼(我想我有線程,但不知道多少,生產類從肌接收原始EMG數據和Form1中應該表現出來):從Myo接收數據並將其顯示在c#圖表上

using MyoSharp.Communication; 
using MyoSharp.Device; 
using MyoSharp.Exceptions; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Windows.Forms.DataVisualization.Charting; 


namespace MyoThings 
{ 
    public partial class Form1 : Form 
    { 
     int i = 0; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 



     private void button1_Click(object sender, EventArgs e) 
     { 
      Producer producer = new Producer(); 
      producer.StartConnection(); 
     } 

     public void receiveData(int data) 
     { 
      Console.WriteLine(data); 
      chart1.Series[0].Points.Add(i++, data); // won't add anything - 
      chart1.Invalidate(); 

     } 


    } 

    class Producer 
    { 
     Chart chart = new Chart(); 

     public void StartConnection() 
     { 
      using (var channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create(), 
       MyoErrorHandlerDriver.Create(MyoErrorHandlerBridge.Create())))) 
      { 
       using (var hub = Hub.Create(channel)) 
       { 
        hub.MyoConnected += (sender, e) => 
        { 
         Console.WriteLine($"Myo Connected, handle: {e.Myo.Handle}"); 
         e.Myo.Vibrate(VibrationType.Short); 
         e.Myo.EmgDataAcquired += Myo_EmgDataAcquired; 
         e.Myo.SetEmgStreaming(true); 

        }; 
        channel.StartListening(); 
        //int i = 0; 
        while (true) 
        { 

        } 
       } 
      } 

     } 

     private static void Myo_EmgDataAcquired(object sender, EmgDataEventArgs e) 
     { 
      //Console.WriteLine(e.EmgData.GetDataForSensor(1)); 
      Producer producer = new Producer(); 
      Form1 form = new Form1(); 
      //sends data of myo to chart 
      form.receiveData(e.EmgData.GetDataForSensor(1)); 

     } 
    } 
} 
+0

在'Myo_EmgDataAcquired'您爲每個肌電圖的數據,這是來給你創造新的形式和製作人。在這種方法中,您只能繪製(刷新)圖表。 –

+0

我無法從Form1訪問圖表。 – Someone

回答

0

我可以回答它自己。我把圖表製作類,並添加點

using MyoSharp.Communication; 
    using MyoSharp.Device; 
    using MyoSharp.Exceptions; 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Forms; 
    using System.Threading; 
    using System.Diagnostics; 
    using System.Windows.Forms.DataVisualization.Charting; 

    namespace WindowsFormsApplication2 
    { 
     public partial class Form1 : Form 
     { 

      private Producer producer; 
      private bool sitSensorOne = true; 
      private bool sitSensorTwo = true; 
      private bool sitSensorThree = true; 
      private bool sitSensorFour = true; 
      private bool sitSensorFive = true; 
      private bool sitSensorSix = true; 
      private bool sitSensorSeven = true; 
      private bool sitSensorEighth = true; 

      public Form1() 
      { 
       InitializeComponent(); 
       producer = new Producer(chart1); 
       producer.YSeriesEvent += MyHandler; 
       chart1.Series[0].Enabled = true; 
       Load += (sender, e) => producer.Start(); 

      } 

      private void MyHandler(object sender, int data) 
      { 
       Invoke(new Action(() => 
       { 

       })); 
      } 

    } 
    } 

     class Producer 
     { 
      public event EventHandler<int> YSeriesEvent; 
      private Thread thread; 
      public int Data; 
      private Chart chart; 
      public Producer(Chart chart) 
      { 
       this.chart = chart; 
       thread = new Thread(new ThreadStart(this.Work)); 
       thread.IsBackground = true; 
       thread.Name = "My Worker"; 
      } 

      public void Start() 
      { 
       thread.Start(); 
      } 

      private void Work() 
      { 

       using (var channel = Channel.Create(ChannelDriver.Create(ChannelBridge.Create(), 
         MyoErrorHandlerDriver.Create(MyoErrorHandlerBridge.Create())))) 
       { 
        using (var hub = Hub.Create(channel)) 
        { 
         hub.MyoConnected += (sender, e) => 
         { 
          Console.WriteLine($"Myo connected, handle: {e.Myo.Handle}"); 
          e.Myo.Vibrate(VibrationType.Short); 
          e.Myo.EmgDataAcquired += Myo_EmgDataAcquired; 
          e.Myo.SetEmgStreaming(true); 
          YSeriesEvent?.Invoke(this, Data); 

         }; 
         channel.StartListening(); 
         while (true) { } 
        } 
       } 
      } 
      private void Myo_EmgDataAcquired(object sender, EmgDataEventArgs e) 
      { 

       Data = e.EmgData.GetDataForSensor(3); 
       Console.WriteLine(Data); 
       chart.Invoke(new Action(() => 
       { 
        for (int i = 0; i < 8; i++) 
         chart.Series[i].Points.AddY(e.EmgData.GetDataForSensor(i)); 
       } 
       )); 

      } 

      private void returnData() 
      { 
       chart.Series[0].Points.AddY(Data); 
       Console.WriteLine(Data); 
      } 
     } 
    } 
相關問題