我正在尝试完成一个应用程序,我将通过蓝牙将字符串发送到arduino。我有使用serialPort1在WPF中完美运行的代码。它使用keydown和key up事件通过蓝牙即时发送信号。我试图在UWP中替换这个,我现在已经挣扎了好几天了。我设法让它工作但是当我按下键或向上键时我有一些滞后。信号被发送,但有1秒的延迟,我无法理解为什么。这是WPF的代码示例,没有问题:
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 AForge.Video;
namespace RoverControl
{
public partial class Form1 : Form
{
MJPEGStream stream;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(Form1_KeyDown);
this.KeyUp += new System.Windows.Forms.KeyEventHandler(Form1_KeyUp);
}
//Declare the comands for Rover control//
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) // Holding Keyboard Character "W" //
{
serialPort1.Write("F"); // Passing command "Forward" thorugh letter "F" in arduino code //
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) // Keyboard Character "W" //
{
serialPort1.Write("S"); // Keyboard Character "S" //
}
}
void stream_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap bmp = (Bitmap)eventArgs.Frame.Clone();
pictureBox1.Image = bmp;
}
private void Start_Click(object sender, EventArgs e)
{
string IP = "";
IP = textBox3.Text;
stream = new MJPEGStream(IP);
stream.NewFrame += stream_NewFrame;
try
{
stream.Start();
}
catch (Exception)
{
MessageBox.Show("Please enter valid IP address.");
}
}
private void Stop_Click(object sender, EventArgs e)
{
stream.Stop();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string COM = "";
COM = comboBox1.Text;
serialPort1.PortName = COM;
serialPort1.BaudRate = 9600;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
serialPort1.Open();
}
catch (Exception)
{
var dialogResult = MessageBox.Show("Please select correct Comunication Port.");
}
}
private void Disconnect_Click(object sender, EventArgs e)
{
serialPort1.Close();
}
}
}
这是我到目前为止在UWP中的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Devices.Enumeration;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
// The Blank Page item template is documented at
https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace Robot2
{
public sealed partial class MainPage : Page
{
private DeviceInformationCollection deviceCollection;
private DeviceInformation selectedDevice;
private RfcommDeviceService deviceService;
//public string deviceName = ""; // Specify the device name to be selected; You can find the device name from the webb under bluetooth
StreamSocket streamSocket = new StreamSocket();
public MainPage()
{
this.InitializeComponent();
InitializeRfcommServer();
}
private void selectorComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//var selector = BluetoothDevice.GetDeviceSelector();
//var BTDevinfo = DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
// var BTDevinfo = DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort), new string[] { "System.Devices.AepService.AepId" });
//selectorComboBox.ItemsSource = deviceCollection;
}
private void Connect_Click(object sender, RoutedEventArgs e)
{
ConnectToDevice();
}
private async void InitializeRfcommServer()
{
try
{
string device1 = RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort);
deviceCollection = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(device1);
selectorComboBox.ItemsSource = deviceCollection;
}
catch (Exception exception)
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = exception.Message;
}
}
private async void ConnectToDevice()
{
foreach (var item in deviceCollection)
{
{
selectedDevice = item;
}
}
if (selectedDevice == null)
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = "Cannot find the device specified; Please check the device name";
return;
}
else
{
deviceService = await RfcommDeviceService.FromIdAsync(selectedDevice.Id);
if (deviceService != null)
{
//connect the socket
try
{
await streamSocket.ConnectAsync(deviceService.ConnectionHostName, deviceService.ConnectionServiceName);
}
catch (Exception ex)
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = "Cannot connect bluetooth device:" + ex.Message;
}
}
else
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = "Didn't find the specified bluetooth device";
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (deviceService != null)
{
//send data
string sendData = "F";
if (string.IsNullOrEmpty(sendData))
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = "Please specify the string you are going to send";
}
else
{
DataWriter dwriter = new DataWriter(streamSocket.OutputStream);
UInt32 len = dwriter.MeasureString(sendData);
dwriter.WriteUInt32(len);
dwriter.WriteString(sendData);
//await dwriter.StoreAsync();
//await dwriter.FlushAsync();
}
}
}
private async void Grid_KeyDown(object sender, KeyRoutedEventArgs e)
{
//handling code here
if (e.Key == Windows.System.VirtualKey.F)
{
string sendData = "F";
if (string.IsNullOrEmpty(sendData))
{
errorStatus.Visibility = Visibility.Visible;
errorStatus.Text = "Please specify the string you are going to send";
}
else
{
DataWriter dwriter = new DataWriter(streamSocket.OutputStream);
UInt32 len = dwriter.MeasureString(sendData);
dwriter.WriteUInt32(len);
dwriter.WriteString(sendData);
await dwriter.StoreAsync();
await dwriter.FlushAsync();
}
}
}
}
}
我一直在阅读有关可能是解决方案的页面加载事件,但是我尝试过的内容不起作用。任何想法如何修改UWP编码以避免滞后/延迟?