当前位置 : 主页 > 编程语言 > c语言 >

vb.net – 阻止自己的函数运行的计时器,除非一步一步地运行

来源:互联网 收集:自由互联 发布时间:2021-06-24
自上周以来,我遇到了一个问题,尽管有高级开发人员的建议. 我想通过COM端口通过计时器向电机发送电动命令. 当我一步一步地或当我添加一个停止点时,这是 好的!但是,当我让计时器和
自上周以来,我遇到了一个问题,尽管有高级开发人员的建议.

我想通过COM端口通过计时器向电机发送电动命令.

>当我一步一步地或当我添加一个停止点时,这是
好的!但是,当我让计时器和程序运行时
他们自己,机动化命令永远不会发生!为什么?命令
应该按照一步一步的方法运行!

我实际上是在没有任何参数的情况下调用方法envoi_commande_motorisation().它只是将特定格式的帧发送到COM端口,以使电机移动.然而,在逐步退出调试时似乎永远不会被调用.这个命令包含在Timer_moto_tick(发送者,e)中作为Timer_moto.tick,它只是我定期验证电机是否在右边方位的定时器,知道它在Demande_etat_motorisation()中的位置.

我将计时器的频率从1000毫秒改为3000毫秒,每次500毫秒,但它没有改变任何东西……

>我添加了一个调用envoi_commande_motorisation()的按钮
手动,它的工作原理!然而,这是手工制作,我想自动化
它.

这是犯罪现场的图片:

Private Sub Timer_moto_Tick(sender As Object, e As EventArgs) Handles Timer_moto.Tick
    'asking for the motor position
    Demande_etat_motorisation()

    'checking if we are acually in the automatize management of the motor and not in the manual way
    If RadioButton_Manuel.Checked = False Then
        'checking if the motor isn't already at the right place
        If ((CDbl(Liste_azimut.Text) <> Val(Aff_position_azimut_source.Text)) Or (CDbl(Liste_elevation.Text) <> Val(Aff_position_site_source.Text))) Then
            'otherwise we change the motor position text and say him to go this way
            lecture_port_comm_moto()
            Liste_azimut.Text = CType(Val(Aff_position_azimut_source.Text), String)
            Liste_elevation.Text = CType(Val(Aff_position_site_source.Text), String)
            envoi_commande_motorisation()

        End If

        envoi_commande_motorisation()

    End If

End Sub

正如评论中所述,我也给出了Demande_etat_motorisation和envoi_commande_motorisation

Public Sub Demande_etat_motorisation()
    Dim i As Integer
    Dim info As String

    If init_en_cours = True Then Exit Sub


    'asking for motor state
    'frame in hexa  : 57 00 00 00 00 00 00 00 00 00 00 1F 20
    tableau_hexa(0) = &H57
    For i = 1 To 10
        tableau_hexa(i) = &H00
    Next i
    tableau_hexa(11) = &H1F
    tableau_hexa(12) = &H20
    'envoi de la données
    info = "sending frames on ports "
    'port 1 -------------------------------------------------------------------------------------------------------------------------
    If etat_port_4.Checked = True And CheckBox_Moto_1.Checked = True Then
        'chosing which port for motorisation
        Try

            Select Case Liste_port_4.Text
                Case Zone_param_comm1.Text
                    Port_serie_1.Write(tableau_hexa, 0, 13)
                    Port_serie_1.ReceivedBytesThreshold = seuil_port_reception_moto
                Case Zone_param_comm2.Text
                    Port_serie_2.Write(tableau_hexa, 0, 13)
                    Port_serie_2.ReceivedBytesThreshold = seuil_port_reception_moto
                Case Zone_param_comm3.Text
                    Port_serie_3.Write(tableau_hexa, 0, 13)
                    Port_serie_3.ReceivedBytesThreshold = seuil_port_reception_moto

            End Select

        Catch ex As Exception
            zone1.Text = "Error Motorisation 1 : " + ex.ToString
        End Try
    End If
    'End If
    'port 2 ----------------------------------------------------------------------------------------------------------------------
    If etat_port_5.Checked = True And CheckBox_Moto_2.Checked = True Then
        'sending the command
        'here we chose which port to write in for the motor
        Try
            'assignation du à ouvrir
            Select Case Liste_port_5.Text
                Case Zone_param_comm1.Text
                    Port_serie_1.Write(tableau_hexa, 0, 13)
                    Port_serie_1.ReceivedBytesThreshold = seuil_port_reception_moto
                Case Zone_param_comm2.Text
                    Port_serie_2.Write(tableau_hexa, 0, 13)
                    Port_serie_2.ReceivedBytesThreshold = seuil_port_reception_moto
                Case Zone_param_comm3.Text
                    Port_serie_3.Write(tableau_hexa, 0, 13)
                    Port_serie_3.ReceivedBytesThreshold = seuil_port_reception_moto

            End Select

        Catch ex As Exception
            zone1.Text = "Error Motorisation 2 : " + ex.ToString
        End Try
    End If

