我正在尝试编写一个简单的Forms应用程序,以通过USB端口将数据发送到监听终端设备。有人告诉我,我需要使用STX ETX LRC成帧发送数据,但是我不知道这意味着什么。我是我们公司的软件测试人员,不熟悉通过USB进行数据传输。有谁可以帮助我吗?这是我当前的表单代码:
private void sendRequestButton_Click(object sender, EventArgs e)
{
try
{
_serialPort = new SerialPort
{
PortName = portsDropdown.Text,
BaudRate = 19200,//connectionTypeDropdown.Text.Equals(Usb) ? 115200 : 19200,
DataBits = 8,
Parity = Parity.None,
StopBits = StopBits.One,
};
_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
_serialPort.Open();
_serialPort.Write(requestTextbox.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, @"Caught Exception:", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
_serialPort.Close();
}
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
var serialPort = (SerialPort)sender;
_response = serialPort.ReadExisting();
Debug.Print(_response);
}
答案 0 :(得分:-1)
这就是我用来通过C#中的串行端口进行连接的内容。这是在WPF中,因此您必须进行一些调整。
using System;
using System.IO.Ports;
using System.Windows;
using System.Windows.Input;
namespace SerialTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private SerialPort port = new SerialPort();
int intBaud = 0;
string strComport = "";
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
string[] ports = SerialPort.GetPortNames();
cbCom.ItemsSource = ports;
}
private void StartUp()
{
int intbaud;
if (int.TryParse(cbBaud.SelectionBoxItem.ToString(), out intbaud))
{
intBaud = intbaud;
strComport = cbCom.SelectedItem.ToString();
SerialStart();
}
else
{
MessageBox.Show("Enter a valid Baudrate");
}
}
private void SerialStart()
{
try
{
port.BaudRate = int.Parse(cbBaud.SelectionBoxItem.ToString());
port.DataBits = 8;
port.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "One");
port.Parity = (Parity)Enum.Parse(typeof(Parity), "None");
port.PortName = cbCom.SelectedItem.ToString();
port.DataReceived += new SerialDataReceivedEventHandler(SerialReceive);
port.Handshake = Handshake.None;
if (port.IsOpen) port.Close();
port.Open();
}
catch (Exception ex)
{
txtTerm.AppendText(ex.ToString());
}
}
public enum LogMsgType { Incoming, Outgoing, Normal, Warning, Error };
private void Log(LogMsgType msgtype, string msg)
{
try
{
txtTerm.Dispatcher.Invoke(new EventHandler(delegate
{
txtTerm.AppendText(msg);
}));
}
catch (Exception ex)
{
ex.ToString();
}
}
private void SerialReceive(object sender, SerialDataReceivedEventArgs e)
{
if (!port.IsOpen) return;
string data = port.ReadExisting();
this.Dispatcher.Invoke(() =>
{
txtTerm.AppendText(data);
txtTerm.ScrollToEnd();
});
}
private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return && port.IsOpen)
{
try
{
port.WriteLine(txtInput.Text + "\r\n");
}
catch (Exception ex)
{
this.Dispatcher.Invoke(() =>
{
txtTerm.AppendText(ex.ToString()); ;
});
}
this.Dispatcher.Invoke(() =>
{
txtTerm.AppendText(txtInput.Text + "\n");
txtInput.Text = "";
});
}
}
}
}
修改 对于STX和ETC来说,您还必须将字符转换为字节 https://www.codeproject.com/questions/1107562/sending-ascii-control-stx-and-etx-in-csharp