Author Topic: Back to back Writes  (Read 842 times)

timryder

  • Jr. Member
  • **
  • Posts: 84
  • Still trying to figure it out
    • View Profile
Back to back Writes
« on: December 17, 2021, 12:52:36 PM »
Hey, i have developed a custom app which uses a lot of direct .Write calls to the COM object, which in this case is a CLX driver.  So when I do the .write is there any reason I can't just go back to back with them? Do I have to put a thread sleep in there? Is the .Write function ASync or Sync? Can I get some result verifying that the write was successful?

Here's my code for example where the HMI is controlling the process of the machine through writing of values. It's necessary this way unfortunately based on the machine design.


Code: [Select]
Public Sub RunUserProgram()
        While Globals.CycleStart
            Dim cmdComplete As Boolean = False
            For i = 0 To MainMDIParent.childFormProgramEditor.MarkingOrder.Controls.Count - 1                           'Ensures they're executed in the right order
                For Each c As Control In MainMDIParent.childFormProgramEditor.MarkingOrder.Controls()                   'Loop through every entity
                    Dim tempControl As EntityBaseClass = c
                    If tempControl.entityID = i And tempControl.EntityCheckState = CheckState.Checked Then

                        MainMDIParent.childFormProgramEditor.SetEntityBgColor(c, Color.LimeGreen)

                        If TypeOf c Is MoveEntity Then
ProcessMoveEntity:
                            Dim entity As MoveEntity = c

                            If entity.Axis = "X" Then
                                PLC.Write("cmdAxis", 1)
                            ElseIf entity.Axis = "Z" Then
                                PLC.Write("cmdAxis", 2)
                            ElseIf entity.Axis = "A" Then
                                PLC.Write("cmdAxis", 3)
                            End If

                            If Globals.loggingMode = Globals.LoggingModeSelection.Debug Then MainMDIParent.childFormMain.UpdateMachineActivity("Sending target data: " & entity.TargetPos)
                            PLC.Write("cmdTarget", entity.TargetPos)            'Send the Target Position to the PLC
                            Thread.Sleep(100)

                            Call MainMDIParent.childFormMain.UpdateMachineActivity("Verifying Command Position " & entity.Axis & ": " & entity.TargetPos)

                            'While Not verifyCmdPos(entity.TargetPos) And Not Globals.AbortActive
                            '    System.Threading.Thread.Sleep(100)
                            '    'targetOK = verifyCmdPos(entity.TargetPos)
                            'End While
RetryVerifyTarget:

                            If Not verifyCmdPos(entity.TargetPos) Then
                                Dim resultDlg As Integer = MessageBox.Show("Failed to verify Taget Position data", "Data Transmit Failure", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
                                If resultDlg = DialogResult.Retry Then
                                    GoTo RetryVerifyTarget
                                Else
                                    GoTo CancelOperation
                                End If
                            End If

                            Call MainMDIParent.childFormMain.UpdateMachineActivity("Moving " & entity.Axis & " to: " & entity.TargetPos)

                            PLC.Write("cmdMoveCommand", 1)            'Write the command down to the PLC to move
                            Thread.Sleep(100)
                            Call MainMDIParent.childFormMain.UpdateMachineActivity("Waiting for Move Done")

                            While Not verifyMoveDone(entity)  'Loop to check that the move is complete and axis is in position
                                If Globals.AbortActive Then GoTo CancelOperation
                                Thread.Sleep(100)
                            End While

                            Call MainMDIParent.childFormMain.UpdateMachineActivity("Target Move Completed")
                        ElseIf TypeOf c Is TriggerBurnEntity Then
                            Dim entiy As TriggerBurnEntity = c
                            Call MainMDIParent.childFormMain.UpdateMachineActivity("Triggering Laser")
                            PLC.Write("cmdLaserTrigger", 1)
                            Thread.Sleep(100)
                        ElseIf TypeOf c Is LoadProgramEntity Then
                            Dim entity As LoadProgramEntity = c
                            Call MainMDIParent.childFormMain.UpdateMachineActivity("Changing Laser Program to: " & entity.ProgramNumber)
                            PLC.Write("cmdProgramNumber", entity.ProgramNumber)
                            Thread.Sleep(100)
                            PLC.Write("cmdPrgChg", 1)
                            Thread.Sleep(100)
                        ElseIf TypeOf c Is LotNumberEntity Then
                            Dim entity As LotNumberEntity = c
                            Call MainMDIParent.childFormMain.UpdateMachineActivity("Updating Lot Number: " & MainMDIParent.childFormMain.tbLotNumber.Text)
                            PLC.Write("cmdLotNumber", Globals.LotNumber)
                            Thread.Sleep(100)
                            PLC.Write("cmdBlockNumber", entity.BlockNumber)
                            Thread.Sleep(100)
                            PLC.Write("cmdProgramNumber", entity.ProgramNumber)
                            Thread.Sleep(100)
                            PLC.Write("cmdLotChange", 1)
                            Thread.Sleep(100)
                        ElseIf TypeOf c Is ChangeMarkingStateEntity Then
                            Dim entity As ChangeMarkingStateEntity = c
                            entity.parseBlockValues(entity.BlockNumbers, entity.StateCommand)
                            System.Threading.Thread.Sleep(100)
                            Exit Sub                                             'Skip the command complete from PLC since we arent using it.
                        End If
                    End If
                Next
            Next


CancelOperation:   'TODO Insert code to handle an error

        End While
    End Sub
Still just trying to figure out this thing called Life.

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Back to back Writes
« Reply #1 on: December 17, 2021, 07:15:11 PM »
Wen using write, it shouldn't be a problem running them back to back. It is the BeginRead and BegiinWrite that you have to be careful with because they go into a queue which can fill up.