End Sub

Public Sub envoi_commande_motorisation()
    'sending data on serial ports
    'sending 13 chars
    Dim i As Integer
    Dim cjunk, cjunk1 As String
    Dim nombre As Integer
    Dim info As String

    BorneMinAz = Nothing
    BorneMaxAz = Nothing
    BorneMinElev = Nothing
    BorneMaxElev = Nothing

    If init_en_cours = True Then Exit Sub
    If Me.Liste_azimut.Text <> "" And Me.Liste_elevation.Text <> "" Then
        Me.zone1.Text = ""
        'classical command is being sent
        tableau_hexa(0) = &H57
        tableau_hexa(1) = &H30
        'in degree degre
        tableau_hexa(5) = &H1
        tableau_hexa(6) = &H30

        tableau_hexa(10) = &H1
        'final bits for a command
        tableau_hexa(11) = &H2F
        tableau_hexa(12) = &H20
        'azimut calculation
        nombre = CInt(Liste_azimut.Text) + 360
        cjunk1 = CStr(nombre)

        'numbers upper to 100
        cjunk = Mid(cjunk1, 1, 1)
        tableau_hexa(2) = CByte(&H30 + Val(cjunk))
        'having back the decade
        cjunk = Mid(cjunk1, 2, 1)
        tableau_hexa(3) = CByte(&H30 + Val(cjunk))
        'unite
        cjunk = Mid(cjunk1, 3, 1)
        tableau_hexa(4) = CByte(&H30 + Val(cjunk))

        'tilt calculation
        nombre = CInt(Liste_elevation.Text) + 360
        cjunk1 = CStr(nombre)

        'number upper to 100
        cjunk = Mid(cjunk1, 1, 1)
        tableau_hexa(7) = CByte(&H30 + Val(cjunk))
        'decade recuperation
        cjunk = Mid(cjunk1, 2, 1)
        tableau_hexa(8) = CByte(&H30 + Val(cjunk))
        'unite
        cjunk = Mid(cjunk1, 3, 1)
        tableau_hexa(9) = CByte(&H30 + Val(cjunk))
        'affichage de la trame envoyée
        cjunk = ""
        For i = 0 To 12
            cjunk = cjunk + CStr(Hex(tableau_hexa(i))) + " "
        Next
        envoi_azimut_elevation.Text = "frame sent: " + cjunk


        info = "frames being sent on the port : "
        'port 1

        'motorisation 1 --------------------------------------------------------------------------------------------------------------------
        If etat_port_4.Checked = True And CheckBox_Moto_1.Checked = True Then
            'chosing port for motor 1
            Try

                Select Case Liste_port_4.Text
                    Case Zone_param_comm1.Text
                        Port_serie_1.Write(tableau_hexa, 0, 13)
                        Port_serie_1.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm2.Text
                        Port_serie_2.Write(tableau_hexa, 0, 13)
                        Port_serie_2.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm3.Text
                        Port_serie_3.Write(tableau_hexa, 0, 13)
                        Port_serie_3.ReceivedBytesThreshold = seuil_port_reception_moto

                End Select

            Catch ex As Exception
                zone1.Text = "Erreur Motorisation 1 : " + ex.ToString
            End Try

            'affichage de l'info sur l'afficheur noir
            'azimut
            i = CInt(Val(Liste_azimut.Text))
            Select Case i
                Case 0 To 9
                    Affiche_info_azimut.Text = "00" + Format(i, "##0")
                Case 10 To 99
                    Affiche_info_azimut.Text = "0" + Format(i, "##0")
                Case Else
                    Affiche_info_azimut.Text = Format(i, "##0")
            End Select
            Affiche_info_azimut.ForeColor = Color.Green
            'tilt
            i = CInt(Val(Liste_elevation.Text))
            Select Case i
                Case -9 To -1
                    Affiche_info_elevation.Text = "-0" + Format(Abs(i), "##")
                Case 0 To 9
                    Affiche_info_elevation.Text = "0" + Format(i, "##")

                Case Else
                    Affiche_info_elevation.Text = Format(i, "##")
            End Select
            Affiche_info_elevation.ForeColor = Color.Green


        End If
        'port 2 -------------------------------------------------------------------------------------------------------------------------

        If etat_port_5.Checked = True And CheckBox_Moto_2.Checked = True Then
            'envoi de la commande
            'choix du port choisi pour la motorisation 1
            Try
                'assignation du à ouvrir
                Select Case Liste_port_5.Text
                    Case Zone_param_comm1.Text
                        Port_serie_1.Write(tableau_hexa, 0, 13)
                        Port_serie_1.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm2.Text
                        Port_serie_2.Write(tableau_hexa, 0, 13)
                        Port_serie_2.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm3.Text
                        Port_serie_3.Write(tableau_hexa, 0, 13)
                        Port_serie_3.ReceivedBytesThreshold = seuil_port_reception_moto

                End Select

            Catch ex As Exception
                zone1.Text = "Erreur Motorisation 2 : " + ex.ToString
            End Try
            'affichage de l'info sur l'afficheur noir
            'azimut
            i = CInt(Val(Liste_azimut.Text))
            Select Case i
                Case 0 To 9
                    Affiche_info_azimut.Text = "00" + Format(i, "##0")
                Case 10 To 99
                    Affiche_info_azimut.Text = "0" + Format(i, "##0")
                Case Else
                    Affiche_info_azimut.Text = Format(i, "##0")
            End Select
            Affiche_info_azimut.ForeColor = Color.LightGreen
            'elevation
            i = CInt(Val(Liste_elevation.Text))
            Select Case i
                Case -9 To -1
                    Affiche_info_elevation.Text = "-0" + Format(Abs(i), "##")
                Case 0 To 9
                    Affiche_info_elevation.Text = "0" + Format(i, "##")

                Case Else
                    Affiche_info_elevation.Text = Format(i, "##")
            End Select

            Affiche_info_elevation.ForeColor = Color.Green

            'demande de position
            Demande_etat_motorisation()

        End If
        'End If
        '
    End If

