Option Strict Off Imports System.Net Imports System.Net.Sockets Imports System.Text Imports System.Runtime.InteropServices 'for joystick Public Class Form1 Inherits System.Windows.Forms.Form Public Declare Function joyGetPos Lib "winmm.dll" Alias "joyGetPos" (ByVal ujoyID As Integer, ByRef pji As JOYINFO) As Integer _ Public Structure JOYINFO Dim dwXpos As Integer Dim dwYpos As Integer Dim dwZpos As Integer Dim dwButtons As Integer End Structure Dim joypos As JOYINFO Dim X As Integer = 0 Dim Y As Byte = 0 Dim dm110 As Integer 'Transmitted Data DM110 - DM114 Dim dm110H As Byte Dim dm110L As Byte 'left/right Dim dm111 As Integer Dim dm111H As Byte Dim dm111L As Byte 'forward/reverse Dim dm112 As Integer Dim dm112H As Byte Dim dm112L As Byte 'Comm watchdog Dim dm113 As Integer Dim dm113H As Byte Dim dm113L As Byte 'buttons 1 - 8 Dim dm114 As Integer Dim dm114H As Byte Dim dm114L As Byte 'PC switches Dim ReceivedData(27) As Integer Friend receiveInProgress As Boolean = False Public Delegate Sub DisplayResultsDelegate(ByVal Text As Integer, ByRef dataBuffer() As Byte) Dim DisplayResultsMarshaler As DisplayResultsDelegate = AddressOf Me.DisplayResults Public udpSocket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) Private ReadyToExchangeData As Boolean = False Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Show() GetLocalHostInfo() End Sub Friend Sub GetLocalHostInfo() 'The IP address and port the remote PLC will send to. Dim localIPAddress As IPAddress = IPAddress.Parse("192.168.1.108") Dim LocalPort As Integer = 9600 Try 'bind the port to the IP address and Use the IP address and port to define a endpoint. Dim LocalIPEndpoint As New IPEndPoint(localIPAddress, LocalPort) 'Tell the UDP socket to use the specified port number. udpSocket.Bind(LocalIPEndpoint) 'Display information about the local computer: TxtLog.AppendText(ControlChars.CrLf & " PC IP Address " & localIPAddress.ToString & " Port " & CStr(LocalPort)) Catch e As Exception MessageBox.Show(e.ToString) End Try End Sub 'Actions to perform on each timer tick: 200 millisecs Private Sub Timer1_Tick(ByVal sender As System.Object, e As System.EventArgs) Handles Timer1.Tick TextBox5.Text = "" joyGetPos(0, joypos) '0 is joystick ID dm110 = joypos.dwXpos / 257 'reduce to 0 - 255 dm110L = CByte(dm110) '0 - FF If dm110 > 128 Then TextBox5.Text = "Right" ElseIf dm110 < 126 Then TextBox5.Text = "Left" End If dm111 = joypos.dwYpos / 257 'reduce to 0 - 255 dm111L = CByte(dm111) '0 - FF If dm111 > 128 Then TextBox5.Text = "Reverse" ElseIf dm111 < 126 Then TextBox5.Text = "Forward" End If If dm111 < 126 And dm110 < 126 Then TextBox5.Text = "Forward Left" ElseIf dm111 < 126 And dm110 > 128 Then TextBox5.Text = "Forward Right" End If dm113 = joypos.dwButtons If dm113 <= 255 Then dm113L = CByte(dm113) 'buttons 1,2,4,8 and 16,32,64,128 only If dm113 = 1 Then TextBox6.Text = "Starter" ElseIf dm113 = 4 Then TextBox6.Text = "Horn" ElseIf dm113 > 0 Then TextBox6.Text = "Button " & dm113 'buttons 1,2,4,8 and 16,32,64,128 and 256,512,1024,2048 Else TextBox6.Text = "" 'no button pressed End If If X = 0 Or X = 2 Or X = 4 Then dm112L = CByte("1") 'watchdog oscilator If X = 1 Or X = 3 Or X = 5 Then dm112L = CByte("0") WriteToPLC() 'write data to PLC X = X + 1 If X >= 5 Then SendReadToPLC() 'send read command to PLC once every 5 transmissions (once per sec) X = 0 End If End Sub Friend Sub ReceiveDataFromRemoteHost() 'Static Dim ReadyToExchangeData As Boolean Try 'If the local and remote IP addresses and ports haven't been specified, do so. If (ReadyToExchangeData = False) Then ReadyToExchangeData = PrepareToExchangeData() 'PrepareToExchangeData(udpSocket) End If If ReadyToExchangeData = True Then If receiveInProgress = False Then receiveInProgress = True 'Set receiveInProgress True to prevent another attempt 'to receive data until this receive attempt has completed. Receive(udpSocket) 'Retrieve any received data. End If Else TxtLog.Text = ControlChars.CrLf & "connection failed" End If Catch e As Exception MessageBox.Show(e.ToString) End Try End Sub 'connect to remote PLC Friend Function PrepareToExchangeData() As Boolean Dim RemoteIpAddress As IPAddress = IPAddress.Parse("192.168.1.100") Dim RemotePort As Integer = 9600 Try ' Use the IP Address and port to create an IPEndpoint. Dim RemoteIpEndPoint As New IPEndPoint(RemoteIpAddress, RemotePort) 'The Connect method is required to specify an endpoint before using Socket.Send. udpSocket.Connect(RemoteIpEndPoint) 'Display information about the remote PLC TxtLog.AppendText(ControlChars.CrLf & "PLC IP Address " & RemoteIpAddress.ToString & " Port " & CStr(RemotePort)) If udpSocket.Connected Then 'If Connect was successful, set ReadyToExchangeData True ReadyToExchangeData = True 'to indicate that transfers may be attempted. Else TxtLog.AppendText(ControlChars.CrLf & "Winsock error: " + Convert.ToString(System.Runtime.InteropServices.Marshal.GetLastWin32Error())) End If Return ReadyToExchangeData Catch e As Exception MessageBox.Show(e.ToString) End Try End Function Private Sub Receive(ByVal udpSocket As Socket) Try 'Call udpSocket's BeginReceive method to start waiting for received data. Dim receivedData As New socketData receivedData.workSocket = udpSocket 'Wait for data from the remote host. 'ReceiveCallback is called when a datagram has arrived. 'Pass receivedData, which contains udpSocket, to the callback. udpSocket.BeginReceive _ (receivedData.socketDataBuffer, 0, receivedData.socketDataBufferLength, 0, _ AddressOf ReceiveCallback, receivedData) Catch e As Exception MessageBox.Show(e.ToString) End Try End Sub Private Sub ReceiveCallback(ByVal ar As IAsyncResult) 'This routine is called after calling BeginReceive, 'when a datagram has arrived at the specified socket. 'ar.asyncstate is a socketData structure that contains the received data. Try 'Convert the passed IAsyncResult to socketData. Dim state As socketData = CType(ar.AsyncState, socketData) 'Read the received data. Dim numberOfBytesReceived As Integer = state.workSocket.EndReceive(ar) 'Call the DisplayResults routine to display the received data in a text box on the Form. 'Use Invoke with a delegate to make the call in the form's thread. 'args is the object containing the information that DisplayResults will use. Dim args() As Object = {numberOfBytesReceived, state.socketDataBuffer} MyBase.Invoke(DisplayResultsMarshaler, args) Catch e As Exception MessageBox.Show(e.ToString) End Try End Sub Private Sub DisplayResults(ByVal Text As Integer, ByRef dataBuffer() As Byte) 'insert data into array For count = 0 To (Text - 1) ReceivedData(count) = dataBuffer(count) 'DM140 - DM144 Next count 'display batt condition DM140 Select Case CByte(ReceivedData(15)) Case 1 TextBox10.Text = ("100%") Case 2 TextBox10.Text = ("75%") Case 4 TextBox10.Text = ("50%") Case 8 TextBox10.Text = ("25%") Case 16 TextBox10.Text = ("0%") Case Else TextBox10.Text = ("err") End Select 'TextBox11.Text = CByte(ReceivedData(16)) 'DM141H TextBox12.Text = CByte(ReceivedData(17)) 'DM141L TextBox13.Text = CByte(ReceivedData(18)) 'DM142H TextBox14.Text = CByte(ReceivedData(19)) 'DM142L TextBox15.Text = CByte(ReceivedData(20)) 'DM143H TextBox16.Text = CByte(ReceivedData(21)) 'DM143L TextBox17.Text = CByte(ReceivedData(22)) 'DM144H TextBox18.Text = Hex(ReceivedData(23)) 'DM144L receiveInProgress = False End Sub Friend Sub SendReadToPLC() 'Finn's command to read 5 words (10 bytes) from PLC starting at DM140 Dim dataToSend(17) As Byte Dim count As Integer = 0 Try If (ReadyToExchangeData = False) Then ReadyToExchangeData = PrepareToExchangeData() End If If ReadyToExchangeData = True Then dataToSend(0) = CByte("128") dataToSend(1) = CByte("00") dataToSend(2) = CByte("02") dataToSend(3) = CByte("00") dataToSend(4) = CByte("100") dataToSend(5) = CByte("00") dataToSend(6) = CByte("00") dataToSend(7) = CByte("108") dataToSend(8) = CByte("01") dataToSend(9) = CByte("00") dataToSend(10) = CByte("01") dataToSend(11) = CByte("01") dataToSend(12) = CByte("130") dataToSend(13) = CByte("00") dataToSend(14) = CByte("140") dataToSend(15) = CByte("00") dataToSend(16) = CByte("00") dataToSend(17) = CByte("05") 'Send the data to the remote PLC. udpSocket.Send(dataToSend, 0, dataToSend.Length, SocketFlags.None) ReceiveDataFromRemoteHost() Else TxtLog.AppendText(ControlChars.CrLf & "connection failed") End If Catch e As Exception MessageBox.Show(e.ToString) End Try End Sub Friend Sub WriteToPLC() 'command to write 5 words (10 Bytes) to PLC Dim dataToSend(27) As Byte 'total command = 27 bytes Dim count As Integer = 0 Try If (ReadyToExchangeData = False) Then ReadyToExchangeData = PrepareToExchangeData() End If If ReadyToExchangeData = True Then dataToSend(0) = CByte("129") dataToSend(1) = CByte("00") dataToSend(2) = CByte("02") dataToSend(3) = CByte("00") dataToSend(4) = CByte("100") dataToSend(5) = CByte("00") dataToSend(6) = CByte("00") dataToSend(7) = CByte("108") dataToSend(8) = CByte("01") dataToSend(9) = CByte("00") dataToSend(10) = CByte("01") dataToSend(11) = CByte("02") dataToSend(12) = CByte("130") dataToSend(13) = CByte("00") dataToSend(14) = CByte("110") dataToSend(15) = CByte("00") dataToSend(16) = CByte("00") dataToSend(17) = CByte("5") dataToSend(18) = CByte(dm110H) dataToSend(19) = CByte(dm110L) dataToSend(20) = CByte(dm111H) dataToSend(21) = CByte(dm111L) dataToSend(22) = CByte(dm112H) dataToSend(23) = CByte(dm112L) dataToSend(24) = CByte(dm113H) dataToSend(25) = CByte(dm113L) dataToSend(26) = CByte(dm114H) dataToSend(27) = CByte(dm114L) 'Send the data to the remote PLC. udpSocket.Send(dataToSend, 0, dataToSend.Length, SocketFlags.None) Else TxtLog.AppendText("connection failed") End If Catch e As Exception MessageBox.Show(e.ToString) End Try End Sub Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Motor Power If TextBox21.Text = ("ON") Then TextBox21.Text = ("OFF") dm114L = (dm114L - CByte("1")) ElseIf TextBox21.Text = ("OFF") Then TextBox21.Text = ("ON") dm114L = (dm114L + CByte("1")) End If End Sub Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Ignition If TextBox22.Text = ("ON") Then TextBox22.Text = ("OFF") dm114L = (dm114L - CByte("2")) ElseIf TextBox22.Text = ("OFF") Then TextBox22.Text = ("ON") dm114L = (dm114L + CByte("2")) End If End Sub Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 'Lights If TextBox23.Text = ("ON") Then TextBox23.Text = ("OFF") dm114L = (dm114L - CByte("4")) ElseIf TextBox23.Text = ("OFF") Then TextBox23.Text = ("ON") dm114L = (dm114L + CByte("4")) End If End Sub Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click 'Enable Starter If TextBox24.Text = ("ON") Then TextBox24.Text = ("OFF") dm114L = (dm114L - CByte("8")) ElseIf TextBox24.Text = ("OFF") Then TextBox24.Text = ("ON") dm114L = (dm114L + CByte("8")) End If End Sub Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 'Misc If TextBox7.Text = ("ON") Then TextBox7.Text = ("OFF") dm114L = (dm114L - CByte("16")) ElseIf TextBox7.Text = ("OFF") Then TextBox7.Text = ("ON") dm114L = (dm114L + CByte("16")) End If End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Me.Dispose() End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub End Class