' Station #10 Torque RS232/Ethernet Logic
Private Sub Stn10TorqueConnectCallback(ByVal ar As IAsyncResult)
Try
Stn10TorqueClientSocket.EndConnect(ar)
Dim Stn10TorqueBytes(4095) As Byte
Stn10TorqueClientSocket.BeginReceive(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length, SocketFlags.None, AddressOf Stn10TorqueReceiveCallback, Stn10TorqueBytes)
Catch sx As SocketException
MsgBox("Stn10 Socket Exception: " & vbCrLf & sx.Message & vbCrLf & sx.SocketErrorCode.ToString)
Catch ax As ArgumentException
MsgBox("Stn10 Argument Exception: " & vbCrLf & ax.Message)
Catch iox As InvalidOperationException
MsgBox("Stn10 Invalid Operation Exception: " & vbCrLf & iox.Message)
Catch ex As Exception
MsgBox("Stn10 General Exception: " & vbCrLf & ex.Message)
End Try
End Sub
Private Sub Stn10TorqueReceiveCallback(ByVal ar As IAsyncResult)
Dim Stn10TorqueBytes() As Byte = CType(ar.AsyncState, Byte())
Dim Stn10TorqueNumBytes As Int32
Try
Stn10TorqueNumBytes = Stn10TorqueClientSocket.EndReceive(ar)
Catch ex As Exception
Stn10EthernetDropCnt = Stn10EthernetDropCnt + 1
Try
Stn10TorqueClientSocket.Shutdown(SocketShutdown.Both)
Stn10TorqueClientSocket.Close()
Catch
End Try
Stn10TorqueEthernetStart()
Dim InitializeText As String = Chr(32)
Dim InitializeBytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(InitializeText)
If Stn10TorqueClientSocket.Connected = True Then
SyncLock Stn10TorqueClientSocket
Try
Stn10TorqueClientSocket.Send(InitializeBytes, InitializeBytes.Length, SocketFlags.None)
Catch
End Try
End SyncLock
Else
Stn10TorqueEthernetStart()
End If
End Try
If Stn10TorqueNumBytes = 0 Then
Try
Stn10TorqueClientSocket.Shutdown(SocketShutdown.Both)
Stn10TorqueClientSocket.Close()
Catch
End Try
Stn10TorqueEthernetStart()
Else
Dim Stn10TorqueRecv As String = Stn10TorqueASCII.GetString(Stn10TorqueBytes, 0, Stn10TorqueNumBytes)
'Clear the buffer
Array.Clear(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length)
'Store data
Dim Stn10TorqueDlg As New Stn10TorqueOneStringDelegate(AddressOf Stn10TorqueStoreReceivedData)
Dim Args() As Object = {Stn10TorqueRecv}
Me.Invoke(Stn10TorqueDlg, Args)
'Begin Receive again
Stn10TorqueClientSocket.BeginReceive(Stn10TorqueBytes, 0, Stn10TorqueBytes.Length, SocketFlags.None, AddressOf Stn10TorqueReceiveCallback, Stn10TorqueBytes)
End If
End Sub
Private Sub Stn10TorqueStoreReceivedData(ByVal Data As String)
' Coded String:
' 1086 Sp:01-01 X.XX Nm XX
' Torque Angle
Dim DecodeTorque As String
Dim DecodeAngle As String
Dim DecodeLenght As Integer
DecodeLenght = Data.Length
If Mid(Data, 1, 1) <> "#" And DecodeLenght >= 31 Then
DecodeTorque = Mid(Data, (DecodeLenght - 30), 15)
DecodeAngle = Mid(Data, (DecodeLenght - 15), 15)
If Stn10ValveTorqueLH = True Then
Stn10LHCoverScrewTextBox.Text = "Torque=" & RemoveSpaces(DecodeTorque) & " Angle=" & RemoveSpaces(DecodeAngle) & " deg"
Stn10ValveTorqueLH = False
Dim TorqueDecodeCnt As Integer
Dim TorqueDecode As String = ""
Dim AngleDecodeCnt As Integer
Dim AngleDecode As String = ""
For TorqueDecodeCnt = 1 To DecodeTorque.Length
If Mid(DecodeTorque, TorqueDecodeCnt, 1) >= Chr(48) And Mid(DecodeTorque, TorqueDecodeCnt, 1) <= Chr(57) Then
TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
ElseIf Mid(DecodeTorque, TorqueDecodeCnt, 1) = Chr(46) Then
TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
End If
Next
For AngleDecodeCnt = 1 To DecodeAngle.Length
If Mid(DecodeAngle, AngleDecodeCnt, 1) >= Chr(48) And Mid(DecodeAngle, AngleDecodeCnt, 1) <= Chr(57) Then
AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
ElseIf Mid(DecodeAngle, AngleDecodeCnt, 1) = Chr(46) Then
AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
End If
Next
' Updates the data base with the information
CmdProdDBStn10 = New OleDbCommand("UPDATE ProductionData SET Stn10LHCoverTorque = @Stn10LHCoverTorque, Stn10LHCoverAngle = @Stn10LHCoverAngle WHERE SerialNumber = @SerialNumber", ConProdDBStn10)
CmdProdDBStn10.Parameters.AddWithValue("@Stn10LHCoverTorque", (TorqueDecode))
CmdProdDBStn10.Parameters.AddWithValue("@Stn10LHCoverAngle", (AngleDecode))
CmdProdDBStn10.Parameters.AddWithValue("@SerialNumber", Stn10CurrentWorkingSerialNumber)
Try
ConProdDBStn10.Open()
Catch
Finally
ConProdDBStn10.Close()
ConProdDBStn10.Open()
End Try
Try
CmdProdDBStn10.ExecuteNonQuery()
PC2PLCReply("LHCSGood", "S10")
Catch ex As Exception
MsgBox("Error updating the production database....", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Updating")
End Try
ConProdDBStn10.Close()
ElseIf Stn10ValveTorqueRH = True Then
Stn10RHCoverScrewTextBox.Text = "Torque=" & RemoveSpaces(DecodeTorque) & " Angle=" & RemoveSpaces(DecodeAngle) & " deg"
Stn10ValveTorqueRH = False
Dim TorqueDecodeCnt As Integer
Dim TorqueDecode As String = ""
Dim AngleDecodeCnt As Integer
Dim AngleDecode As String = ""
For TorqueDecodeCnt = 1 To DecodeTorque.Length
If Mid(DecodeTorque, TorqueDecodeCnt, 1) >= Chr(48) And Mid(DecodeTorque, TorqueDecodeCnt, 1) <= Chr(57) Then
TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
ElseIf Mid(DecodeTorque, TorqueDecodeCnt, 1) = Chr(46) Then
TorqueDecode = TorqueDecode + Mid(DecodeTorque, TorqueDecodeCnt, 1)
End If
Next
For AngleDecodeCnt = 1 To DecodeAngle.Length
If Mid(DecodeAngle, AngleDecodeCnt, 1) >= Chr(48) And Mid(DecodeAngle, AngleDecodeCnt, 1) <= Chr(57) Then
AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
ElseIf Mid(DecodeAngle, AngleDecodeCnt, 1) = Chr(46) Then
AngleDecode = AngleDecode + Mid(DecodeAngle, AngleDecodeCnt, 1)
End If
Next
' Updates the data base with the information
CmdProdDBStn10 = New OleDbCommand("UPDATE ProductionData SET Stn10RHCoverTorque = @Stn10RHCoverTorque, Stn10RHCoverAngle = @Stn10RHCoverAngle WHERE SerialNumber = @SerialNumber", ConProdDBStn10)
CmdProdDBStn10.Parameters.AddWithValue("@Stn10RHCoverTorque", (TorqueDecode))
CmdProdDBStn10.Parameters.AddWithValue("@Stn10RHCoverAngle", (AngleDecode))
CmdProdDBStn10.Parameters.AddWithValue("@SerialNumber", Stn10CurrentWorkingSerialNumber)
Try
ConProdDBStn10.Open()
Catch
Finally
ConProdDBStn10.Close()
ConProdDBStn10.Open()
End Try
Try
CmdProdDBStn10.ExecuteNonQuery()
PC2PLCReply("RHCSGood", "S10")
Catch ex As Exception
MsgBox("Error updating the production database....", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Error Updating")
End Try
ConProdDBStn10.Close()
End If
End If
End Sub
Private Delegate Sub Stn10TorqueOneStringDelegate(ByVal Data As String)
' Thread occupying private sub