End Sub

到目前为止,高级开发人员仍然认为这是一个定时频率问题.

也许我应该处理串口的中断,但我怎么能这样做呢?他告诉我为每个端口设置阈值,以便通过中断开始读取,这样我就可以在缓冲区满时读取它.

编辑:

Diagnostics.Debug.Writeline

据我所知,我在我的程序可能崩溃的地方写了Diagnostics.Debug.Writeline(“这里有一些特定的文字”).实际上,在拿起终端上写的行后,我注意到它是Serialports.Writeline(,,)会产生问题.

确实有以下代码和调试行:

Private Sub Timer_moto_Tick(sender As Object, e As EventArgs) Handles Timer_moto.Tick
    'asking for the motor position
    Demande_etat_motorisation()

    'checking if we are acually in the automatize management of the motor and not in the manual way
    If RadioButton_Manuel.Checked = False Then

        Diagnostics.Debug.WriteLine("test if we are at the right place")

        If ((CDbl(Liste_azimut.Text) <> Val(Aff_position_azimut_source.Text)) Or (CDbl(Liste_elevation.Text) <> Val(Aff_position_site_source.Text))) Then
            lecture_port_comm_moto()
            Liste_azimut.Text = CType(Val(Aff_position_azimut_source.Text), String)
            Liste_elevation.Text = CType(Val(Aff_position_site_source.Text), String)
            envoi_commande_motorisation()

            Diagnostics.Debug.WriteLine("we have just send the orders to the camera")

        End If
    End If
End Sub

在终端上读取每个调试行,我在envoi_commande_motorisation()中添加了一些

