RS232 port.close崩溃程序 [英] RS232 port.close crashes program

查看:70
本文介绍了RS232 port.close崩溃程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,
我有一个程序通过RS232从秤上读取重量.规模一旦稳定,就只会不断地抽出数据.因此,在加载表单时,我禁用了RS232事件处理程序.扫描与称重相关的序列号后,我启用rs232接收事件并将值读取到文本框中.读取完成后,我再次禁用了接收事件.这一切都完美地工作.

当我尝试关闭端口时,问题就来了.

Good day,
I have a program reading a weight from a scale through RS232. The scale, once stable, just keeps pumping data out. So when the form loads I disable the RS232 event handler. After the serial number related to the weighing is scanned I enable the rs232 receive event and read the values into a textbox. After the read is completed I disable the receive event again. This all works perfectly.

The problem comes when I try to close the Port.

Private Sub frmBoxCheck_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            ''AddHandler MyPort.DataReceived, AddressOf MyPort_DataReceived
            ''MyPort.ReadBufferSize = 0
            ''MyPort.PortName = "COM1"
            RemoveHandler MyPort.DataReceived, AddressOf MyPort_DataReceived
            MyPort.DiscardInBuffer()
            If MyPort.IsOpen = True Then MyPort.Close()



            If dat.IsTableLoaded("AccessorySetup") = True Then dat.DS.Tables.Remove("AccessorySetup")
            If dat.IsTableLoaded("SCALES") = True Then dat.DS.Tables.Remove("SCALES")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub



If MyPort.IsOpen = True Then MyPort.Close()
当程序命中myport.close时,整个应用程序将挂起,并且使其再次运行的唯一方法是重新引导计算机.您无法停止该过程.请参见下面的代码,欢迎提出任何想法.谢谢




If MyPort.IsOpen = True Then MyPort.Close()
When the program hits myport.close the whole app hangs and the only way to make it work again is to reboot the machine. You cannot stop the process. Please see code below any ideas are welcome. thanks


<pre lang="msil">Private Sub frmBoxCheck_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ''Disable the rs232 read event
        RemoveHandler MyPort.DataReceived, AddressOf MyPort_DataReceived
        ''Setup Rs232 port
        Rs232_Setup()
    End Sub
    Sub Rs232_Setup()
        Try
            If dat.ScaleLoad_Table("SELECT * FROM [ACCESSORY].[dbo].[Setup]", "AccessorySetup") = True Then
                Dim intScaleID As Integer = 0
                intScaleID = dat.DS.Tables("AccessorySetup").Rows(2).Item("SetupValue")
                If dat.ScaleLoad_Table("SELECT * From [ACCESSORY].[dbo].[Scales] WHERE ScaleID=''" & intScaleID & "''", "SCALES") = True Then
                    Dim dtScale = dat.DS.Tables("SCALES")
                    If dtScale.Rows.Count = 1 Then
                        MyPort.PortName = dtScale.Rows(0).Item("LastComPort").ToString
                        Dim strRS232Full As String
                        Dim strRS232Setup(20) As String
                        strRS232Full = dtScale.Rows(0).Item("RS232_Setup").ToString
                        strRS232Setup = strRS232Full.Split(",")
                        MyPort.PortName = dtScale.Rows(0).Item("LastComPort").ToString
                        MyPort.BaudRate = strRS232Setup(0)
                        MyPort.DataBits = strRS232Setup(1)
                        ''Stop bit
                        Select Case strRS232Setup(2)
                            Case "0"
                                MyPort.StopBits = Ports.StopBits.None
                            Case "1"
                                MyPort.StopBits = Ports.StopBits.One
                            Case "1.5"
                                MyPort.StopBits = Ports.StopBits.OnePointFive
                            Case "2"
                                MyPort.StopBits = Ports.StopBits.Two
                        End Select
                        ''Parity
                        Select Case strRS232Setup(3)
                            Case "None"
                                MyPort.Parity = Ports.Parity.None
                            Case "Even"
                                MyPort.Parity = Ports.Parity.Even
                            Case "Odd"
                                MyPort.Parity = Ports.Parity.Odd
                            Case "Mark"
                                MyPort.Parity = Ports.Parity.Mark
                            Case "Space"
                                MyPort.Parity = Ports.Parity.Space
                        End Select
                        MyPort.ReadTimeout = 1000
                        MyPort.Handshake = Ports.Handshake.None
                        MyPort.Open()
                    Else
                        MessageBox.Show("Scale not found or too many values returned.")
                    End If
                Else
                    MessageBox.Show("Cannot Load Scale table.")
                End If
            Else
                MessageBox.Show("Cannot Load Accessory setup table.")
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Private Sub MyPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles MyPort.DataReceived
        Try
            ''Pause for 100ms
            Threading.Thread.Sleep(100)
            ''Add values read to a string builder
            myStringBuilder.Append(MyPort.ReadLine)
            ''Pass the string builder to the update text box delegate
            Me.Invoke(New EventHandler(AddressOf UpdateTextBox))
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub UpdateTextBox()
        Try
            Dim arryCnt As Integer = 11
            ''Write the entire mess into a texbox and trim off the edges
            txtRead.AppendText(";" & myStringBuilder.ToString)
            myStringBuilder.Remove(0, myStringBuilder.Length)
            ''Release port
            ''remove receive event handler
            RemoveHandler MyPort.DataReceived, AddressOf MyPort_DataReceived
            ''Clear the input buffer in the driver
            '' MyPort.DiscardInBuffer()
            ''Close the port
            ''If MyPort.IsOpen = True Then MyPort.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


推荐答案

鉴于这只是RS232数据,因此数据速率将非常低.我很想保持接收状态连续,并更新我的课程中的当前权重"值(可能是静态的,因为每个端口只能有一个).然后,您的问题应该消失了:接收到数据后,处理程序仍然存在.应该花很长时间检查...
Given that this is only RS232 data, the data rate is going to be pretty low. I would be very tempted to keep the receive enabled continuously, and update a "current weight" value (probably static since there can be only one per port) in my class. Then your problem should disappear: then handler still exists when data is received. It shouldn''t take long to check...


这篇关于RS232 port.close崩溃程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