读取串口时抛出IO操作中止错误(IO operation aborted error thrown while reading serial port)
我们正在尝试使用.Net串口类读取连接到串行端口的外部设备(本例中为称重秤)写入的数据。
首先我们初始化串口如下:
InitializeSerialPort() { if ((serialPort != null) && (serialPort.IsOpen)) { serialPort.Close(); serialPort.Dispose(); serialPort = null; } serialPort = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One) { Handshake = Handshake.None }; serialPort.DataReceived += serialPort_DataReceived; serialPort.NewLine = "\r"; }
我们使用后台工作线程通过在串行端口上发送命令(由称重秤理解)来连续轮询设备。 一旦我们发送命令,连接到串行端口的设备就会响应响应输出。 我们调用SerialPort类的ReadLine API来获取设备在DataReceived事件中写入的串行端口上的数据,如下面的代码片段所示:
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { data = serialPort.ReadLine(); } catch(System.IO.IOException ex) { //since serial port reading threw an error so there is no value to be parsed hence exit the function. return; } //if no error then parse the data received }
我正在使用.Net framework 4.0的
System.IO.Ports.SerialPort
类。 我可以看到很多人在其他论坛上发布此问题,但没有具体的解决方案。 他们中的一些人将.Net串口类称为bug,至今尚未由Microsoft修复。 提到此错误的论坛之一就在这里We are trying to read data written by an external device (weighing scale in this case) connected to serial port using .Net serial port class.
First we initialize the serial port as below:
InitializeSerialPort() { if ((serialPort != null) && (serialPort.IsOpen)) { serialPort.Close(); serialPort.Dispose(); serialPort = null; } serialPort = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One) { Handshake = Handshake.None }; serialPort.DataReceived += serialPort_DataReceived; serialPort.NewLine = "\r"; }
We are using background worker thread to poll the device on continuous interval by sending a command(understood by the weighing scale) on the serial port. As soon as we send the command the device connected to serial port reacts with a response output. We call ReadLine API of SerialPort class to get the data present on the serial port written by the device in the DataReceived event as shown in the code snippet below :
private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { data = serialPort.ReadLine(); } catch(System.IO.IOException ex) { //since serial port reading threw an error so there is no value to be parsed hence exit the function. return; } //if no error then parse the data received }
I'm using
System.IO.Ports.SerialPort
class of .Net framework 4.0. I can see a number of people posting this issue on other forums but with no specific resolution. Some of them terming .Net Serial port class as buggy which has not been fixed by Microsoft till date. One of the forums where this error is mentioned is hereI also tried the solution posted here but of no help. I need some input if any one else has come across this issue or its resolution.
原文:https://stackoverflow.com/questions/21305392
最满意答案
相关问答
更多-
使用FindString方法获取ComboBox中第一个项目的索引,文本为“已售出”。 然后将SelectedIndex设置为: comboBox1.SelectedIndex = comboBox1.FindString("Sold"); 如果找不到文本“已售出”,则所选索引将为-1,默认情况下组合框将显示空白项。 Use the FindString method to get the index of the first item in the ComboBox with the text "Sol ...
-
通过ComboBox选择数据库(Database Selection Via ComboBox)[2022-06-29]
你需要这个查询; string Sql = "SELECT * FROM sys.databases"; You need this query; string Sql = "SELECT * FROM sys.databases"; -
ComboBox作为数据库导航器(ComboBox as database navigator)[2023-06-13]
是的你可以。 您可以使用QSqlQueryModel或QSqlTableModel将数据库数据导入项目模型。 您可以使用QComboBox::setModel()将模型设置为组合框。 您可以使用QComboBox::setModelColumn()选择组合框中的哪一列显示 请参阅QComboBox文档 Yes, you can. You can use QSqlQueryModel or QSqlTableModel to get the database data into a item model. T ... -
为你的Combobox制作两种方法,并在这里召唤你的膳食组合框就是样本 public ordermeal() { InitializeComponent(); yourMealtypeCombobox(); } public void yourMealtypeCombobox() { DataTable dt = md.GetALLMealTypes(); // ...
-
我认为这是你的问题 cmd1.Parameters.Add(new SqlParameter("@Initials", comboBox1.SelectedIndex.ToString())); 改为 cmd1.Parameters.Add(new SqlParameter("@Initials", comboBox1.SelectedValue.ToString())); 要么 cmd1.Parameters.Add(new SqlParameter("@Initials", comboBox1.Se ...
-
在你的支持bean(示例中的YourBean)中,你应该有一个字符串数组(或者带有getter方法的对象,它返回你想要的String)。 例如,假设您在辅助bean中有以下代码: private ArrayList
logins; // read from DB private String selectedLogin; // this will hold the selected value // this method will be called by the JSF framewo ... -
Combobox数据库值(Combobox Database value)[2021-12-18]
在DevExpress上看一下这个演示 。 您可能想要搜索更多有关Cascading Comboboxes的信息 Take a look at this demo on DevExpress. You might want to search more about Cascading Comboboxes -
1)从Db填充数据( finally block使用finally block来关闭打开的对象,因为此代码在所有情况下都会执行) void whatever { Connection conn = null; Statement st1 = null; try { st1 = conn.createStatement(); } catch (SQLException e) { e.printStackTrace(); } finally ...
-
假设数据表中字段的名称(数据库中列的名称)是"ida"并且是string类型,那么您需要执行以下操作: comboBox.ItemsSource = dt.Rows .Cast
() .Select(x => x.Field ("ida")); Assuming that the name of the field in the data table (the name of column in the database) is "ida" and i ... -
首先,为了保存分数而不是实际响应,那么您应该将代码更改为: .Cells(lRow, 1).Value = Me.txtFecha.ListIndex -2 如果你想扭转AE2A的分数: .Cells(lRow, 2).Value = -(Me.txtTime.ListIndex -2) First of all, in order to save the score and not the actual response, then you should change the code to: .Ce ...