Public Sub envoi_commande_motorisation()
'sending data on serial ports
'sending 13 chars
    Dim i As Integer
    Dim cjunk, cjunk1 As String
    Dim nombre As Integer
    Dim info As String

    BorneMinAz = Nothing
    BorneMaxAz = Nothing
    BorneMinElev = Nothing
    BorneMaxElev = Nothing

    Diagnostics.Debug.WriteLine("We just get into envoi_commande_motorisation()")

    If init_en_cours = True Then Exit Sub
    If Me.Liste_azimut.Text <> "" And Me.Liste_elevation.Text <> "" Then
        Me.zone1.Text = ""
    'classical command is being sent
        tableau_hexa(0) = &H57
        tableau_hexa(1) = &H30
        'transforming it in degree
        tableau_hexa(5) = &H1
        tableau_hexa(6) = &H30
        'again
        tableau_hexa(10) = &H1
        'final bits for a command

        tableau_hexa(11) = &H2F
        tableau_hexa(12) = &H20
        'azimut calculation
        nombre = CInt(Liste_azimut.Text) + 360
        cjunk1 = CStr(nombre)

        'number bigger than 100
        cjunk = Mid(cjunk1, 1, 1)
        tableau_hexa(2) = CByte(&H30 + Val(cjunk))
        'taking the decade
        cjunk = Mid(cjunk1, 2, 1)
        tableau_hexa(3) = CByte(&H30 + Val(cjunk))
        'unity
        cjunk = Mid(cjunk1, 3, 1)
        tableau_hexa(4) = CByte(&H30 + Val(cjunk))

        'elevation calculation
        nombre = CInt(Liste_elevation.Text) + 360
        cjunk1 = CStr(nombre)

        'number bigger than 100
        cjunk = Mid(cjunk1, 1, 1)
        tableau_hexa(7) = CByte(&H30 + Val(cjunk))
        'taking the decade
        cjunk = Mid(cjunk1, 2, 1)
        tableau_hexa(8) = CByte(&H30 + Val(cjunk))
        'units
        cjunk = Mid(cjunk1, 3, 1)
        tableau_hexa(9) = CByte(&H30 + Val(cjunk))
        displaying the frame sent 
        cjunk = ""
        For i = 0 To 12
            cjunk = cjunk + CStr(Hex(tableau_hexa(i))) + " "
        Next
        envoi_azimut_elevation.Text = "Trame envoi : " + cjunk


        Diagnostics.Debug.WriteLine("We are going to get into the writes")


        info = "sending data on ports "
        'port 1
        'motorisation 1 --------------------------------------------------------------------------------------------------------------------
        If etat_port_4.Checked = True And CheckBox_Moto_1.Checked = True Then
            'chosing ports
            Try

                Select Case Liste_port_4.Text
                    Case Zone_param_comm1.Text
                        Port_serie_1.Write(tableau_hexa, 0, 13)
                    'Port_serie_1.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm2.Text
                        Port_serie_2.Write(tableau_hexa, 0, 13)
                    'Port_serie_2.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm3.Text
                        Port_serie_3.Write(tableau_hexa, 0, 13)
                    'Port_serie_3.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm4.Text
                        Diagnostics.Debug.WriteLine("we are going to write in COM port 4")

                        Port_serie_4.Write(tableau_hexa, 0, 13)
                        Diagnostics.Debug.WriteLine("We just wrote in COM Port 4")
                        Timer_moto.Start()
                    'Port_serie_4.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm5.Text
                        Port_serie_5.Write(tableau_hexa, 0, 13)
                    'Port_serie_5.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm6.Text
                        Port_serie_6.Write(tableau_hexa, 0, 13)
                        'Port_serie_6.ReceivedBytesThreshold = seuil_port_reception_moto
                End Select

            Catch ex As Exception
                zone1.Text = "Erreur Motorisation 1 : " + ex.ToString
            End Try

            'displaying azimut infomations on screen
            'azimut
            i = CInt(Val(Liste_azimut.Text))
            Select Case i
                Case 0 To 9
                    Affiche_info_azimut.Text = "00" + Format(i, "##0")
                Case 10 To 99
                    Affiche_info_azimut.Text = "0" + Format(i, "##0")
                Case Else
                    Affiche_info_azimut.Text = Format(i, "##0")
            End Select
            Affiche_info_azimut.ForeColor = Color.Green
            'elevation
            i = CInt(Val(Liste_elevation.Text))
            Select Case i
                Case -9 To -1
                    Affiche_info_elevation.Text = "-0" + Format(Abs(i), "##")
                Case 0 To 9
                    Affiche_info_elevation.Text = "0" + Format(i, "##")

                Case Else
                    Affiche_info_elevation.Text = Format(i, "##")
            End Select
            Affiche_info_elevation.ForeColor = Color.Green


        End If
        'port 2 -------------------------------------------------------------------------------------------------------------------------

        If etat_port_5.Checked = True And CheckBox_Moto_2.Checked = True Then
            'sending command
            'chosing port
            Try

                Select Case Liste_port_5.Text
                    Case Zone_param_comm1.Text
                        Port_serie_1.Write(tableau_hexa, 0, 13)
                    'Port_serie_1.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm2.Text
                        Port_serie_2.Write(tableau_hexa, 0, 13)
                    'Port_serie_2.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm3.Text
                        Port_serie_3.Write(tableau_hexa, 0, 13)
                    'Port_serie_3.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm4.Text
                        Port_serie_4.Write(tableau_hexa, 0, 13)
                    'Port_serie_4.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm5.Text
                        Port_serie_5.Write(tableau_hexa, 0, 13)
                    'Port_serie_5.ReceivedBytesThreshold = seuil_port_reception_moto
                    Case Zone_param_comm6.Text
                        Port_serie_6.Write(tableau_hexa, 0, 13)
                        'Port_serie_6.ReceivedBytesThreshold = seuil_port_reception_moto
                End Select

            Catch ex As Exception
                zone1.Text = "Erreur Motorisation 2 : " + ex.ToString
            End Try
            'affichage de l'info sur l'afficheur noir
            'azimut
            i = CInt(Val(Liste_azimut.Text))
            Select Case i
                Case 0 To 9
                    Affiche_info_azimut.Text = "00" + Format(i, "##0")
                Case 10 To 99
                    Affiche_info_azimut.Text = "0" + Format(i, "##0")
                Case Else
                    Affiche_info_azimut.Text = Format(i, "##0")
            End Select
            Affiche_info_azimut.ForeColor = Color.LightGreen
            'elevation
            i = CInt(Val(Liste_elevation.Text))
            Select Case i
                Case -9 To -1
                    Affiche_info_elevation.Text = "-0" + Format(Abs(i), "##")
                Case 0 To 9
                    Affiche_info_elevation.Text = "0" + Format(i, "##")

                Case Else
                    Affiche_info_elevation.Text = Format(i, "##")
            End Select

            Affiche_info_elevation.ForeColor = Color.Green

            'demande de position
            'Demande_etat_motorisation()

        End If
        'End If
        '
    End If
