串行端口在Visual C#2010中不起作用 [英] Serial Port not working in Visual C# 2010
问题描述
我正在努力让我的串口工作。 第一部分工作得很好。 它找到所有可用的通信端口,我可以正确选择端口。 但是当我尝试向其发送字符时,接收事件不会发生。 这是我的代码
。 我做错了什么? 现在我只想显示收到消息框的内容,但理想情况下我想解析数据。
I am trying to get my Serial port to work. The first part is working great. It finds all available comm ports and I can correctly select the port. But when I try to send characters to it, the receive event does not happen. here is my code. what am I doing wrong? Right now I just want to display what is being received into a Message Box, but ideally I want to parse through the data.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (string ports in System.IO.Ports.SerialPort.GetPortNames())
{
COMPortsBox.Items.Add(ports);
}
COMPortsBox.Sorted = true;
}
private void COMPortsBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.RtsEnable = false;
serialPort1.DtrEnable = false;
serialPort1.Close();
System.Threading.Thread.Sleep(250);
}
serialPort1.PortName = COMPortsBox.Text;
serialPort1.BaudRate = 115200; // Default for 115200 bit/s, 8 data bits, no parity, 1 stop bit
serialPort1.WriteTimeout = 2000; // Max time to wait for CTS = 2 sec.
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
}
private void DisplayText(object sender, EventArgs e)
{
MessageBox.Show(RxString);
}
private void serialPort1_DataReceived
(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
}
}
推荐答案
你好engirerwolf,
Hello engirerwolf,
我想让我的串口工作。 第一部分工作得很好。 它找到所有可用的通信端口,我可以正确选择端口。 但是当我尝试向其发送字符时,接收事件不会发生。 这是我的代码
。 我做错了什么? 现在我只想显示收到消息框的内容,但理想情况下我想解析数据。
I am trying to get my Serial port to work. The first part is working great. It finds all available comm ports and I can correctly select the port. But when I try to send characters to it, the receive event does not happen. here is my code. what am I doing wrong? Right now I just want to display what is being received into a Message Box, but ideally I want to parse through the data.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string RxString;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (string ports in System.IO.Ports.SerialPort.GetPortNames())
{
COMPortsBox.Items.Add(ports);
}
COMPortsBox.Sorted = true;
}
private void COMPortsBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (serialPort1.IsOpen)
{
serialPort1.RtsEnable = false;
serialPort1.DtrEnable = false;
serialPort1.Close();
System.Threading.Thread.Sleep(250);
}
serialPort1.PortName = COMPortsBox.Text;
serialPort1.BaudRate = 115200; // Default for 115200 bit/s, 8 data bits, no parity, 1 stop bit
serialPort1.WriteTimeout = 2000; // Max time to wait for CTS = 2 sec.
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
}
private void DisplayText(object sender, EventArgs e)
{
MessageBox.Show(RxString);
}
private void serialPort1_DataReceived
(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
}
}
这篇关于串行端口在Visual C#2010中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!