C#串行连接事件处理程序
因为我想添加一个旋转的3D立方体,所以我决定从Winforms切换到WPF。我听说在那里实现3D图形要容易得多。因此,我将代码复制到新的WPF项目中。但是现在我已经遇到了在文本框上可视化我的值的问题。它不起作用。看起来Evenhandler在从com端口接收数据时没有触发事件。我已经测试过我的函数了,它们工作正常C#串行连接事件处理程序,c#,wpf,serial-port,C#,Wpf,Serial Port,因为我想添加一个旋转的3D立方体,所以我决定从Winforms切换到WPF。我听说在那里实现3D图形要容易得多。因此,我将代码复制到新的WPF项目中。但是现在我已经遇到了在文本框上可视化我的值的问题。它不起作用。看起来Evenhandler在从com端口接收数据时没有触发事件。我已经测试过我的函数了,它们工作正常 namespace cube { /// <summary> /// Interaktionslogik für MainWindow.xaml /
namespace cube
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
getAvailablePorts();
serial.DataReceived += Serial_DataReceived;
}
public bool button3clicked = false;
public bool button4clicked = false;
public bool button5clicked = false;
SerialPort serial = new SerialPort();
void getAvailablePorts()
{
List<string> Itemlist = new List<string>();
String[] ports = SerialPort.GetPortNames();
Itemlist.AddRange(ports);
comboBox1.ItemsSource = Itemlist;
}
private void button1_Click_1(object sender, EventArgs e)
{
try
{
if (comboBox1.Text == "" || textBox6.Text == "")
{
MessageBox.Show("Please Select Port Settings");
}
else
{
serial.PortName = comboBox1.Text;
serial.BaudRate = Convert.ToInt32(textBox6.Text);
serial.Parity = Parity.None;
serial.StopBits = StopBits.One;
serial.DataBits = 8;
serial.Handshake = Handshake.None;
serial.Open();
MessageBox.Show("connected!");
}
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Unauthorised Access");
}
}
private void button2_Click_1(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
MessageBox.Show("connection closed!");
serial.Close();
}
private void AppendText(TextBox tB1, TextBox tB2, TextBox tB3, TextBox tB4, string[] text)
{
//try
{
tB1.Text = text[0];
tB2.Text = text[1];
tB3.Text = text[2];
tB4.Text = text[3];
}
//catch (Exception) { }
}
private void Safe_Position1(TextBox tBr1, TextBox tBi1, TextBox tBj1, TextBox tBk1, string[] text)
{
if (button3clicked == true)
{
tBr1.Text = text[0];
tBi1.Text = text[1];
tBj1.Text = text[2];
tBk1.Text = text[3];
button3clicked = false;
}
}
private void Safe_Position2(TextBox tBr2, TextBox tBi2, TextBox tBj2, TextBox tBk2, string[] text)
{
if (button4clicked == true)
{
tBr2.Text = text[0];
tBi2.Text = text[1];
tBj2.Text = text[2];
tBk2.Text = text[3];
button4clicked = false;
}
}
private void Safe_Position3(TextBox tBr3, TextBox tBi3, TextBox tBj3, TextBox tBk3, string[] text)
{
if (button5clicked == true)
{
tBr3.Text = text[0];
tBi3.Text = text[1];
tBj3.Text = text[2];
tBk3.Text = text[3];
button5clicked = false;
}
}
private void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string serialData = serial.ReadLine();
String[] text = serialData.Split(new char[] { ' ' });
AppendText(textBox1, textBox2, textBox3, textBox4, text);
Safe_Position1(textBox5, textBox7, textBox8, textBox9, text);
Safe_Position2(textBox10, textBox11, textBox12, textBox13, text);
Safe_Position3(textBox14, textBox15, textBox16, textBox17, text);
}
private void button3_Click(object sender, EventArgs e)
{
button3clicked = true;
}
private void button4_Click(object sender, EventArgs e)
{
button4clicked = true;
}
private void button5_Click(object sender, EventArgs e)
{
button5clicked = true;
}
}
}
命名空间多维数据集
{
///
///Interaktionslogik für MainWindow.xaml
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
getAvailableReports();
serial.DataReceived+=serial_DataReceived;
}
public bool buttonn3clicked=假;
public bool buttonn4clicked=false;
public bool button5clicked=假;
SerialPort serial=新的SerialPort();
void getAvailableReports()
{
List Itemlist=新列表();
String[]ports=SerialPort.GetPortNames();
Itemlist.AddRange(端口);
comboBox1.ItemsSource=Itemlist;
}
私有无效按钮1\u单击\u 1(对象发送者,事件参数e)
{
尝试
{
如果(comboBox1.Text==“”| | textBox6.Text==“”)
{
MessageBox.Show(“请选择端口设置”);
}
其他的
{
serial.PortName=comboBox1.Text;
serial.BaudRate=Convert.ToInt32(textBox6.Text);
串行。奇偶校验=奇偶校验。无;
serial.StopBits=StopBits.One;
serial.DataBits=8;
串行.握手=握手.无;
serial.Open();
MessageBox.Show(“已连接!”);
}
}
捕获(未经授权的访问例外)
{
MessageBox.Show(“未经授权的访问”);
}
}
私有无效按钮2\u单击\u 1(对象发送者,事件参数e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
MessageBox.Show(“连接已关闭!”);
serial.Close();
}
专用文本(文本框tB1、文本框tB2、文本框tB3、文本框tB4、字符串[]文本)
{
//试一试
{
tB1.Text=Text[0];
tB2.文本=文本[1];
tB3.文本=文本[2];
tB4.文本=文本[3];
}
//捕获(异常){}
}
专用无效安全位置1(文本框tBr1、文本框tBi1、文本框tBj1、文本框tBk1、字符串[]文本)
{
如果(按钮3单击==真)
{
tBr1.Text=文本[0];
tBi1.Text=文本[1];
tBj1.Text=文本[2];
tBk1.Text=文本[3];
按钮3clicked=假;
}
}
专用无效安全位置2(文本框tBr2、文本框tBi2、文本框tBj2、文本框tBk2、字符串[]文本)
{
如果(按钮4单击==真)
{
tBr2.Text=文本[0];
tBi2.Text=文本[1];
tBj2.Text=文本[2];
tBk2.Text=文本[3];
按钮4单击=错误;
}
}
专用无效安全位置3(文本框tBr3、文本框tBi3、文本框tBj3、文本框tBk3、字符串[]文本)
{
如果(按钮5单击==真)
{
tBr3.Text=文本[0];
tBi3.Text=文本[1];
tBj3.Text=Text[2];
tBk3.Text=文本[3];
按钮5单击=错误;
}
}
private void Serial_DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
string serialData=serial.ReadLine();
String[]text=serialData.Split(新字符[]{''});
附录文本(文本框1、文本框2、文本框3、文本框4、文本);
安全位置1(文本框5、文本框7、文本框8、文本框9、文本);
安全位置2(文本框10、文本框11、文本框12、文本框13、文本);
安全位置3(文本框14、文本框15、文本框16、文本框17、文本);
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
button3clicked=真;
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
button4clicked=真;
}
私有无效按钮5_单击(对象发送者,事件参数e)
{
button5clicked=true;
}
}
}
它抛出:
调用线程无法访问此对象,因为另一个
线程拥有它
有人对此有解决方案吗?“Evenhandler在从com端口接收数据时没有触发事件”-您是指
Serial\u DataReceived
事件处理程序还是其他什么<代码>串行。接收到的数据不依赖于平台。可能有一些异常,空的catch{}
忽略了它。是的,你是对的。我有个例外。它说:调用线程无法访问此对象,因为另一个线程拥有它。在我的Winforms项目中,我这样解决它:如果(textBox1.InvokeRequired){textBox1.BeginInvoke(新操作(()=>AppendText(tB1,tB2,tB3,tB4,text));}wpf等价物是Dispatcher.Invoke()的可能副本