End Sub

每个Diagnostics.Debug.WriteLine消息都会在终端上读取并显示,但相机不会移动.因此,当链接到COM端口4时,我认为是严重问题的Port_serie_4.Write(tableau_hexa,0,13),
 即使其余代码延迟如下所示.
奇怪的是,如果我们在正确的地方,它总是写我测试.即使我给它命令移动,就好像他认为我们在正确的位置,除非点击要求envoi_commande_motorisation()的按钮.
.

延迟

我在这里也加了一个延迟:

Case Zone_param_comm4.Text
                    Diagnostics.Debug.WriteLine("we are going to write in COM port 4")

                    Port_serie_4.Write(tableau_hexa, 0, 13)
                    System.Threading.Thread.Sleep(500)
                    Diagnostics.Debug.WriteLine("We just wrote in COM Port 4")

然而,目前没有任何改变.

也许我应该删除这个问题代码的某些部分或从上面删除.随意提出建议并感谢您为解决此问题所带来的所有帮助.

我想你曾经试图在这里做太多.也许你需要从编写一个真正有效的非常简单的程序开始,然后构建它.

首先,使用表单和按钮创建一个新项目.单击该按钮时,将打开COM端口.

如果可行,您只需在打开端口后发送一个命令.将相机设置在给定位置的命令.

如果可行,则添加一个TextBox,您可以在其中输入所需位置.因此,当您单击按钮时,它会将相机移动到该位置.

如果有效,……

而且你一直在这样建立,直​​到你发现出了什么问题

网友评论