我是c#编程的新手,我在通过发送“AT”命令并接收响应并在UI上显示来检测调制解调器时遇到问题。该应用程序是在visual studio 2012桌面WPF应用程序中开发的.UI有两个列表一个用于列出可用的COM端口,另一个用于列出调制解调器响应及其相应的com端口。当我运行代码时,它只显示com端口,并且不显示响应形式的调制解调器。当我检查同一设备的响应时通过Hyperterminal,我能够收到“OK”的回复。我已经在下面发布了我的代码。请让我知道我做了哪些错误,以便我的代码可以运行。
CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Management;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Windows.Interop;
using System.IO.Ports;
using System.Threading;
using System.IO;
using System.Runtime.CompilerServices;
namespace WWU2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const int WM_DEVICECHANGE = 0x0219; // device change event
private const int DBT_DEVICEARRIVAL = 0x8000; // system detected a new device
private const int DBT_DEVICEREMOVEPENDING = 0x8003; // about to remove, still available
private const int DBT_DEVICEREMOVECOMPLETE = 0x8004; // device is gone
private const int DBT_DEVTYP_PORT = 0x00000003; // serial, parallel
private SerialPort _serialPort;
public MainWindow()
{
InitializeComponent();
foreach (string s in SerialPort.GetPortNames())
{
List1.Items.Add(s);
portcall(s);
}
}
public async void portcall(String s)
{
await this.Dispatcher.InvokeAsync(() =>
{
communication(s);
Thread.Sleep(500);
});
}
public void communication(string s)
{
_serialPort = new SerialPort(s, 9600, Parity.None, 8, StopBits.One);
_serialPort.Handshake = Handshake.RequestToSend;
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.DtrEnable = true;
// String input = "AT";
_serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
SendData();
}
private async void SendData()//string input)
{
String input = "AT";
await this.Dispatcher.InvokeAsync(() =>
{
if (!_serialPort.IsOpen)
{
try
{
_serialPort.Open();
}
catch { List1.Items.Add("cannot open comport"); }
}
_serialPort.Write(input);
});
}
private async void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string data = _serialPort.ReadLine();
// Invokes the delegate on the UI thread, and sends the data that was received to the invoked method.
// ---- The "si_DataReceived" method will be executed on the UI thread which allows populating of the Listbox.
await this.Dispatcher.InvokeAsync(() =>
{
si_DataReceived(data);
});
}
catch (Exception) { }//_serialPort[i].Close(); }
// await Dispatcher.RunAsync(WindowStartupLocation.UI.CoreDispatcherPriority.High,()=> {this.List2.Items.Add(data.Trim())});
}
private void si_DataReceived(string data)
{
this.List2.Items.Add(data.Trim());
this.List2.Items.Add(_serialPort.PortName.ToString());
if (data.IndexOf("OK") > -1)
{
this.List2.Items.Add(data.Trim());
}
if (_serialPort.IsOpen)
{
_serialPort.DataReceived -= sp_DataReceived;
_serialPort.Close();
// _serialPort.Dispose();
}
}
}
}