skizziks_53

Dweeno Link 2017 v2.0 +user buttons

Aug 26th, 2017
118
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Dweeno Link 2017 v2.0
  2. 'Note: this is the form code for a VB.net project.
  3.  
  4. Imports System.ComponentModel
  5. Imports System.IO
  6.  
  7. Public Class Form1
  8.  
  9. Dim WithEvents MySerialPort As New Ports.SerialPort
  10. Dim outgoingData As String = ""
  11. Dim incomingData As String = ""
  12.  
  13. Dim currentButtonFilePath As String = ""
  14. Dim buttonFileName As String = ""
  15. Dim UserConfig_Button_List As New List(Of Button)
  16. Dim UserConfig_ButtonText_List As New List(Of TextBox)
  17. Dim UserConfig_ButtonCommand_List As New List(Of TextBox)
  18.  
  19.  
  20. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  21. 'The BaudRate of the serial port (set immediately below) and the baud rate set in the Arduino sketch must match.
  22. 'Most Arduino examples leave the baud rate at the lowest setting of 9600, but even the China clone boards can usually run at 115200.
  23. MySerialPort.BaudRate = 115200
  24. 'Don't change the settings below unless you know what they do.
  25. MySerialPort.Parity = Ports.Parity.None
  26. MySerialPort.StopBits = Ports.StopBits.One
  27. MySerialPort.DataBits = 8
  28. MySerialPort.Handshake = Ports.Handshake.None
  29. MySerialPort.RtsEnable = True
  30. MySerialPort.ReadTimeout = 1000
  31.  
  32. ComboBox1.Items.Add(115200)
  33. ComboBox1.Items.Add(57600)
  34. ComboBox1.Items.Add(38400)
  35. ComboBox1.Items.Add(28800)
  36. ComboBox1.Items.Add(19200)
  37. ComboBox1.Items.Add(14400)
  38. ComboBox1.Items.Add(9600)
  39. ComboBox1.SelectedIndex = 0
  40.  
  41. AddHandler MySerialPort.DataReceived, AddressOf DataReceivedHandler
  42. 'OpenFileDialog2.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  43. 'currentButtonFilePath = Application.StartupPath()
  44. currentButtonFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  45.  
  46.  
  47. UserConfig_Button_List.Clear()
  48. UserConfig_Button_List.Add(UPButton1)
  49. UserConfig_Button_List.Add(UPButton2)
  50. UserConfig_Button_List.Add(UPButton3)
  51. UserConfig_Button_List.Add(UPButton4)
  52. UserConfig_Button_List.Add(UPButton5)
  53.  
  54. UserConfig_Button_List.Add(UPButton6)
  55. UserConfig_Button_List.Add(UPButton7)
  56. UserConfig_Button_List.Add(UPButton8)
  57. UserConfig_Button_List.Add(UPButton9)
  58. UserConfig_Button_List.Add(UPButton10)
  59.  
  60. UserConfig_Button_List.Add(UPButton11)
  61. UserConfig_Button_List.Add(UPButton12)
  62. UserConfig_Button_List.Add(UPButton13)
  63. UserConfig_Button_List.Add(UPButton14)
  64. UserConfig_Button_List.Add(UPButton15)
  65.  
  66. UserConfig_Button_List.Add(UPButton16)
  67. UserConfig_Button_List.Add(UPButton17)
  68. UserConfig_Button_List.Add(UPButton18)
  69. UserConfig_Button_List.Add(UPButton19)
  70. UserConfig_Button_List.Add(UPButton20)
  71.  
  72.  
  73. UserConfig_ButtonText_List.Clear()
  74. UserConfig_ButtonText_List.Add(UBText_TextBox1)
  75. UserConfig_ButtonText_List.Add(UBText_TextBox2)
  76. UserConfig_ButtonText_List.Add(UBText_TextBox3)
  77. UserConfig_ButtonText_List.Add(UBText_TextBox4)
  78. UserConfig_ButtonText_List.Add(UBText_TextBox5)
  79.  
  80. UserConfig_ButtonText_List.Add(UBText_TextBox6)
  81. UserConfig_ButtonText_List.Add(UBText_TextBox7)
  82. UserConfig_ButtonText_List.Add(UBText_TextBox8)
  83. UserConfig_ButtonText_List.Add(UBText_TextBox9)
  84. UserConfig_ButtonText_List.Add(UBText_TextBox10)
  85.  
  86. UserConfig_ButtonText_List.Add(UBText_TextBox11)
  87. UserConfig_ButtonText_List.Add(UBText_TextBox12)
  88. UserConfig_ButtonText_List.Add(UBText_TextBox13)
  89. UserConfig_ButtonText_List.Add(UBText_TextBox14)
  90. UserConfig_ButtonText_List.Add(UBText_TextBox15)
  91.  
  92. UserConfig_ButtonText_List.Add(UBText_TextBox16)
  93. UserConfig_ButtonText_List.Add(UBText_TextBox17)
  94. UserConfig_ButtonText_List.Add(UBText_TextBox18)
  95. UserConfig_ButtonText_List.Add(UBText_TextBox19)
  96. UserConfig_ButtonText_List.Add(UBText_TextBox20)
  97.  
  98.  
  99. UserConfig_ButtonCommand_List.Clear()
  100. UserConfig_ButtonCommand_List.Add(UBCom_TextBox1)
  101. UserConfig_ButtonCommand_List.Add(UBCom_TextBox2)
  102. UserConfig_ButtonCommand_List.Add(UBCom_TextBox3)
  103. UserConfig_ButtonCommand_List.Add(UBCom_TextBox4)
  104. UserConfig_ButtonCommand_List.Add(UBCom_TextBox5)
  105.  
  106. UserConfig_ButtonCommand_List.Add(UBCom_TextBox6)
  107. UserConfig_ButtonCommand_List.Add(UBCom_TextBox7)
  108. UserConfig_ButtonCommand_List.Add(UBCom_TextBox8)
  109. UserConfig_ButtonCommand_List.Add(UBCom_TextBox9)
  110. UserConfig_ButtonCommand_List.Add(UBCom_TextBox10)
  111.  
  112. UserConfig_ButtonCommand_List.Add(UBCom_TextBox11)
  113. UserConfig_ButtonCommand_List.Add(UBCom_TextBox12)
  114. UserConfig_ButtonCommand_List.Add(UBCom_TextBox13)
  115. UserConfig_ButtonCommand_List.Add(UBCom_TextBox14)
  116. UserConfig_ButtonCommand_List.Add(UBCom_TextBox15)
  117.  
  118. UserConfig_ButtonCommand_List.Add(UBCom_TextBox16)
  119. UserConfig_ButtonCommand_List.Add(UBCom_TextBox17)
  120. UserConfig_ButtonCommand_List.Add(UBCom_TextBox18)
  121. UserConfig_ButtonCommand_List.Add(UBCom_TextBox19)
  122. UserConfig_ButtonCommand_List.Add(UBCom_TextBox20)
  123.  
  124. End Sub
  125.  
  126. Private Sub DataReceivedHandler(sender As Object, e As EventArgs)
  127. Try
  128. incomingData = MySerialPort.ReadExisting()
  129. MySerialPort.DiscardInBuffer()
  130. DisplayPortData()
  131. Catch x As Exception
  132. MessageBox.Show("Exception = " & x.Message)
  133. End Try
  134. End Sub
  135.  
  136. Private Sub DisplayPortData()
  137. 'This sub is what allows the serial port thread to move data back into the main thread.
  138. If InvokeRequired Then
  139. Invoke(New MethodInvoker(AddressOf DisplayPortData))
  140. Else
  141. Receiving_TextBox.Text = incomingData
  142. Dim stringVal As String = incomingData.Substring(incomingData.Length - 1)
  143. If String.Compare(stringVal, "*", True) = 0 Then
  144. Send_Button.Enabled = True
  145. 'This is where you would add the ability to process through a list of commands, by re-sending the 'next' command in a list of commands.
  146. Else
  147. MessageBox.Show("error! = [" & incomingData & "]")
  148. End If
  149. End If
  150. End Sub
  151.  
  152. Private Sub RSP_Button_Click(sender As Object, e As EventArgs) Handles RSP_Button.Click
  153. ListBox1.Items.Clear()
  154. For Each sp As String In My.Computer.Ports.SerialPortNames
  155. ListBox1.Items.Add(sp)
  156. Next
  157. End Sub
  158.  
  159. Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
  160. If Not String.IsNullOrWhiteSpace(CType(ListBox1.SelectedItem, String)) Then
  161. MySerialPort.Close()
  162. MySerialPort.PortName = CType(ListBox1.SelectedItem, String)
  163. SelectedPort_TextBox.Text = CType(ListBox1.SelectedItem, String)
  164. MySerialPort.BaudRate = Convert.ToInt32(ComboBox1.SelectedItem.ToString)
  165. MySerialPort.Open()
  166. Send_Button.Enabled = True
  167. End If
  168. End Sub
  169.  
  170. Private Sub Send_Button_Click(sender As Object, e As EventArgs) Handles Send_Button.Click
  171. If MySerialPort.IsOpen() Then
  172. incomingData = ""
  173. outgoingData = Sending_TextBox.Text
  174. 'If you wanted the ability to automatically progress through a list of commands,
  175. 'then this is where you would start the process by sending the first command in the list of commands to send.
  176. SendOutgoingData()
  177. Receiving_TextBox.Text = ""
  178. If CheckBox1.CheckState = CheckState.Unchecked Then
  179. Send_Button.Enabled = False
  180. End If
  181. End If
  182. End Sub
  183.  
  184. Private Sub SendOutgoingData()
  185. MySerialPort.Write(outgoingData)
  186. MySerialPort.DiscardOutBuffer()
  187. End Sub
  188.  
  189. Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
  190. If MySerialPort.IsOpen() Then
  191. MySerialPort.Close()
  192. End If
  193. End Sub
  194.  
  195. Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
  196. If ListBox1.SelectedIndex <> -1 Then
  197. If CheckBox1.CheckState = CheckState.Checked Then
  198. Send_Button.Enabled = True
  199. End If
  200. End If
  201. End Sub
  202.  
  203.  
  204. '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  205. 'below is code for the file-configurable send buttons.
  206.  
  207. Private Sub Button7_Click(sender As Object, e As EventArgs) Handles LoadUBF_Button.Click
  208. OpenFileDialog1.InitialDirectory = currentButtonFilePath
  209. OpenFileDialog1.ShowDialog()
  210. End Sub
  211.  
  212. Private Sub OpenFileDialog1_FileOk(sender As Object, e As CancelEventArgs) Handles OpenFileDialog1.FileOk
  213. 'OpenFileDialog1 is used for reading button-config files only.
  214. UBFpath_TextBox.Text = Path.GetFileName(OpenFileDialog1.FileName)
  215. currentButtonFilePath = Path.GetFullPath(OpenFileDialog1.FileName)
  216. ResetButtonNamesArray()
  217. ResetButtonCommandsArray()
  218. If Read_ButtonsFile() = True Then
  219. RefreshUserButtonText()
  220. End If
  221. End Sub
  222.  
  223. Private Sub ResetButtonNamesArray()
  224. For x = 0 To 19
  225. UserConfig_ButtonText_List(x).Text = "[not set]"
  226. Next
  227. End Sub
  228.  
  229. Private Sub ResetButtonCommandsArray()
  230. For x = 0 To 19
  231. UserConfig_ButtonCommand_List(x).Text = "999:*"
  232. Next
  233. End Sub
  234.  
  235. Private Sub UserConfigButton_EventSub(buttonNumber As Integer)
  236. If MySerialPort.IsOpen() Then
  237. If Send_Button.Enabled = True Then
  238. If Not String.IsNullOrWhiteSpace(UserConfig_ButtonCommand_List(buttonNumber).Text) Then
  239. incomingData = ""
  240. Sending_TextBox.Text = UserConfig_ButtonCommand_List(buttonNumber).Text
  241. outgoingData = Sending_TextBox.Text
  242. SendOutgoingData()
  243. Receiving_TextBox.Text = ""
  244. If CheckBox1.CheckState = CheckState.Unchecked Then
  245. Send_Button.Enabled = False
  246. End If
  247. Else
  248. MessageBox.Show("error: no command to send")
  249. End If
  250. End If
  251. Else
  252. MessageBox.Show("error: serial port not open")
  253. End If
  254. End Sub
  255.  
  256. Private Sub RefreshUserButtonText()
  257. For x = 0 To 19
  258. UserConfig_Button_List(x).Text = UserConfig_ButtonText_List(x).Text
  259. Next
  260. End Sub
  261.  
  262. Private Function Read_ButtonsFile() As Boolean
  263. Dim buttonFileLine As String = ""
  264. Dim lineFields As String()
  265. Dim readLines As Boolean = True
  266. Dim counter As Integer = 0
  267. If File.Exists(OpenFileDialog1.FileName) = True Then
  268. Using fileIn As New StreamReader(OpenFileDialog1.FileName)
  269. Do While readLines = True
  270. Try
  271. buttonFileLine = fileIn.ReadLine()
  272. If String.Compare(buttonFileLine, "EOF", True) = 0 Then
  273. readLines = False
  274. ElseIf String.IsNullOrWhiteSpace(buttonFileLine) Then
  275. readLines = False
  276. MessageBox.Show("Error!" & Environment.NewLine & "button config file contains empty line")
  277. Else
  278. Try
  279. lineFields = buttonFileLine.Split("|")
  280. UserConfig_ButtonText_List(counter).Text = lineFields(2)
  281. UserConfig_ButtonCommand_List(counter).Text = lineFields(3)
  282. counter += 1
  283. Catch ex As ArgumentOutOfRangeException
  284. counter += 1
  285. MessageBox.Show("Error!" & Environment.NewLine & "button config file: read failure" & Environment.NewLine & "error on line # " & counter.ToString())
  286. End Try
  287. End If
  288. Catch ex As EndOfStreamException
  289. readLines = False
  290. MessageBox.Show("Error!" & Environment.NewLine & "button config file stream" & Environment.NewLine & "end-of-file marker not present")
  291. End Try
  292. Loop
  293. End Using
  294. Return True
  295. Else
  296. Return False
  297. End If
  298. End Function
  299.  
  300. Private Sub SaveUBF_Button_Click(sender As Object, e As EventArgs) Handles SaveUBF_Button.Click
  301. SaveFileDialog1.InitialDirectory = currentButtonFilePath
  302. SaveFileDialog1.ShowDialog()
  303. End Sub
  304.  
  305. Private Sub SaveFileDialog1_FileOk(sender As Object, e As CancelEventArgs) Handles SaveFileDialog1.FileOk
  306. Write_ButtonsFile()
  307.  
  308. End Sub
  309.  
  310. Private Sub Write_ButtonsFile()
  311. Using fileOut As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(SaveFileDialog1.FileName, False)
  312. Dim counter As Integer = 0
  313. For x = 0 To 19
  314. counter = x + 1
  315. fileOut.WriteLine("button|" & counter.ToString() & "|" & UserConfig_ButtonText_List(x).Text & "|" & UserConfig_ButtonCommand_List(x).Text)
  316. Next
  317. fileOut.WriteLine("EOF")
  318. currentButtonFilePath = SaveFileDialog1.FileName
  319. UBFpath_TextBox.Text = currentButtonFilePath
  320. End Using
  321. End Sub
  322.  
  323. Private Sub UPButtons_UseEdits_Button_Click(sender As Object, e As EventArgs) Handles UPButtons_UseEdits_Button.Click
  324. RefreshUserButtonText()
  325. End Sub
  326.  
  327. Private Sub UPButton1_Click(sender As Object, e As EventArgs) Handles UPButton1.Click
  328. UserConfigButton_EventSub(0)
  329. End Sub
  330.  
  331. Private Sub UPButton2_Click(sender As Object, e As EventArgs) Handles UPButton2.Click
  332. UserConfigButton_EventSub(1)
  333. End Sub
  334.  
  335. Private Sub UPButton3_Click(sender As Object, e As EventArgs) Handles UPButton3.Click
  336. UserConfigButton_EventSub(2)
  337. End Sub
  338.  
  339. Private Sub UPButton4_Click(sender As Object, e As EventArgs) Handles UPButton4.Click
  340. UserConfigButton_EventSub(3)
  341. End Sub
  342.  
  343. Private Sub UPButton5_Click(sender As Object, e As EventArgs) Handles UPButton5.Click
  344. UserConfigButton_EventSub(4)
  345. End Sub
  346.  
  347. Private Sub UPButton6_Click(sender As Object, e As EventArgs) Handles UPButton6.Click
  348. UserConfigButton_EventSub(5)
  349. End Sub
  350.  
  351. Private Sub UPButton7_Click(sender As Object, e As EventArgs) Handles UPButton7.Click
  352. UserConfigButton_EventSub(6)
  353. End Sub
  354.  
  355. Private Sub UPButton8_Click(sender As Object, e As EventArgs) Handles UPButton8.Click
  356. UserConfigButton_EventSub(7)
  357. End Sub
  358.  
  359. Private Sub UPButton9_Click(sender As Object, e As EventArgs) Handles UPButton9.Click
  360. UserConfigButton_EventSub(8)
  361. End Sub
  362.  
  363. Private Sub UPButton10_Click(sender As Object, e As EventArgs) Handles UPButton10.Click
  364. UserConfigButton_EventSub(9)
  365. End Sub
  366.  
  367. Private Sub UPButton11_Click(sender As Object, e As EventArgs) Handles UPButton11.Click
  368. UserConfigButton_EventSub(10)
  369. End Sub
  370.  
  371. Private Sub UPButton12_Click(sender As Object, e As EventArgs) Handles UPButton12.Click
  372. UserConfigButton_EventSub(11)
  373. End Sub
  374.  
  375. Private Sub UPButton13_Click(sender As Object, e As EventArgs) Handles UPButton13.Click
  376. UserConfigButton_EventSub(12)
  377. End Sub
  378.  
  379. Private Sub UPButton14_Click(sender As Object, e As EventArgs) Handles UPButton14.Click
  380. UserConfigButton_EventSub(13)
  381. End Sub
  382.  
  383. Private Sub UPButton15_Click(sender As Object, e As EventArgs) Handles UPButton15.Click
  384. UserConfigButton_EventSub(14)
  385. End Sub
  386.  
  387. Private Sub UPButton16_Click(sender As Object, e As EventArgs) Handles UPButton16.Click
  388. UserConfigButton_EventSub(15)
  389. End Sub
  390.  
  391. Private Sub UPButton17_Click(sender As Object, e As EventArgs) Handles UPButton17.Click
  392. UserConfigButton_EventSub(16)
  393. End Sub
  394.  
  395. Private Sub UPButton18_Click(sender As Object, e As EventArgs) Handles UPButton18.Click
  396. UserConfigButton_EventSub(17)
  397. End Sub
  398.  
  399. Private Sub UPButton19_Click(sender As Object, e As EventArgs) Handles UPButton19.Click
  400. UserConfigButton_EventSub(18)
  401. End Sub
  402.  
  403. Private Sub UPButton20_Click(sender As Object, e As EventArgs) Handles UPButton20.Click
  404. UserConfigButton_EventSub(19)
  405. End Sub
  406.  
  407. End Class
RAW Paste Data