我正在Raspberry Pi 3上使用Windows IoT Core制作应用程序。
我正在尝试使用串行通信与Arduino进行通信。我在GitHub上使用了本例中显示的大多数方法来创建串行设备并写入它:https://github.com/ms-iot/samples/tree/develop/SerialSample/CS。
程序在前130个字节左右运行正常,但之后会开始大幅减速并在第135个字节左右崩溃。
有时它会导致Raspberry Pi显示Windows 10“死亡蓝屏”,说它需要重启。
我尝试过多次重写代码并使用异步,任务和等待的不同变体,但似乎无法使其工作。
这是我用来初始化串行设备的代码:
private async void setup()
{
serialPort = await initializeSerial();
}
private async Task<SerialDevice> initializeSerial()
{
try
{
string aqs = SerialDevice.GetDeviceSelector();
var dis = await DeviceInformation.FindAllAsync(aqs);
for (int i = 0; i < dis.Count; i++)
{
if (dis[i].Name.Contains("Arduino Uno"))
{
var serialPort = await SerialDevice.FromIdAsync(dis[i].Id);
/* Configure serial settings */
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
// Display configured settings
//Debug.WriteLine("Serial port configured successfully.");
// Create cancellation token object to close I/O operations when closing the device
ReadCancellationTokenSource = new CancellationTokenSource();
return serialPort;
}
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return null;
}
这是我用来写入串行设备的代码:
private async void sendString(string stringToSend)
{
try
{
if (serialPort != null)
{
// Create the DataWriter object and attach to OutputStream
dataWriteObject = new DataWriter(serialPort.OutputStream);
//Launch the WriteAsync task to perform the write
await WriteAsync(stringToSend);
}
else
{
status.Text = "Select a device and connect";
}
}
catch (Exception ex)
{
status.Text = "sendString: " + ex.Message;
}
finally
{
// Cleanup once complete
if (dataWriteObject != null)
{
dataWriteObject.DetachStream();
dataWriteObject = null;
}
}
}
/// <summary>
/// WriteAsync: Task that asynchronously writes data from the input text box 'sendText' to the OutputStream
/// </summary>
/// <returns></returns>
private async Task WriteAsync(string stringToWrite)
{
Task<UInt32> storeAsyncTask;
dataWriteObject.WriteString(stringToWrite);
// Launch an async task to complete the write operation
storeAsyncTask = dataWriteObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
counter++;
status.Text = stringToWrite + ", ";
status.Text += "bytes written successfully! (" + counter + ")";
}
}
任何帮助/建议都将不胜感激。
我重写了我的sendString方法,并使用using语句对其进行了简化:
private async void sendString(string stringToSend)
{
using (DataWriter dataWriter = new DataWriter(serialPort.OutputStream))
{
dataWriter.WriteString(stringToSend);
await dataWriter.StoreAsync();
//await dataWriter.FlushAsync();
dataWriter.DetachStream();
counter++;
status.Text = stringToSend + ", ";
status.Text += "bytes written successfully! (" + counter + ")";
}
}
然而,问题仍然存在。
答案 0 :(得分:1)
我尝试使用UART引脚代替USB线来写入数据,并且没有遇到任何问题。这是我用来在Pi上初始化UART总线的代码,我使用了与上面使用相同的函数来编写:
private async Task<SerialDevice> initializeSerial()
{
try
{
string aqs = SerialDevice.GetDeviceSelector("UART0");
var dis = await DeviceInformation.FindAllAsync(aqs);
SerialDevice serialPort = await SerialDevice.FromIdAsync(dis[0].Id);
/* Configure serial settings */
serialPort.WriteTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.ReadTimeout = TimeSpan.FromMilliseconds(1000);
serialPort.BaudRate = 9600;
serialPort.Parity = SerialParity.None;
serialPort.StopBits = SerialStopBitCount.One;
serialPort.DataBits = 8;
serialPort.Handshake = SerialHandshake.None;
status.Text = "Serial port configured succesfully.";
return serialPort;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
status.Text = "Serial port not configured successfully. Are you sure a serial device is connected?";
return null;
}
这并不能完全解决通过USB线发送数据的问题,但是我在这里发布,以防任何人在使用USB端口时遇到类似问题而想尝试这样做。