Guest User

Untitled

a guest
Nov 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.48 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.Net.Sockets
  3. Imports System.Text
  4. Imports System.Threading
  5. Imports System.Xml
  6.  
  7.  
  8. Public Class frmCommander
  9. Public clsXML As New XMLReaderIO
  10.  
  11. Private tcpThread As Thread
  12.  
  13. Private Sub frmCommander_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  14. ' Start thread process for client
  15. tcpThread = New Thread(AddressOf GetData)
  16. tcpThread.Start()
  17.  
  18. txtOutput.Enabled = False
  19.  
  20. clsXML.Read()
  21. End Sub
  22.  
  23. Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
  24. DisconnectSetup()
  25.  
  26. 'TcpClient.Close()
  27. End Sub
  28.  
  29. Private Sub ConnectSetup()
  30. txtOutput.Enabled = True
  31. btnSend.Enabled = True
  32. btnDisconnect.Enabled = True
  33.  
  34. pnl_RemoteManagement.BringToFront()
  35.  
  36. End Sub
  37.  
  38. Private Sub DisconnectSetup()
  39. txtOutput.Enabled = False
  40. txtOutput.Clear()
  41. btnSend.Enabled = False
  42.  
  43. btnDisconnect.Enabled = False
  44.  
  45. pnl_RemoteManagement.SendToBack()
  46. End Sub
  47.  
  48. Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  49. Dim sendData As [Byte]() = Encoding.ASCII.GetBytes(txtOutput.Text & "@" & "Test")
  50.  
  51. 'nStream.Write(sendData, 0, sendData.Length)
  52. End Sub
  53.  
  54. Private Sub picAddLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picAddLoc.Click
  55. frmAddLocation.ShowDialog()
  56. End Sub
  57.  
  58. Private Sub lblAddLocation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblAddLocation.Click
  59. frmAddLocation.ShowDialog()
  60. End Sub
  61.  
  62. Private Sub picConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picConnect.Click
  63. If oLV.SelectedItems.Count = 0 Then
  64. MessageBox.Show("Please select a location to connect to:", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Stop)
  65. Else
  66. Dim strLocationIP As String = oLV.SelectedItems(0).SubItems(3).Text
  67. Dim tcpClient As New TcpClient
  68. Dim nStream As NetworkStream = tcpClient.GetStream
  69.  
  70. Try
  71. tcpClient.Connect(strLocationIP, 1979)
  72.  
  73. nStream = tcpClient.GetStream()
  74.  
  75. If nStream.CanRead And nStream.CanWrite Then
  76. Debug.Print("Connected to " & strLocationIP)
  77.  
  78. ConnectSetup()
  79.  
  80. 'Thread1.Start()
  81. GetData()
  82. End If
  83.  
  84. Catch ex As Exception
  85. Debug.Print(ex.ToString)
  86. End Try
  87. End If
  88.  
  89. End Sub
  90.  
  91. Private Sub GetData()
  92. Thread.Sleep(1000) ' Slight pause
  93. Dim bytesRead(1024) As Byte ' 1K byte
  94.  
  95. ' If nStream.CanRead Then
  96.  
  97. Dim sbMessage As StringBuilder = New StringBuilder()
  98. Dim numberOfBytesRead As Integer = 0
  99.  
  100. ' Do
  101. ' If nStream.CanWrite Then
  102. ' numberOfBytesRead = nStream.Read(bytesRead, 0, bytesRead.Length)
  103. sbMessage.AppendFormat("{0}", Encoding.ASCII.GetString(bytesRead, 0, numberOfBytesRead))
  104. 'Else
  105. Debug.Print("Unable to read data")
  106. ' End If
  107.  
  108. ' Loop While (nStream.DataAvailable)
  109.  
  110. Debug.Print(("You received the following message : " + sbMessage.ToString()))
  111. ' Else
  112. Debug.Print("Data empty")
  113. ' End If
  114. End Sub
  115.  
  116. Private Sub lblConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblConnect.Click
  117. If oLV.SelectedItems.Count = 0 Then
  118. MessageBox.Show("Please select a location to connect to:", "No Selection", MessageBoxButtons.OK, MessageBoxIcon.Stop)
  119. Else
  120. Dim strLocationIP As String = oLV.SelectedItems(0).SubItems(3).Text
  121.  
  122. ' Try
  123. ' TcpClient.Connect(strLocationIP, 1979)
  124.  
  125. 'nStream = TcpClient.GetStream()
  126.  
  127. ' If nStream.CanRead And nStream.CanWrite Then
  128. Debug.Print("Connected to " & strLocationIP)
  129.  
  130. ConnectSetup()
  131. GetData()
  132.  
  133. End If
  134. ' Catch ex As Exception
  135. ' Debug.Print(ex.ToString)
  136. 'End Try
  137. 'End If
  138. End Sub
  139.  
  140. Private Sub picConfigManagement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picConfigManagement.Click
  141. pnl_RemoteManagement.SendToBack()
  142. End Sub
  143.  
  144. Private Sub lblConfigManagement_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblConfigManagement.Click
  145. pnl_RemoteManagement.SendToBack()
  146. End Sub
  147.  
  148. Private Sub btnChangeTZ_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChangeTZ.Click
  149. TZChange.ShowDialog()
  150. End Sub
  151.  
  152. Private Sub btnTaxInNRGTCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTaxInNRGTCheck.Click
  153. Dim senddata As [Byte]() = Encoding.ASCII.GetBytes("TAXINGT" & "@" & "")
  154.  
  155. ' nStream.Write(senddata, 0, senddata.Length)
  156.  
  157. End Sub
  158.  
  159. Private Sub btnTestMsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestMsg.Click
  160. Dim bytData As [Byte]() = Encoding.ASCII.GetBytes("TESTMSG" & "@" & "HI")
  161. ' nStream.Write(bytData, 0, bytData.Length)
  162. ' nStream.Flush()
  163. End Sub
  164. End Class
  165.  
  166. Public Class XMLReaderIO
  167. Dim xmlConfigReader As New XmlTextReader("Locations.xml")
  168. Dim intID As Integer, intLocationNum As Integer, intMachineNum As Integer
  169. Dim strIPAddress As String, strLocationName As String
  170.  
  171. Public Sub Read()
  172. ' Read contents of XML File into the Listview
  173.  
  174. While xmlConfigReader.Read()
  175. If xmlConfigReader.Name = "Location" Then
  176. If xmlConfigReader.HasAttributes Then
  177. While xmlConfigReader.MoveToNextAttribute()
  178. If xmlConfigReader.Name = "ID" Then
  179. intID = CInt(xmlConfigReader.Value)
  180. End If
  181. End While
  182. End If
  183. End If
  184.  
  185. If xmlConfigReader.Name = "LocationNumber" Then intLocationNum = Trim(xmlConfigReader.ReadString())
  186. If xmlConfigReader.Name = "MachineNumber" Then intMachineNum = Trim(xmlConfigReader.ReadString())
  187. If xmlConfigReader.Name = "LocationName" Then strLocationName = Trim(xmlConfigReader.ReadString())
  188. If xmlConfigReader.Name = "IPAddress" Then strIPAddress = Trim(xmlConfigReader.ReadString())
  189.  
  190.  
  191. If xmlConfigReader.Name.ToString() = "Location" And xmlConfigReader.NodeType.ToString() = "EndElement" Then
  192. Debug.Print("ID #" & intID)
  193. Debug.Print("Loc #" & intLocationNum)
  194. Debug.Print("Mac #" & intMachineNum)
  195. Debug.Print("Location Name: " & strLocationName)
  196. Debug.Print("Location IP: " & strIPAddress)
  197.  
  198. ' Add the read data back to the Listview object
  199. Dim itm As New ListViewItem(intLocationNum.ToString, 0)
  200.  
  201. itm.SubItems.Add(intMachineNum.ToString)
  202. itm.SubItems.Add(strLocationName.ToString)
  203. itm.SubItems.Add(strIPAddress.ToString)
  204.  
  205. clsMain.oLV.Items.Add(itm)
  206. End If
  207. End While
  208.  
  209. xmlConfigReader.Close()
  210.  
  211. End Sub
  212.  
  213. End Class
Add Comment
Please, Sign In to add comment