我试图使用c#在图表上显示Myo数据。我从Myo收到数据并将其发送到图表,但它不会显示任何内容。网上的例子对我没有帮助!这是代码(我想我有线程但不知道,生产者类从myo接收原始emg数据,Form1应该显示它):
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 :(得分: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);
}
}
}