好的,不是单一的应用程序解决方案,但您可以执行以下操作:
未找到 - 未找到用于指定目的的开放 API 应用程序。
接收加速度计数据 - UDP
class Inertia : IDisposable
{
UdpClient listener = new UdpClient(5555);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 5555);
Control context = null;
public Inertia(Control context)
{
this.context = context;
IAsyncResult result = listener.BeginReceive(new AsyncCallback(udpCallback), null);
}
private void udpCallback(IAsyncResult result)
{
byte[] data = listener.EndReceive(result, ref groupEP);
if (data.Length > 0)
{
string line = ASCIIEncoding.ASCII.GetString(data);
if (context.InvokeRequired)
context.BeginInvoke(UDPStringReceived, line);
else
if (UDPStringReceived != null) UDPStringReceived(line);
}
result = listener.BeginReceive(new AsyncCallback(udpCallback), null);
}
public delegate void UDPStringReceivedHandler(string line);
public event UDPStringReceivedHandler UDPStringReceived;
public void Dispose()
{
//TODO: Stop listening
}
}
使用 Inertia 类与 WinForms 一起使用:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
Inertia inertia = null;
void Form1_Load(object sender, EventArgs e)
{
inertia = new Inertia(this);
inertia.UDPStringReceived += inertia_UDPStringReceived;
}
void inertia_UDPStringReceived(string line)
{
try
{
string[] components = line.Split(',');
//My text boxes, showing the data
t.Text = components[0];
//index 1 contains the value 2, which probably indicates "Accelerometer" in next 3 values
aX.Text = components[2];
aY.Text = components[3];
aZ.Text = components[4];
//index 5 contains the value 3, which probably indicates "Gyroscope" in next 3 values
gX.Text = components[6];
gY.Text = components[7];
gZ.Text = components[8];
}
catch (Exception ex)
{
}
}
}