ARDUINO "VB" CODE |
'Add reference - System.Management
Imports System.Management ' Required for comm port identification
Imports System ' Required for comm port and Volume Control
Imports System.IO.Ports ' Required for comm port
Public Class frmEmmaJaneV7
#Region "ArduinoNotes"
' ******************************************************************************************************************
' This program supports the Arduino UNO, NANO and EVERY ONLY
' ******************************************************************************************************************
'
' **** CONFIGURATION STRING ****
' This String contains a description of the functionality of the Arduino Inputs and Outputs
' It is sent to the Arduino via the Serial Port when the Arduino sends a ">Q0<" - where the "0" is the address of the Ardunio
' Inputs can be Normally Low or Normally High
' Outputs are normally Low and become High (5V) when Active.
'[>] Opens String
'[0] = Station Address Number 0 - 9
'[1] = Din02 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output >022222222222<
'[2] = Din03 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output >300002000000<
'[3] = Din04 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output >311112111111<
'[4] = Din05 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[5] = Din06 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[6] = Din07 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[7] = Din08 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[8] = Din09 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[9] = Din10 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[10] = Din11 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[11] = Din12 State - IO - 0,1,2 - 0=InputNL - 1=InputNH - 2=Output
'[<] Closes String
' Din13 State - IO - Din13 is always an Output
'
' **** INPUT STRING ****
' This String is received from the Arduino - It contains the information relating to the state of the Input Pins of the Arduino.
' Each Digital IO pin has 1 Digit Slot to contain its value of either 0 or 1
'[<] Opens String
'[0] = Station Address (0-9) <0000010000000>
'[1] = Din02 - Digit 1 (IO) <0111111111111>
'[2] = Din03 - Digit 1 (IO)
'[3] = Din04 - Digit 1 (IO)
'[4] = Din05 - Digit 1 (IO)
'[5] = Din06 - Digit 1 (IO)
'[6] = Din07 - Digit 1 (IO)
'[7] = Din08 - Digit 1 (IO)
'[8] = Din09 - Digit 1 (IO)
'[9] = Din10 - Digit 1 (IO)
'[10] = Din11 - Digit 1 (IO)
'[11] = Din12 - Digit 1 (IO)
'[12] = Din13 - Digit 1 (IO)
'[>] Closes String
'
' **** RESET STRING ****
' This String is sent to the Arduino to reset its configuration - used in the case of a comms fail
'[>] Opens String >R<
'[R] = Reset instruction >R<
'[<] Closes String
'
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#End Region
#Region "MouseEventsVar and MP3 section and Monitor Off"
'Sounds
Private Const VolUp As Integer = &HA0000 ' Used For Sounds
Private Const VolDn As Integer = &H90000
Private Const MsgNo As Integer = &H319
Declare Function SendMessageW Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As IntPtr) As Int32 ' SCROLLING
Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As IntPtr, ByVal wBar As Int32, ByVal bShow As Int32) As Int32
Private Const SB_VERT = 1
' Monitor Off
Public Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Public Const WM_SYSCOMMAND = &H112
Public Const SC_MONITORPOWER = &HF170& ' (-1)=On; 1=stdby; 2=Off
Public Const MONITOR_ON = -1&
Public Const MONITOR_OFF = 2&
Dim NoOfMonitors As Int16 ' Number of Monitors found
#End Region
#Region "AutomationVar"
' ComPort Initiation
Dim ComPortID As String ' Serial Port Text extracted from Windows
Dim ComPortCnt As Int16 ' Loop Counter for detecting Arduinos attached to com ports - 0 to 9
Dim CommName As String ' Name assigned to CommPort by Windows
Dim SrPtDataIn0 As String : Dim SrPtDataIn1 As String ' Holder for Data String read from the ComPort
Dim SrPtDataIn2 As String : Dim SrPtDataIn3 As String
Dim SrPtDataIn4 As String : Dim SrPtDataIn5 As String
Dim SrPtDataIn6 As String : Dim SrPtDataIn7 As String
Dim SrPtDataIn8 As String : Dim SrPtDataIn9 As String
Dim StartupCnt As Int16 ' Counter used in startup Timer to trigger Reads
Dim StartupTryCnt As Int16 = 0 ' Number of times to try address the Arduinos
Dim StartupFailMarker As Int16 = 0 ' Marker used in the Comms Fail Section
Dim SrPt0Connected As Boolean = False : Dim SrPt1Connected As Boolean = False ' Varible marking each serial port as active - Arduino is Connected to USB Port
Dim SrPt2Connected As Boolean = False : Dim SrPt3Connected As Boolean = False
Dim SrPt4Connected As Boolean = False : Dim SrPt5Connected As Boolean = False
Dim SrPt6Connected As Boolean = False : Dim SrPt7Connected As Boolean = False
Dim SrPt8Connected As Boolean = False : Dim SrPt9Connected As Boolean = False
Dim SrPt0Add As Integer = 99 : Dim SrPt1Add As Integer = 99 ' Holds the Address of the Arduino attached to that Serial Port
Dim SrPt2Add As Integer = 99 : Dim SrPt3Add As Integer = 99
Dim SrPt4Add As Integer = 99 : Dim SrPt5Add As Integer = 99
Dim SrPt6Add As Integer = 99 : Dim SrPt7Add As Integer = 99
Dim SrPt8Add As Integer = 99 : Dim SrPt9Add As Integer = 99
Dim ConfigFileIn As System.IO.StreamReader ' Varible for Stream Reading
Dim ConfigFileOut As System.IO.StreamWriter ' Varible for Stream Writing
Dim ConfigArduinoAddress As Int16 = 99 ' Holds the Arduino Address of the Arduino about to be configured
Dim ConfigString0 As String : Dim ConfigString1 As String : Dim ConfigString2 As String : Dim ConfigString3 As String : Dim ConfigString4 As String ' Configuration String for Arduino X
Dim ConfigString5 As String : Dim ConfigString6 As String : Dim ConfigString7 As String : Dim ConfigString8 As String : Dim ConfigString9 As String
Dim AddressTemp As Integer
Dim ConfigDin02State As String : Dim ConfigDin03State As String : Dim ConfigDin04State As String : Dim ConfigDin05State As String ' Configured State for each of the Digits in the ConfigString - Supports 0, 1, 2
Dim ConfigDin06State As String : Dim ConfigDin07State As String : Dim ConfigDin08State As String : Dim ConfigDin09State As String ' 0 = Input Normally Low - 1 = Input Normally High - 2 = Output
Dim ConfigDin10State As String : Dim ConfigDin11State As String : Dim ConfigDin12State As String
Dim AddIsActive0 As Int16 : Dim AddIsActive1 As Int16 : Dim AddIsActive2 As Int16 : Dim AddIsActive3 As Int16 : Dim AddIsActive4 As Int16 ' Indicator showing if an Arduino Station Address is currently Active - 1 = Active
Dim AddIsActive5 As Int16 : Dim AddIsActive6 As Int16 : Dim AddIsActive7 As Int16 : Dim AddIsActive8 As Int16 : Dim AddIsActive9 As Int16
' DataHandler
Dim DataString0 As String = "<0000000000000>" : Dim DataString1 As String = "<1000000000000>" : Dim DataString2 As String = "<2000000000000>" : Dim DataString3 As String = "<3000000000000>" : Dim DataString4 As String = "<4000000000000>" ' DataString carrying Input and Output Conctions to and from Arduino
Dim DataString5 As String = "<5000000000000>" : Dim DataString6 As String = "<6000000000000>" : Dim DataString7 As String = "<7000000000000>" : Dim DataString8 As String = "<8000000000000>" : Dim DataString9 As String = "<9000000000000>"
Dim DataStringPrev0 As String = "<0000000000000>" : Dim DataStringPrev1 As String = "<1000000000000>" : Dim DataStringPrev2 As String = "<2000000000000>" : Dim DataStringPrev3 As String = "<3000000000000>" : Dim DataStringPrev4 As String = "<4000000000000>" ' Previous cycle Data String
Dim DataStringPrev5 As String = "<5000000000000>" : Dim DataStringPrev6 As String = "<6000000000000>" : Dim DataStringPrev7 As String = "<7000000000000>" : Dim DataStringPrev8 As String = "<8000000000000>" : Dim DataStringPrev9 As String = "<9000000000000>"
Dim DataStringIn0 As String = "<0000000000000>" : Dim DataStringIn1 As String = "<1000000000000>" : Dim DataStringIn2 As String = "<2000000000000>" : Dim DataStringIn3 As String = "<3000000000000>" : Dim DataStringIn4 As String = "<4000000000000>" ' Incoming Data From Serial Port
Dim DataStringIn5 As String = "<5000000000000>" : Dim DataStringIn6 As String = "<6000000000000>" : Dim DataStringIn7 As String = "<7000000000000>" : Dim DataStringIn8 As String = "<8000000000000>" : Dim DataStringIn9 As String = "<9000000000000>"
Dim ConfigStringTemp As String ' Temp ConfigString used in ReadFromArduinAssist() to ensure that Arduino can only adjust Input Digits of DataString
Dim DataStringTemp As String ' Temp Incoming Data received from Serial Port
Dim DataStringInTemp As String ' Temp Incoming Data Address received from Serial Port
Dim DataStringAddTemp As Int16 ' Temp Station Address extracted from Incoming Data from Serial Port
Dim CommsFailCnt0 As Int16 : Dim CommsFailCnt1 As Int16 : Dim CommsFailCnt2 As Int16 : Dim CommsFailCnt3 As Int16 : Dim CommsFailCnt4 As Int16 ' Counter counting number of consecutive tansmission failures to Arduino to initiate a Comms Fail Condition
Dim CommsFailCnt5 As Int16 : Dim CommsFailCnt6 As Int16 : Dim CommsFailCnt7 As Int16 : Dim CommsFailCnt8 As Int16 : Dim CommsFailCnt9 As Int16
Dim CommsFailTemp As Int16 ' Temp Maker for Reseting Arduinos under Comms Fail Conditions in CommsFailAssist()
Dim ForceArduinoWrite0 As Int16 : Dim ForceArduinoWrite1 As Int16 : Dim ForceArduinoWrite2 As Int16 : Dim ForceArduinoWrite3 As Int16 : Dim ForceArduinoWrite4 As Int16 ' Marker which causes a forced tansmission of Data to Arduino even if DataString has not changed
Dim ForceArduinoWrite5 As Int16 : Dim ForceArduinoWrite6 As Int16 : Dim ForceArduinoWrite7 As Int16 : Dim ForceArduinoWrite8 As Int16 : Dim ForceArduinoWrite9 As Int16
' TestMode
Dim TestModeActive As Int16 ' Value set to 1 = Program is in Manual Test Mode
Dim TestArduinoAddress As Int16 ' Station Address selected in Test Mode
Dim TestConfigString As String ' Configuration String of Selected Station in Test Mode
Dim TestDataString As String ' Data String of Selected Station in Test Mode
#End Region
Private Sub frmEmmaJaneV7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AutomationInitiate() ' Initiate the Automation Process by reading the Configfile and Starting the InitiateArduinos Timer to search for Com Ports
End Sub
#Region "Automation"
' Comport Initiation
' Comport Initiation
Private Sub AutomationInitiate() ' Initiate the Automation Process by reading the Configfile and Starting the InitiateArduinos Timer to search for Com Ports
ConfigFileRead() ' Reads the Configuration Text File in order to load the Arduino Configuration details from the hard drive
timInitiateArduinos.Enabled = True ' Begin timed sequence for initiating Comm Ports and identifying Arduino addresses
End Sub
Private Sub ConfigFileRead() ' Reads the Configuration Text File in order to load the Arduino Configuration details from the hard drive
If Not System.IO.Directory.Exists("C:\EmmaJane") Then
System.IO.Directory.CreateDirectory("C:\EmmaJane")
End If
If System.IO.File.Exists("C:\EmmaJane\ConfigFile.txt") Then ' If no file exists...
Else
ConfigFileOut = My.Computer.FileSystem.OpenTextFileWriter("C:\EmmaJane\ConfigFile.txt", True) ' Write this file in - all zero state with only the address number - initial state
ConfigFileOut.WriteLine(">0000000000002<") : ConfigFileOut.WriteLine(">1000000000002<") : ConfigFileOut.WriteLine(">2000000000002<")
ConfigFileOut.WriteLine(">3000000000002<") : ConfigFileOut.WriteLine(">4000000000002<") : ConfigFileOut.WriteLine(">5000000000002<")
ConfigFileOut.WriteLine(">6000000000002<") : ConfigFileOut.WriteLine(">7000000000002<") : ConfigFileOut.WriteLine(">8000000000002<")
ConfigFileOut.WriteLine(">9000000000002<") : ConfigFileOut.Close()
End If
ConfigFileIn = My.Computer.FileSystem.OpenTextFileReader("C:\EmmaJane\ConfigFile.txt") ' Read the File and contain the information in the relevant ConfigString String
ConfigString0 = ConfigFileIn.ReadLine() : ConfigString1 = ConfigFileIn.ReadLine() : ConfigString2 = ConfigFileIn.ReadLine()
ConfigString3 = ConfigFileIn.ReadLine() : ConfigString4 = ConfigFileIn.ReadLine() : ConfigString5 = ConfigFileIn.ReadLine()
ConfigString6 = ConfigFileIn.ReadLine() : ConfigString7 = ConfigFileIn.ReadLine() : ConfigString8 = ConfigFileIn.ReadLine()
ConfigString9 = ConfigFileIn.ReadLine() : ConfigFileIn.Close()
End Sub
Private Sub cmdCommSearch_Click(sender As Object, e As EventArgs) Handles cmdCommSearch.Click ' Click to start the Initiation Process - Only affects undetected Arduinos
If timInitiateArduinos.Enabled = False Then
timInitiateArduinos.Enabled = True ' Begin timed sequence for initiating Comm Ports and identifying Arduino addresses
End If
End Sub
Private Sub cmdResetAll_Click(sender As Object, e As EventArgs) Handles cmdResetAll.Click ' Click to start the Initiation Process - Resets all Arduinos and re-detects all
If timInitiateArduinos.Enabled = False Then
SrPt0Add = 99 : SrPt1Add = 99 : SrPt2Add = 99 : SrPt3Add = 99 : SrPt4Add = 99 ' Sets All Address back to Default - 99 - To Reset All Arduinos
SrPt5Add = 99 : SrPt6Add = 99 : SrPt7Add = 99 : SrPt8Add = 99 : SrPt9Add = 99
timInitiateArduinos.Enabled = True ' Begin timed sequence for initiating Comm Ports and identifying Arduino addresses
End If
End Sub
Private Sub timInitiateArduinos_Tick(sender As Object, e As EventArgs) Handles timInitiateArduinos.Tick ' Timed Sequence to Initiate and Configure all detected Arduinos.
StartupCnt = StartupCnt + 1 ' Increment controlling counter by 1 every cycle of the timer @ 100ms
If StartupCnt >= 1 And StartupCnt <= 26 Then ' If the startup count is between 1 and 26...
If (StartupCnt / 2) = Int(StartupCnt / 2) Then ' Flash the Address label while finding the Arduinos
lblAddres.ForeColor = Color.Lime : Else : lblAddres.ForeColor = Color.White ' Freen and White
End If
End If
If StartupCnt = 1 Then ' @ 0.1 Seconds
timDatahandler.Enabled = False ' Disable DataString transmissions
lblDataString.ForeColor = Color.White ' Colour DataString Label White
StartupTryCnt = 0 ' Reset the number of tries that the StartUpFail will attempt to Initiate all Connected Arduinos
cmbArduinoAddressSelect.Enabled = False ' Disable Config Section
cmbTestAddressSelect.Enabled = False ' Disable Test Section
End If
If StartupCnt = 2 Then ' @ 0.2 Seconds
cmbCommSearch.Items.Clear() ' Clears the Combo box containing the found USB devices
ComPortCnt = 0 ' Resets the value indicating the cycle number of the Com Search Loop
If SrPt0Add = 99 Then : cmdSrPtAdd0.BackColor = Color.Black : cmdSrPtAdd0.Text = "" : SrPt0Connected = False : cmdSrPtConfig0.BackColor = Color.Black : cmdSrPtConfig0.Text = "" : End If ' Resets Colour, Text and Status of Arduinos with Address 99 - Those that have not been Assigned an Address
If SrPt1Add = 99 Then : cmdSrPtAdd1.BackColor = Color.Black : cmdSrPtAdd1.Text = "" : SrPt1Connected = False : cmdSrPtConfig1.BackColor = Color.Black : cmdSrPtConfig1.Text = "" : End If
If SrPt2Add = 99 Then : cmdSrPtAdd2.BackColor = Color.Black : cmdSrPtAdd2.Text = "" : SrPt2Connected = False : cmdSrPtConfig2.BackColor = Color.Black : cmdSrPtConfig2.Text = "" : End If
If SrPt3Add = 99 Then : cmdSrPtAdd3.BackColor = Color.Black : cmdSrPtAdd3.Text = "" : SrPt3Connected = False : cmdSrPtConfig3.BackColor = Color.Black : cmdSrPtConfig3.Text = "" : End If
If SrPt4Add = 99 Then : cmdSrPtAdd4.BackColor = Color.Black : cmdSrPtAdd4.Text = "" : SrPt4Connected = False : cmdSrPtConfig4.BackColor = Color.Black : cmdSrPtConfig4.Text = "" : End If
If SrPt5Add = 99 Then : cmdSrPtAdd5.BackColor = Color.Black : cmdSrPtAdd5.Text = "" : SrPt5Connected = False : cmdSrPtConfig5.BackColor = Color.Black : cmdSrPtConfig5.Text = "" : End If
If SrPt6Add = 99 Then : cmdSrPtAdd6.BackColor = Color.Black : cmdSrPtAdd6.Text = "" : SrPt6Connected = False : cmdSrPtConfig6.BackColor = Color.Black : cmdSrPtConfig6.Text = "" : End If
If SrPt7Add = 99 Then : cmdSrPtAdd7.BackColor = Color.Black : cmdSrPtAdd7.Text = "" : SrPt7Connected = False : cmdSrPtConfig7.BackColor = Color.Black : cmdSrPtConfig7.Text = "" : End If
If SrPt8Add = 99 Then : cmdSrPtAdd8.BackColor = Color.Black : cmdSrPtAdd8.Text = "" : SrPt8Connected = False : cmdSrPtConfig8.BackColor = Color.Black : cmdSrPtConfig8.Text = "" : End If
If SrPt9Add = 99 Then : cmdSrPtAdd9.BackColor = Color.Black : cmdSrPtAdd9.Text = "" : SrPt9Connected = False : cmdSrPtConfig9.BackColor = Color.Black : cmdSrPtConfig9.Text = "" : End If
Try ' Next 2 lines search Windows for any device attached to a USB port - Each found item initiates another cycle of the loop using ComPortID as its identifying text
Dim searcher = New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_PnPEntity WHERE ClassGuid=""{4d36e978-e325-11ce-bfc1-08002be10318}""")
For Each queryObj As ManagementObject In searcher.Get() : ComPortID = (queryObj("Name")) : cmbCommSearch.Items.Add(ComPortID)
If ComPortID.Contains("Arduino") Then ' Searches Windows for all connected USB devices on com ports and initiates all ports that have "Arduino" in their description
CommName = ComPortID.Substring(ComPortID.LastIndexOf("(") + 1, (ComPortID.LastIndexOf(")") - ComPortID.LastIndexOf("(")) - 1) ' CommName is the text found between brackets in the com identiying text - Typically COM1
If ComPortCnt = 0 And SrPt0Add = 99 Then : Try : SrPt0.Close() : SrPt0.PortName = CommName : SrPt0.Open() : SrPt0Connected = True : cmdSrPtAdd0.BackColor = Color.Green : Catch : End Try : End If ' For all 10 slots where Arduinos are found...
If ComPortCnt = 1 And SrPt1Add = 99 Then : Try : SrPt1.Close() : SrPt1.PortName = CommName : SrPt1.Open() : SrPt1Connected = True : cmdSrPtAdd1.BackColor = Color.Green : Catch : End Try : End If ' And where the Address is still 99 (Un-Assigned)...
If ComPortCnt = 2 And SrPt2Add = 99 Then : Try : SrPt2.Close() : SrPt2.PortName = CommName : SrPt2.Open() : SrPt2Connected = True : cmdSrPtAdd2.BackColor = Color.Green : Catch : End Try : End If ' Open the Comm Port...
If ComPortCnt = 3 And SrPt3Add = 99 Then : Try : SrPt3.Close() : SrPt3.PortName = CommName : SrPt3.Open() : SrPt3Connected = True : cmdSrPtAdd3.BackColor = Color.Green : Catch : End Try : End If ' Find the Port Name...
If ComPortCnt = 4 And SrPt4Add = 99 Then : Try : SrPt4.Close() : SrPt4.PortName = CommName : SrPt4.Open() : SrPt4Connected = True : cmdSrPtAdd4.BackColor = Color.Green : Catch : End Try : End If ' Close the Comm Port...
If ComPortCnt = 5 And SrPt5Add = 99 Then : Try : SrPt5.Close() : SrPt5.PortName = CommName : SrPt5.Open() : SrPt5Connected = True : cmdSrPtAdd5.BackColor = Color.Green : Catch : End Try : End If ' Set Connected Status to true...
If ComPortCnt = 6 And SrPt6Add = 99 Then : Try : SrPt6.Close() : SrPt6.PortName = CommName : SrPt6.Open() : SrPt6Connected = True : cmdSrPtAdd6.BackColor = Color.Green : Catch : End Try : End If ' Set Colour to Green.
If ComPortCnt = 7 And SrPt7Add = 99 Then : Try : SrPt7.Close() : SrPt7.PortName = CommName : SrPt7.Open() : SrPt7Connected = True : cmdSrPtAdd7.BackColor = Color.Green : Catch : End Try : End If
If ComPortCnt = 8 And SrPt8Add = 99 Then : Try : SrPt8.Close() : SrPt8.PortName = CommName : SrPt8.Open() : SrPt8Connected = True : cmdSrPtAdd8.BackColor = Color.Green : Catch : End Try : End If
If ComPortCnt = 9 And SrPt9Add = 99 Then : Try : SrPt9.Close() : SrPt9.PortName = CommName : SrPt9.Open() : SrPt9Connected = True : cmdSrPtAdd9.BackColor = Color.Green : Catch : End Try : End If
ComPortCnt = ComPortCnt + 1 ' Increments the com search loop counter
End If
Next
Catch err As ManagementException : End Try
If SrPt0Connected = True And SrPt0Add = 99 Then : Try : SrPt0.WriteLine(">R<") : Catch : End Try : End If ' * Reset Any Attached Arduino with an Un-Assigned Address.
If SrPt1Connected = True And SrPt1Add = 99 Then : Try : SrPt1.WriteLine(">R<") : Catch : End Try : End If ' If Serial Port is Connected...
If SrPt2Connected = True And SrPt2Add = 99 Then : Try : SrPt2.WriteLine(">R<") : Catch : End Try : End If ' And Arduino has an Un-Assigned Address...
If SrPt3Connected = True And SrPt3Add = 99 Then : Try : SrPt3.WriteLine(">R<") : Catch : End Try : End If ' Write ">R<" to Arduino to force a Software Reset.
If SrPt4Connected = True And SrPt4Add = 99 Then : Try : SrPt4.WriteLine(">R<") : Catch : End Try : End If
If SrPt5Connected = True And SrPt5Add = 99 Then : Try : SrPt5.WriteLine(">R<") : Catch : End Try : End If
If SrPt6Connected = True And SrPt6Add = 99 Then : Try : SrPt6.WriteLine(">R<") : Catch : End Try : End If
If SrPt7Connected = True And SrPt7Add = 99 Then : Try : SrPt7.WriteLine(">R<") : Catch : End Try : End If
If SrPt8Connected = True And SrPt8Add = 99 Then : Try : SrPt8.WriteLine(">R<") : Catch : End Try : End If
If SrPt9Connected = True And SrPt9Add = 99 Then : Try : SrPt9.WriteLine(">R<") : Catch : End Try : End If
End If
If StartupCnt = 20 Then ' @ 2.0 Seconds * Identify Arduino Addresses
If SrPt0Connected = True And SrPt0Add = 99 Then : SrPtDataIn0 = "" ' If an Arduino is connected to the Serial Port but still has an Un-Assigned Address - Then Clear Incoming Data Varible...
Try : SrPtDataIn0 = SrPt0.ReadExisting() : Catch : End Try ' Read Data String from Arduino - Into Incoming Data Varible...
If SrPtDataIn0.Length >= 6 Then ' If the SrPtData String is >= 6 characters...
If SrPtDataIn0.Substring(0, 1) = ">" And SrPtDataIn0.Substring(1, 1) = "Q" And SrPtDataIn0.Substring(3, 1) = "<" Then ' If read String = "<Q*>"....
SrPt0Add = Val(SrPtDataIn0.Substring(2, 1)) : cmdSrPtAdd0.Text = SrPt0Add ' Identify the Connected Arduino Address and assign to SrPt0Add.
End If : End If : End If
If SrPt1Connected = True And SrPt1Add = 99 Then : SrPtDataIn1 = ""
Try : SrPtDataIn1 = SrPt1.ReadExisting() : Catch : End Try
If SrPtDataIn1.Length >= 6 Then
If SrPtDataIn1.Substring(0, 1) = ">" And SrPtDataIn1.Substring(1, 1) = "Q" And SrPtDataIn1.Substring(3, 1) = "<" Then
SrPt1Add = Val(SrPtDataIn1.Substring(2, 1)) : cmdSrPtAdd1.Text = SrPt1Add
End If : End If : End If
If SrPt2Connected = True And SrPt2Add = 99 Then : SrPtDataIn2 = ""
Try : SrPtDataIn2 = SrPt2.ReadExisting() : Catch : End Try
If SrPtDataIn2.Length >= 6 Then
If SrPtDataIn2.Substring(0, 1) = ">" And SrPtDataIn2.Substring(1, 1) = "Q" And SrPtDataIn2.Substring(3, 1) = "<" Then
SrPt2Add = Val(SrPtDataIn2.Substring(2, 1)) : cmdSrPtAdd2.Text = SrPt2Add
End If : End If : End If
If SrPt3Connected = True And SrPt3Add = 99 Then : SrPtDataIn3 = ""
Try : SrPtDataIn3 = SrPt3.ReadExisting() : Catch : End Try
If SrPtDataIn3.Length >= 6 Then
If SrPtDataIn3.Substring(0, 1) = ">" And SrPtDataIn3.Substring(1, 1) = "Q" And SrPtDataIn3.Substring(3, 1) = "<" Then
SrPt3Add = Val(SrPtDataIn3.Substring(2, 1)) : cmdSrPtAdd3.Text = SrPt3Add
End If : End If : End If
If SrPt4Connected = True And SrPt4Add = 99 Then : SrPtDataIn4 = ""
Try : SrPtDataIn4 = SrPt4.ReadExisting() : Catch : End Try
If SrPtDataIn4.Length >= 6 Then
If SrPtDataIn4.Substring(0, 1) = ">" And SrPtDataIn4.Substring(1, 1) = "Q" And SrPtDataIn4.Substring(3, 1) = "<" Then
SrPt4Add = Val(SrPtDataIn4.Substring(2, 1)) : cmdSrPtAdd4.Text = SrPt4Add
End If : End If : End If
If SrPt5Connected = True And SrPt5Add = 99 Then : SrPtDataIn5 = ""
Try : SrPtDataIn5 = SrPt5.ReadExisting() : Catch : End Try
If SrPtDataIn5.Length >= 6 Then
If SrPtDataIn5.Substring(0, 1) = ">" And SrPtDataIn5.Substring(1, 1) = "Q" And SrPtDataIn5.Substring(3, 1) = "<" Then
SrPt5Add = Val(SrPtDataIn5.Substring(2, 1)) : cmdSrPtAdd5.Text = SrPt5Add
End If : End If : End If
If SrPt6Connected = True And SrPt6Add = 99 Then : SrPtDataIn6 = ""
Try : SrPtDataIn6 = SrPt6.ReadExisting() : Catch : End Try
If SrPtDataIn6.Length >= 6 Then
If SrPtDataIn6.Substring(0, 1) = ">" And SrPtDataIn6.Substring(1, 1) = "Q" And SrPtDataIn6.Substring(3, 1) = "<" Then
SrPt6Add = Val(SrPtDataIn6.Substring(2, 1)) : cmdSrPtAdd6.Text = SrPt6Add
End If : End If : End If
If SrPt7Connected = True And SrPt7Add = 99 Then : SrPtDataIn7 = ""
Try : SrPtDataIn7 = SrPt7.ReadExisting() : Catch : End Try
If SrPtDataIn7.Length >= 6 Then
If SrPtDataIn7.Substring(0, 1) = ">" And SrPtDataIn7.Substring(1, 1) = "Q" And SrPtDataIn7.Substring(3, 1) = "<" Then
SrPt7Add = Val(SrPtDataIn7.Substring(2, 1)) : cmdSrPtAdd7.Text = SrPt7Add
End If : End If : End If
If SrPt8Connected = True And SrPt8Add = 99 Then : SrPtDataIn8 = ""
Try : SrPtDataIn8 = SrPt8.ReadExisting() : Catch : End Try
If SrPtDataIn8.Length >= 6 Then
If SrPtDataIn8.Substring(0, 1) = ">" And SrPtDataIn8.Substring(1, 1) = "Q" And SrPtDataIn8.Substring(3, 1) = "<" Then
SrPt8Add = Val(SrPtDataIn8.Substring(2, 1)) : cmdSrPtAdd8.Text = SrPt8Add
End If : End If : End If
If SrPt9Connected = True And SrPt9Add = 99 Then : SrPtDataIn9 = ""
Try : SrPtDataIn9 = SrPt9.ReadExisting() : Catch : End Try
If SrPtDataIn9.Length >= 6 Then
If SrPtDataIn9.Substring(0, 1) = ">" And SrPtDataIn9.Substring(1, 1) = "Q" And SrPtDataIn9.Substring(3, 1) = "<" Then
SrPt9Add = Val(SrPtDataIn9.Substring(2, 1)) : cmdSrPtAdd9.Text = SrPt9Add
End If : End If : End If
End If
If StartupCnt = 25 Then : StartupFailMarker = 0 ' @ 2.5 Seconds...Set the StartupFailMarker to 0 '
If SrPt0Connected = True And SrPt0Add = 99 Then : StartupFailMarker = 1 : End If ' * Identify whether any Connedted Arduinos have failed to receive an Address
If SrPt1Connected = True And SrPt1Add = 99 Then : StartupFailMarker = 1 : End If ' If any Arduino is connected...
If SrPt2Connected = True And SrPt2Add = 99 Then : StartupFailMarker = 1 : End If ' But still has an Un-Assigned Address...
If SrPt3Connected = True And SrPt3Add = 99 Then : StartupFailMarker = 1 : End If ' Set the StartupFailMarker to 1
If SrPt4Connected = True And SrPt4Add = 99 Then : StartupFailMarker = 1 : End If
If SrPt5Connected = True And SrPt5Add = 99 Then : StartupFailMarker = 1 : End If
If SrPt6Connected = True And SrPt6Add = 99 Then : StartupFailMarker = 1 : End If
If SrPt7Connected = True And SrPt7Add = 99 Then : StartupFailMarker = 1 : End If
If SrPt8Connected = True And SrPt8Add = 99 Then : StartupFailMarker = 1 : End If
If SrPt9Connected = True And SrPt9Add = 99 Then : StartupFailMarker = 1 : End If
If StartupFailMarker = 1 And StartupTryCnt <= 5 Then ' If any Arduino has failed to receive an Address and the number of attempts is 5 or less...
StartupTryCnt = StartupTryCnt + 1 : StartupCnt = 19 ' Increment the StartupTryCnt and return to the 0.2 Second timer value - Just after the location of attached devices
End If
End If
If StartupCnt = 26 Then ' @ 2.6 Seconds... - Only after there are no StartUpFails or after 5 Failed StartUp Attempts...
If SrPt0Connected = True And SrPt0Add = 99 Then : cmdSrPtAdd0.BackColor = Color.Red : cmdSrPtAdd0.Text = "X" : SrPt0Connected = False : SrPt0.Close() : End If ' * Any Connected Arduino which has failed to receive an address after 5 attempts is marked as Comms Failed
If SrPt1Connected = True And SrPt1Add = 99 Then : cmdSrPtAdd1.BackColor = Color.Red : cmdSrPtAdd1.Text = "X" : SrPt1Connected = False : SrPt1.Close() : End If ' If an Arduino is Connected...
If SrPt2Connected = True And SrPt2Add = 99 Then : cmdSrPtAdd2.BackColor = Color.Red : cmdSrPtAdd2.Text = "X" : SrPt2Connected = False : SrPt2.Close() : End If ' And the address is still Un-Assigned...
If SrPt3Connected = True And SrPt3Add = 99 Then : cmdSrPtAdd3.BackColor = Color.Red : cmdSrPtAdd3.Text = "X" : SrPt3Connected = False : SrPt3.Close() : End If ' Colour the Address Text Box RED and Set the Text to "X" to mark it as Comms Failed...
If SrPt4Connected = True And SrPt4Add = 99 Then : cmdSrPtAdd4.BackColor = Color.Red : cmdSrPtAdd4.Text = "X" : SrPt4Connected = False : SrPt4.Close() : End If ' Mark SrPt*Connected as FALSE...
If SrPt5Connected = True And SrPt5Add = 99 Then : cmdSrPtAdd5.BackColor = Color.Red : cmdSrPtAdd5.Text = "X" : SrPt5Connected = False : SrPt5.Close() : End If ' Close the Port.
If SrPt6Connected = True And SrPt6Add = 99 Then : cmdSrPtAdd6.BackColor = Color.Red : cmdSrPtAdd6.Text = "X" : SrPt6Connected = False : SrPt6.Close() : End If
If SrPt7Connected = True And SrPt7Add = 99 Then : cmdSrPtAdd7.BackColor = Color.Red : cmdSrPtAdd7.Text = "X" : SrPt7Connected = False : SrPt7.Close() : End If
If SrPt8Connected = True And SrPt8Add = 99 Then : cmdSrPtAdd8.BackColor = Color.Red : cmdSrPtAdd8.Text = "X" : SrPt8Connected = False : SrPt8.Close() : End If
If SrPt9Connected = True And SrPt9Add = 99 Then : cmdSrPtAdd9.BackColor = Color.Red : cmdSrPtAdd9.Text = "X" : SrPt9Connected = False : SrPt9.Close() : End If
lblAddres.ForeColor = Color.White ' Reset the Colour of the Flashing Label
End If
If StartupCnt >= 27 And StartupCnt <= 40 Then ' ! Begin the ConfigString Send Section Here
If (StartupCnt / 2) = Int(StartupCnt / 2) Then : lblConfig.ForeColor = Color.Lime : Else : lblConfig.ForeColor = Color.White : End If ' Flash the Config label while configuring the Arduinos
End If
If StartupCnt = 28 Then : StartupTryCnt = 0 : End If ' @ 2.8 Seconds... Reset the number of tries that the StartUpFail will attempt to Config all Connected Arduinos
If StartupCnt = 30 Then ' @ 3.0 Seconds...
If SrPt0Connected = True Then : AddressTemp = SrPt0Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt0.WriteLine(ConfigStringTemp) : End If ' * Send Config String to Arduinos
If SrPt1Connected = True Then : AddressTemp = SrPt1Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt1.WriteLine(ConfigStringTemp) : End If ' If an Arduino is connected...
If SrPt2Connected = True Then : AddressTemp = SrPt2Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt2.WriteLine(ConfigStringTemp) : End If ' Set a Temp Address and String...
If SrPt3Connected = True Then : AddressTemp = SrPt3Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt3.WriteLine(ConfigStringTemp) : End If ' Send Temp values to InitiateArduinosAssist()...
If SrPt4Connected = True Then : AddressTemp = SrPt4Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt4.WriteLine(ConfigStringTemp) : End If ' Write Selected Config String to Arduino.
If SrPt5Connected = True Then : AddressTemp = SrPt5Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt5.WriteLine(ConfigStringTemp) : End If
If SrPt6Connected = True Then : AddressTemp = SrPt6Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt6.WriteLine(ConfigStringTemp) : End If
If SrPt7Connected = True Then : AddressTemp = SrPt7Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt7.WriteLine(ConfigStringTemp) : End If
If SrPt8Connected = True Then : AddressTemp = SrPt8Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt8.WriteLine(ConfigStringTemp) : End If
If SrPt9Connected = True Then : AddressTemp = SrPt9Add : ConfigStringTemp = "" : InitiateArduinosAssist() : SrPt9.WriteLine(ConfigStringTemp) : End If
End If
If StartupCnt = 35 Then ' @ 3.5 Seconds...
If SrPt0Connected = True Then : Try : SrPtDataIn0 = SrPt0.ReadExisting() : Catch : End Try : If SrPtDataIn0.Length > 15 Then : Do While SrPtDataIn0.Length > 15 : SrPtDataIn0 = SrPtDataIn0.Remove(SrPtDataIn0.Length - 1, 1) : Loop : End If : cmdSrPtConfig0.Text = SrPtDataIn0 : End If ' * Read Config String back from Arduino to Conform Receipt
If SrPt1Connected = True Then : Try : SrPtDataIn1 = SrPt1.ReadExisting() : Catch : End Try : If SrPtDataIn1.Length > 15 Then : Do While SrPtDataIn1.Length > 15 : SrPtDataIn1 = SrPtDataIn1.Remove(SrPtDataIn1.Length - 1, 1) : Loop : End If : cmdSrPtConfig1.Text = SrPtDataIn1 : End If ' If an Arduino is connected...
If SrPt2Connected = True Then : Try : SrPtDataIn2 = SrPt2.ReadExisting() : Catch : End Try : If SrPtDataIn2.Length > 15 Then : Do While SrPtDataIn2.Length > 15 : SrPtDataIn2 = SrPtDataIn2.Remove(SrPtDataIn2.Length - 1, 1) : Loop : End If : cmdSrPtConfig2.Text = SrPtDataIn2 : End If ' Read from the Serial Port...
If SrPt3Connected = True Then : Try : SrPtDataIn3 = SrPt3.ReadExisting() : Catch : End Try : If SrPtDataIn3.Length > 15 Then : Do While SrPtDataIn3.Length > 15 : SrPtDataIn3 = SrPtDataIn3.Remove(SrPtDataIn3.Length - 1, 1) : Loop : End If : cmdSrPtConfig3.Text = SrPtDataIn3 : End If ' Remove any extra digits from the received string
If SrPt4Connected = True Then : Try : SrPtDataIn4 = SrPt4.ReadExisting() : Catch : End Try : If SrPtDataIn4.Length > 15 Then : Do While SrPtDataIn4.Length > 15 : SrPtDataIn4 = SrPtDataIn4.Remove(SrPtDataIn4.Length - 1, 1) : Loop : End If : cmdSrPtConfig4.Text = SrPtDataIn4 : End If ' Write the received text to the cmdSrPtConfig* Text Box...
If SrPt5Connected = True Then : Try : SrPtDataIn5 = SrPt5.ReadExisting() : Catch : End Try : If SrPtDataIn5.Length > 15 Then : Do While SrPtDataIn5.Length > 15 : SrPtDataIn5 = SrPtDataIn5.Remove(SrPtDataIn5.Length - 1, 1) : Loop : End If : cmdSrPtConfig5.Text = SrPtDataIn5 : End If
If SrPt6Connected = True Then : Try : SrPtDataIn6 = SrPt6.ReadExisting() : Catch : End Try : If SrPtDataIn6.Length > 15 Then : Do While SrPtDataIn6.Length > 15 : SrPtDataIn6 = SrPtDataIn6.Remove(SrPtDataIn6.Length - 1, 1) : Loop : End If : cmdSrPtConfig6.Text = SrPtDataIn6 : End If
If SrPt7Connected = True Then : Try : SrPtDataIn7 = SrPt7.ReadExisting() : Catch : End Try : If SrPtDataIn7.Length > 15 Then : Do While SrPtDataIn7.Length > 15 : SrPtDataIn7 = SrPtDataIn7.Remove(SrPtDataIn7.Length - 1, 1) : Loop : End If : cmdSrPtConfig7.Text = SrPtDataIn7 : End If
If SrPt8Connected = True Then : Try : SrPtDataIn8 = SrPt8.ReadExisting() : Catch : End Try : If SrPtDataIn8.Length > 15 Then : Do While SrPtDataIn8.Length > 15 : SrPtDataIn8 = SrPtDataIn8.Remove(SrPtDataIn8.Length - 1, 1) : Loop : End If : cmdSrPtConfig8.Text = SrPtDataIn8 : End If
If SrPt9Connected = True Then : Try : SrPtDataIn9 = SrPt9.ReadExisting() : Catch : End Try : If SrPtDataIn9.Length > 15 Then : Do While SrPtDataIn9.Length > 15 : SrPtDataIn9 = SrPtDataIn9.Remove(SrPtDataIn0.Length - 1, 1) : Loop : End If : cmdSrPtConfig9.Text = SrPtDataIn9 : End If
End If
If StartupCnt = 40 Then : StartupFailMarker = 0 ' @ 4.0 Seconds... Set the StartupFailMarker to 0
If SrPt0Connected = True Then : If SrPtDataIn0.Length <> 15 Then : StartupFailMarker = 1 : End If : End If ' * Check whether the Config String was correctly received by the Arduino
If SrPt1Connected = True Then : If SrPtDataIn1.Length <> 15 Then : StartupFailMarker = 1 : End If : End If ' If an Arduino is connected...
If SrPt2Connected = True Then : If SrPtDataIn2.Length <> 15 Then : StartupFailMarker = 1 : End If : End If ' Check to see that the Returned Config String length is 15 Characters...
If SrPt3Connected = True Then : If SrPtDataIn3.Length <> 15 Then : StartupFailMarker = 1 : End If : End If ' If not, Mark it as a failed attempt.
If SrPt4Connected = True Then : If SrPtDataIn4.Length <> 15 Then : StartupFailMarker = 1 : End If : End If
If SrPt5Connected = True Then : If SrPtDataIn5.Length <> 15 Then : StartupFailMarker = 1 : End If : End If
If SrPt6Connected = True Then : If SrPtDataIn6.Length <> 15 Then : StartupFailMarker = 1 : End If : End If
If SrPt7Connected = True Then : If SrPtDataIn7.Length <> 15 Then : StartupFailMarker = 1 : End If : End If
If SrPt8Connected = True Then : If SrPtDataIn8.Length <> 15 Then : StartupFailMarker = 1 : End If : End If
If SrPt9Connected = True Then : If SrPtDataIn9.Length <> 15 Then : StartupFailMarker = 1 : End If : End If
If SrPt0Connected = True Then : If SrPtDataIn0.Length = 15 Then : If SrPtDataIn0.Substring(0, 1) <> ">" Or SrPtDataIn0.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If ' If any Received ConfigString does not match the required parameters...
If SrPt1Connected = True Then : If SrPtDataIn1.Length = 15 Then : If SrPtDataIn1.Substring(0, 1) <> ">" Or SrPtDataIn1.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If ' Mark it as a failed attempt.
If SrPt2Connected = True Then : If SrPtDataIn2.Length = 15 Then : If SrPtDataIn2.Substring(0, 1) <> ">" Or SrPtDataIn2.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If SrPt3Connected = True Then : If SrPtDataIn3.Length = 15 Then : If SrPtDataIn3.Substring(0, 1) <> ">" Or SrPtDataIn3.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If SrPt4Connected = True Then : If SrPtDataIn4.Length = 15 Then : If SrPtDataIn4.Substring(0, 1) <> ">" Or SrPtDataIn4.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If SrPt5Connected = True Then : If SrPtDataIn5.Length = 15 Then : If SrPtDataIn5.Substring(0, 1) <> ">" Or SrPtDataIn5.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If SrPt6Connected = True Then : If SrPtDataIn6.Length = 15 Then : If SrPtDataIn6.Substring(0, 1) <> ">" Or SrPtDataIn6.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If SrPt7Connected = True Then : If SrPtDataIn7.Length = 15 Then : If SrPtDataIn7.Substring(0, 1) <> ">" Or SrPtDataIn7.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If SrPt8Connected = True Then : If SrPtDataIn8.Length = 15 Then : If SrPtDataIn8.Substring(0, 1) <> ">" Or SrPtDataIn8.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If SrPt9Connected = True Then : If SrPtDataIn9.Length = 15 Then : If SrPtDataIn9.Substring(0, 1) <> ">" Or SrPtDataIn9.Substring(14, 1) <> "<" Then : StartupFailMarker = 1 : End If : End If : End If
If StartupFailMarker = 1 And StartupTryCnt <= 5 Then ' If any Arduino has failed to receive a Correct Config String and the number of attempts is 5 or less...
StartupTryCnt = StartupTryCnt + 1 : StartupCnt = 29 ' Increment the StartupTryCnt and return to the 2.9 Second timer value - Just before the location Config Section
End If
End If
If StartupCnt = 41 Then ' @ 4.1 Seconds... - Only after there are no StartUpFails or after 5 Failed StartUp Attempts...
If SrPt0Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn0.Substring(0, 1) <> ">" Or SrPtDataIn0.Substring(14, 1) <> "<" Then : cmdSrPtConfig0.BackColor = Color.Red : cmdSrPtConfig0.Text = "CONFIG FAIL" : SrPt0Connected = False : SrPt0.Close() : End If : End If ' * Any Connected Arduino which has failed to receive a Config after 5 attempts is marked as CONFIG FAIL
If SrPt1Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn1.Substring(0, 1) <> ">" Or SrPtDataIn1.Substring(14, 1) <> "<" Then : cmdSrPtConfig1.BackColor = Color.Red : cmdSrPtConfig1.Text = "CONFIG FAIL" : SrPt1Connected = False : SrPt1.Close() : End If : End If ' If an Arduino is Connected...
If SrPt2Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn2.Substring(0, 1) <> ">" Or SrPtDataIn2.Substring(14, 1) <> "<" Then : cmdSrPtConfig2.BackColor = Color.Red : cmdSrPtConfig2.Text = "CONFIG FAIL" : SrPt2Connected = False : SrPt2.Close() : End If : End If ' And the Config String Length is not 15 Characters...
If SrPt3Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn3.Substring(0, 1) <> ">" Or SrPtDataIn3.Substring(14, 1) <> "<" Then : cmdSrPtConfig3.BackColor = Color.Red : cmdSrPtConfig3.Text = "CONFIG FAIL" : SrPt3Connected = False : SrPt3.Close() : End If : End If ' Colour the Config text Box RED and Set the Text to CONFIG FAILED...
If SrPt4Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn4.Substring(0, 1) <> ">" Or SrPtDataIn4.Substring(14, 1) <> "<" Then : cmdSrPtConfig4.BackColor = Color.Red : cmdSrPtConfig4.Text = "CONFIG FAIL" : SrPt4Connected = False : SrPt4.Close() : End If : End If ' Mark SrPt*Connected as FALSE...
If SrPt5Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn5.Substring(0, 1) <> ">" Or SrPtDataIn5.Substring(14, 1) <> "<" Then : cmdSrPtConfig5.BackColor = Color.Red : cmdSrPtConfig5.Text = "CONFIG FAIL" : SrPt5Connected = False : SrPt5.Close() : End If : End If ' Close the Port.
If SrPt6Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn6.Substring(0, 1) <> ">" Or SrPtDataIn6.Substring(14, 1) <> "<" Then : cmdSrPtConfig6.BackColor = Color.Red : cmdSrPtConfig6.Text = "CONFIG FAIL" : SrPt6Connected = False : SrPt6.Close() : End If : End If
If SrPt7Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn7.Substring(0, 1) <> ">" Or SrPtDataIn7.Substring(14, 1) <> "<" Then : cmdSrPtConfig7.BackColor = Color.Red : cmdSrPtConfig7.Text = "CONFIG FAIL" : SrPt7Connected = False : SrPt7.Close() : End If : End If
If SrPt8Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn8.Substring(0, 1) <> ">" Or SrPtDataIn8.Substring(14, 1) <> "<" Then : cmdSrPtConfig8.BackColor = Color.Red : cmdSrPtConfig8.Text = "CONFIG FAIL" : SrPt8Connected = False : SrPt8.Close() : End If : End If
If SrPt9Connected = True Then : If SrPtDataIn0.Length <> 15 Or SrPtDataIn9.Substring(0, 1) <> ">" Or SrPtDataIn9.Substring(14, 1) <> "<" Then : cmdSrPtConfig9.BackColor = Color.Red : cmdSrPtConfig9.Text = "CONFIG FAIL" : SrPt9Connected = False : SrPt9.Close() : End If : End If
lblConfig.ForeColor = Color.White ' Reset the Colour of the Flashing Config Label
End If
If StartupCnt = 42 Then ' @ 4.2 Seconds... -
cmdAdd0DataString.Text = DataString0 : cmdAdd1DataString.Text = DataString1 : cmdAdd2DataString.Text = DataString2 : cmdAdd3DataString.Text = DataString3 : cmdAdd4DataString.Text = DataString4 ' Populate the DataString text Boxes
cmdAdd5DataString.Text = DataString5 : cmdAdd6DataString.Text = DataString6 : cmdAdd7DataString.Text = DataString7 : cmdAdd8DataString.Text = DataString8 : cmdAdd9DataString.Text = DataString9
ForceArduinoWrite0 = 1 : ForceArduinoWrite1 = 1 : ForceArduinoWrite2 = 1 : ForceArduinoWrite3 = 1 : ForceArduinoWrite4 = 1 ' Force a Transmit to Arduino when Data Transmit resumes
ForceArduinoWrite5 = 1 : ForceArduinoWrite6 = 1 : ForceArduinoWrite7 = 1 : ForceArduinoWrite8 = 1 : ForceArduinoWrite9 = 1
End If
If StartupCnt = 43 Then ' @ 4.3 Seconds... - * Mark Active Address as Active
If SrPt0Add = 0 Or SrPt1Add = 0 Or SrPt2Add = 0 Or SrPt3Add = 0 Or SrPt4Add = 0 Or SrPt5Add = 0 Or SrPt6Add = 0 Or SrPt7Add = 0 Or SrPt8Add = 0 Or SrPt9Add = 0 Then ' If any Serial Port is Using Address X
AddIsActive0 = 1 : lblAdd0.ForeColor = Color.Lime : Else : AddIsActive0 = 0 : lblAdd0.ForeColor = Color.White : End If ' Mark AddIsActiveX as 1 and Colour Address Label x as Lime
If SrPt0Add = 1 Or SrPt1Add = 1 Or SrPt2Add = 1 Or SrPt3Add = 1 Or SrPt4Add = 1 Or SrPt5Add = 1 Or SrPt6Add = 1 Or SrPt7Add = 1 Or SrPt8Add = 1 Or SrPt9Add = 1 Then ' Else Reverse
AddIsActive1 = 1 : lblAdd1.ForeColor = Color.Lime : Else : AddIsActive1 = 0 : lblAdd1.ForeColor = Color.White : End If
If SrPt0Add = 2 Or SrPt1Add = 2 Or SrPt2Add = 2 Or SrPt3Add = 2 Or SrPt4Add = 2 Or SrPt5Add = 2 Or SrPt6Add = 2 Or SrPt7Add = 2 Or SrPt8Add = 2 Or SrPt9Add = 2 Then
AddIsActive2 = 1 : lblAdd2.ForeColor = Color.Lime : Else : AddIsActive2 = 0 : lblAdd2.ForeColor = Color.White : End If
If SrPt0Add = 3 Or SrPt1Add = 3 Or SrPt2Add = 3 Or SrPt3Add = 3 Or SrPt4Add = 3 Or SrPt5Add = 3 Or SrPt6Add = 3 Or SrPt7Add = 3 Or SrPt8Add = 3 Or SrPt9Add = 3 Then
AddIsActive3 = 1 : lblAdd3.ForeColor = Color.Lime : Else : AddIsActive3 = 0 : lblAdd3.ForeColor = Color.White : End If
If SrPt0Add = 4 Or SrPt1Add = 4 Or SrPt2Add = 4 Or SrPt3Add = 4 Or SrPt4Add = 4 Or SrPt5Add = 4 Or SrPt6Add = 4 Or SrPt7Add = 4 Or SrPt8Add = 4 Or SrPt9Add = 4 Then
AddIsActive4 = 1 : lblAdd4.ForeColor = Color.Lime : Else : AddIsActive4 = 0 : lblAdd4.ForeColor = Color.White : End If
If SrPt0Add = 5 Or SrPt1Add = 5 Or SrPt2Add = 5 Or SrPt3Add = 5 Or SrPt4Add = 5 Or SrPt5Add = 5 Or SrPt6Add = 5 Or SrPt7Add = 5 Or SrPt8Add = 5 Or SrPt9Add = 5 Then
AddIsActive5 = 1 : lblAdd5.ForeColor = Color.Lime : Else : AddIsActive5 = 0 : lblAdd5.ForeColor = Color.White : End If
If SrPt0Add = 6 Or SrPt1Add = 6 Or SrPt2Add = 6 Or SrPt3Add = 6 Or SrPt4Add = 6 Or SrPt5Add = 6 Or SrPt6Add = 6 Or SrPt7Add = 6 Or SrPt8Add = 6 Or SrPt9Add = 6 Then
AddIsActive6 = 1 : lblAdd6.ForeColor = Color.Lime : Else : AddIsActive6 = 0 : lblAdd6.ForeColor = Color.White : End If
If SrPt0Add = 7 Or SrPt1Add = 7 Or SrPt2Add = 7 Or SrPt3Add = 7 Or SrPt4Add = 7 Or SrPt5Add = 7 Or SrPt6Add = 7 Or SrPt7Add = 7 Or SrPt8Add = 7 Or SrPt9Add = 7 Then
AddIsActive7 = 1 : lblAdd7.ForeColor = Color.Lime : Else : AddIsActive7 = 0 : lblAdd7.ForeColor = Color.White : End If
If SrPt0Add = 8 Or SrPt1Add = 8 Or SrPt2Add = 8 Or SrPt3Add = 8 Or SrPt4Add = 8 Or SrPt5Add = 8 Or SrPt6Add = 8 Or SrPt7Add = 8 Or SrPt8Add = 8 Or SrPt9Add = 8 Then
AddIsActive8 = 1 : lblAdd8.ForeColor = Color.Lime : Else : AddIsActive8 = 0 : lblAdd8.ForeColor = Color.White : End If
If SrPt0Add = 9 Or SrPt1Add = 9 Or SrPt2Add = 9 Or SrPt3Add = 9 Or SrPt4Add = 9 Or SrPt5Add = 9 Or SrPt6Add = 9 Or SrPt7Add = 9 Or SrPt8Add = 9 Or SrPt9Add = 9 Then
AddIsActive9 = 1 : lblAdd9.ForeColor = Color.Lime : Else : AddIsActive9 = 0 : lblAdd9.ForeColor = Color.White : End If
End If
If StartupCnt = 44 Then ' @ 4.4 Seconds... - * Close down the Timer
timDatahandler.Enabled = True ' Enable DataString transmissions
StartupCnt = 0 ' Reset the Startup Timer Counter
cmbArduinoAddressSelect.Enabled = True ' Enable Config Section
cmbTestAddressSelect.Enabled = True ' Enable Test Section
timInitiateArduinos.Enabled = False ' Disable the InitiateArduinos timer
End If
End Sub
Private Sub InitiateArduinosAssist() ' Assists with the Selecting of the Correct Config String to send to each Arduino - Used in timInitiateArduinos_Tick
If AddressTemp = 0 Then : ConfigStringTemp = (ConfigString0) : End If ' If the Temp Address is *...
If AddressTemp = 1 Then : ConfigStringTemp = (ConfigString1) : End If ' Set the Temp Config String to the correct ConfigString...
If AddressTemp = 2 Then : ConfigStringTemp = (ConfigString2) : End If
If AddressTemp = 3 Then : ConfigStringTemp = (ConfigString3) : End If
If AddressTemp = 4 Then : ConfigStringTemp = (ConfigString4) : End If
If AddressTemp = 5 Then : ConfigStringTemp = (ConfigString5) : End If
If AddressTemp = 6 Then : ConfigStringTemp = (ConfigString6) : End If
If AddressTemp = 7 Then : ConfigStringTemp = (ConfigString7) : End If
If AddressTemp = 8 Then : ConfigStringTemp = (ConfigString8) : End If
If AddressTemp = 9 Then : ConfigStringTemp = (ConfigString9) : End If
End Sub
' Configuration Section
Private Sub cmbArduinAddressSelect_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbArduinoAddressSelect.SelectedIndexChanged ' Select an Arduino Address in the Configuration Combo Box to begin the Config Process
If cmdEnableDin02.Enabled = False Then ' If the Config utility is not already in use...
ConfigArduinoAddress = Val(cmbArduinoAddressSelect.Text.Substring(8, 1)) ' Determine the address of the Arduino to be configured from the Combo Box text
ArduinoConfigRead() ' Determine which Arduino address number the user has selected and Populate the Config Section with the relevant Arduinos information
EnableConfigButtons() ' Config section buttons are enabled and cmbArduinoAddressSelect Combo Box is disabled
IOConfigButtons() ' Configure the Buttons in the Configuration Section
End If
End Sub
Private Sub ArduinoConfigRead() ' Determine which Arduino address number the user has selected and Populate the Config Section with the relevant Arduinos information
If ConfigArduinoAddress = 0 Then : ConfigStringTemp = ConfigString0 : ArduinoConfigReadAssist() : End If ' Determine which Arduino address number the user has selected...
If ConfigArduinoAddress = 1 Then : ConfigStringTemp = ConfigString1 : ArduinoConfigReadAssist() : End If ' Attach the appropriate ConfigString String to the ConfigStringTemp String...
If ConfigArduinoAddress = 2 Then : ConfigStringTemp = ConfigString2 : ArduinoConfigReadAssist() : End If ' Send that to ArduinoConfigReadAssist() which populates the Config Section with the relevant Arduinos information derived from the ConfigStringTemp String.
If ConfigArduinoAddress = 3 Then : ConfigStringTemp = ConfigString3 : ArduinoConfigReadAssist() : End If
If ConfigArduinoAddress = 4 Then : ConfigStringTemp = ConfigString4 : ArduinoConfigReadAssist() : End If
If ConfigArduinoAddress = 5 Then : ConfigStringTemp = ConfigString5 : ArduinoConfigReadAssist() : End If
If ConfigArduinoAddress = 6 Then : ConfigStringTemp = ConfigString6 : ArduinoConfigReadAssist() : End If
If ConfigArduinoAddress = 7 Then : ConfigStringTemp = ConfigString7 : ArduinoConfigReadAssist() : End If
If ConfigArduinoAddress = 8 Then : ConfigStringTemp = ConfigString8 : ArduinoConfigReadAssist() : End If
If ConfigArduinoAddress = 9 Then : ConfigStringTemp = ConfigString9 : ArduinoConfigReadAssist() : End If
End Sub
Private Sub ArduinoConfigReadAssist() ' Populate the Config Section with the relevant Arduinos information derived from the ConfigStringTemp String.
ConfigDin02State = ConfigStringTemp.Substring(2, 1) ' Din02 State
ConfigDin03State = ConfigStringTemp.Substring(3, 1) ' Din03 State
ConfigDin04State = ConfigStringTemp.Substring(4, 1) ' Din04 State
ConfigDin05State = ConfigStringTemp.Substring(5, 1) ' Din05 State
ConfigDin06State = ConfigStringTemp.Substring(6, 1) ' Din06 State
ConfigDin07State = ConfigStringTemp.Substring(7, 1) ' Din07 State
ConfigDin08State = ConfigStringTemp.Substring(8, 1) ' Din08 State
ConfigDin09State = ConfigStringTemp.Substring(9, 1) ' Din09 State
ConfigDin10State = ConfigStringTemp.Substring(10, 1) ' Din10 State
ConfigDin11State = ConfigStringTemp.Substring(11, 1) ' Din11 State
ConfigDin12State = ConfigStringTemp.Substring(12, 1) ' Din12 State
End Sub
Private Sub EnableConfigButtons() ' Config section buttons are enabled and cmbArduinoAddressSelect Combo Box is disabled
cmdEnableDin02.Enabled = True : cmdEnableDin03.Enabled = True : cmdEnableDin04.Enabled = True : cmdEnableDin05.Enabled = True
cmdEnableDin06.Enabled = True : cmdEnableDin07.Enabled = True : cmdEnableDin08.Enabled = True : cmdEnableDin09.Enabled = True
cmdEnableDin10.Enabled = True : cmdEnableDin11.Enabled = True : cmdEnableDin12.Enabled = True : cmdEnableDin13.Enabled = True
cmdConfigWriteToArduino.Enabled = True : cmdConfigDone.Enabled = True
cmbArduinoAddressSelect.Enabled = False
cmbTestAddressSelect.Enabled = False ' Disable Test Section
End Sub
Private Sub IOConfigButtons() ' Updates the Text and Colour of each IO config button to indicate the value or functionality of the Arduino input as selected by the user
' Din02
If ConfigDin02State = 0 Then : cmdEnableDin02.Text = "Input Low" : End If ' 0 = Input Enabled with a normally Low state
If ConfigDin02State = 1 Then : cmdEnableDin02.Text = "Input High" : End If ' 1 = Input Enabled with a normally High state
If ConfigDin02State = 2 Then : cmdEnableDin02.Text = "Output" : End If ' 2 = Set as an Output - Text, colour and font changed accordingly
' Din03
If ConfigDin03State = 0 Then : cmdEnableDin03.Text = "Input Low" : End If
If ConfigDin03State = 1 Then : cmdEnableDin03.Text = "Input High" : End If
If ConfigDin03State = 2 Then : cmdEnableDin03.Text = "Output" : End If
' Din04
If ConfigDin04State = 0 Then : cmdEnableDin04.Text = "Input Low" : End If
If ConfigDin04State = 1 Then : cmdEnableDin04.Text = "Input High" : End If
If ConfigDin04State = 2 Then : cmdEnableDin04.Text = "Output" : End If
' Din05
If ConfigDin05State = 0 Then : cmdEnableDin05.Text = "Input Low" : End If
If ConfigDin05State = 1 Then : cmdEnableDin05.Text = "Input High" : End If
If ConfigDin05State = 2 Then : cmdEnableDin05.Text = "Output" : End If
' Din06
If ConfigDin06State = 0 Then : cmdEnableDin06.Text = "Input Low" : End If
If ConfigDin06State = 1 Then : cmdEnableDin06.Text = "Input High" : End If
If ConfigDin06State = 2 Then : cmdEnableDin06.Text = "Output" : End If
' Din07
If ConfigDin07State = 0 Then : cmdEnableDin07.Text = "Input Low" : End If
If ConfigDin07State = 1 Then : cmdEnableDin07.Text = "Input High" : End If
If ConfigDin07State = 2 Then : cmdEnableDin07.Text = "Output" : End If
' Din08
If ConfigDin08State = 0 Then : cmdEnableDin08.Text = "Input Low" : End If
If ConfigDin08State = 1 Then : cmdEnableDin08.Text = "Input High" : End If
If ConfigDin08State = 2 Then : cmdEnableDin08.Text = "Output" : End If
' Din09
If ConfigDin09State = 0 Then : cmdEnableDin09.Text = "Input Low" : End If
If ConfigDin09State = 1 Then : cmdEnableDin09.Text = "Input High" : End If
If ConfigDin09State = 2 Then : cmdEnableDin09.Text = "Output" : End If
' Din10
If ConfigDin10State = 0 Then : cmdEnableDin10.Text = "Input Low" : End If
If ConfigDin10State = 1 Then : cmdEnableDin10.Text = "Input High" : End If
If ConfigDin10State = 2 Then : cmdEnableDin10.Text = "Output" : End If
' Din11
If ConfigDin11State = 0 Then : cmdEnableDin11.Text = "Input Low" : End If
If ConfigDin11State = 1 Then : cmdEnableDin11.Text = "Input High" : End If
If ConfigDin11State = 2 Then : cmdEnableDin11.Text = "Output" : End If
' Din12
If ConfigDin12State = 0 Then : cmdEnableDin12.Text = "Input Low" : End If
If ConfigDin12State = 1 Then : cmdEnableDin12.Text = "Input High" : End If
If ConfigDin12State = 2 Then : cmdEnableDin12.Text = "Output" : End If
' Din13
cmdEnableDin13.Text = "Output" ' Din13 can only be an Output
' Each time a change is made, the new information is written to the Local HD Config File
ConfigStringTemp = ">" & ConfigArduinoAddress & ConfigDin02State & ConfigDin03State & ConfigDin04State & ConfigDin05State & ConfigDin06State & ConfigDin07State ' Rebuild ConfigString according to changes made above
ConfigStringTemp = ConfigStringTemp & ConfigDin08State & ConfigDin09State & ConfigDin10State & ConfigDin11State & ConfigDin12State & "2<"
If ConfigArduinoAddress = 0 Then : ConfigString0 = ConfigStringTemp : End If : If ConfigArduinoAddress = 1 Then : ConfigString1 = ConfigStringTemp : End If ' Write the new ConfigString to the appropriate varible
If ConfigArduinoAddress = 2 Then : ConfigString2 = ConfigStringTemp : End If : If ConfigArduinoAddress = 3 Then : ConfigString3 = ConfigStringTemp : End If
If ConfigArduinoAddress = 4 Then : ConfigString4 = ConfigStringTemp : End If : If ConfigArduinoAddress = 5 Then : ConfigString5 = ConfigStringTemp : End If
If ConfigArduinoAddress = 6 Then : ConfigString6 = ConfigStringTemp : End If : If ConfigArduinoAddress = 7 Then : ConfigString7 = ConfigStringTemp : End If
If ConfigArduinoAddress = 8 Then : ConfigString8 = ConfigStringTemp : End If : If ConfigArduinoAddress = 9 Then : ConfigString9 = ConfigStringTemp : End If
' Writes the current config to a text file to local HD
If System.IO.File.Exists("C:\EmmaJane\ConfigFile.txt") = True Then ' First Delete File
System.IO.File.Delete("C:\EmmaJane\ConfigFile.txt")
End If
ConfigFileOut = My.Computer.FileSystem.OpenTextFileWriter("C:\EmmaJane\ConfigFile.txt", True) ' Write New Config File
ConfigFileOut.WriteLine(ConfigString0) : ConfigFileOut.WriteLine(ConfigString1)
ConfigFileOut.WriteLine(ConfigString2) : ConfigFileOut.WriteLine(ConfigString3)
ConfigFileOut.WriteLine(ConfigString4) : ConfigFileOut.WriteLine(ConfigString5)
ConfigFileOut.WriteLine(ConfigString6) : ConfigFileOut.WriteLine(ConfigString7)
ConfigFileOut.WriteLine(ConfigString8) : ConfigFileOut.WriteLine(ConfigString9)
ConfigFileOut.Close()
End Sub
Private Sub cmdEnableDin02_Click(sender As Object, e As EventArgs) Handles cmdEnableDin02.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin02State = ConfigDin02State + 1 : If ConfigDin02State > 2 Then : ConfigDin02State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin03_Click(sender As Object, e As EventArgs) Handles cmdEnableDin03.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin03State = ConfigDin03State + 1 : If ConfigDin03State > 2 Then : ConfigDin03State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin04_Click(sender As Object, e As EventArgs) Handles cmdEnableDin04.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin04State = ConfigDin04State + 1 : If ConfigDin04State > 2 Then : ConfigDin04State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin05_Click(sender As Object, e As EventArgs) Handles cmdEnableDin05.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin05State = ConfigDin05State + 1 : If ConfigDin05State > 2 Then : ConfigDin05State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin06_Click(sender As Object, e As EventArgs) Handles cmdEnableDin06.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin06State = ConfigDin06State + 1 : If ConfigDin06State > 2 Then : ConfigDin06State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin07_Click(sender As Object, e As EventArgs) Handles cmdEnableDin07.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin07State = ConfigDin07State + 1 : If ConfigDin07State > 2 Then : ConfigDin07State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin08_Click(sender As Object, e As EventArgs) Handles cmdEnableDin08.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin08State = ConfigDin08State + 1 : If ConfigDin08State > 2 Then : ConfigDin08State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin09_Click(sender As Object, e As EventArgs) Handles cmdEnableDin09.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin09State = ConfigDin09State + 1 : If ConfigDin09State > 2 Then : ConfigDin09State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin10_Click(sender As Object, e As EventArgs) Handles cmdEnableDin10.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin10State = ConfigDin10State + 1 : If ConfigDin10State > 2 Then : ConfigDin10State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin11_Click(sender As Object, e As EventArgs) Handles cmdEnableDin11.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin11State = ConfigDin11State + 1 : If ConfigDin11State > 2 Then : ConfigDin11State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdEnableDin12_Click(sender As Object, e As EventArgs) Handles cmdEnableDin12.Click ' Din Config (Functionality) Selection then write to IOConfigButtons()
ConfigDin12State = ConfigDin12State + 1 : If ConfigDin12State > 2 Then : ConfigDin12State = 0 : End If : IOConfigButtons()
End Sub
Private Sub cmdConfigWriteToArduino_Click(sender As Object, e As EventArgs) Handles cmdConfigWriteToArduino.Click ' Manage the writing of the newly configured Config String to the selected Arduino
If SrPt0Add = ConfigArduinoAddress Then : SrPt0Add = 99 : End If ' Find the right Addressed Serial Port...
If SrPt1Add = ConfigArduinoAddress Then : SrPt1Add = 99 : End If ' Set Address to 99 to force a reset of that ComPort
If SrPt2Add = ConfigArduinoAddress Then : SrPt2Add = 99 : End If
If SrPt3Add = ConfigArduinoAddress Then : SrPt3Add = 99 : End If
If SrPt4Add = ConfigArduinoAddress Then : SrPt4Add = 99 : End If
If SrPt5Add = ConfigArduinoAddress Then : SrPt5Add = 99 : End If
If SrPt6Add = ConfigArduinoAddress Then : SrPt6Add = 99 : End If
If SrPt7Add = ConfigArduinoAddress Then : SrPt7Add = 99 : End If
If SrPt8Add = ConfigArduinoAddress Then : SrPt8Add = 99 : End If
If SrPt9Add = ConfigArduinoAddress Then : SrPt9Add = 99 : End If
DisableConfigButtons() ' Disables Config Section buttons
timInitiateArduinos.Enabled = True ' Begin timed sequence for initiating Comm Ports and identifying Arduino addresses
End Sub
Private Sub cmdConfigDone_Click(sender As Object, e As EventArgs) Handles cmdConfigDone.Click ' Ends config section without writing to Arduino
cmbArduinoAddressSelect.SelectedItem = Nothing ' Clears Conbo Box
DisableConfigButtons() ' Disables Config Section buttons
End Sub
Private Sub DisableConfigButtons() ' Config section buttons are Disabled and cmbArduinoAddressSelect Combo Box is Enabled - Colour set back to White
cmdEnableDin02.Enabled = False : cmdEnableDin03.Enabled = False : cmdEnableDin04.Enabled = False : cmdEnableDin05.Enabled = False
cmdEnableDin06.Enabled = False : cmdEnableDin07.Enabled = False : cmdEnableDin08.Enabled = False : cmdEnableDin09.Enabled = False
cmdEnableDin10.Enabled = False : cmdEnableDin11.Enabled = False : cmdEnableDin12.Enabled = False : cmdEnableDin13.Enabled = False
cmdConfigWriteToArduino.Enabled = False : cmdConfigDone.Enabled = False : cmbArduinoAddressSelect.Enabled = True
cmdEnableDin02.ForeColor = Color.White : cmdEnableDin03.ForeColor = Color.White : cmdEnableDin04.ForeColor = Color.White : cmdEnableDin05.ForeColor = Color.White
cmdEnableDin06.ForeColor = Color.White : cmdEnableDin07.ForeColor = Color.White : cmdEnableDin08.ForeColor = Color.White : cmdEnableDin09.ForeColor = Color.White
cmdEnableDin10.ForeColor = Color.White : cmdEnableDin11.ForeColor = Color.White : cmdEnableDin12.ForeColor = Color.White : cmdEnableDin13.ForeColor = Color.White
cmdConfigWriteToArduino.ForeColor = Color.White : cmdConfigDone.ForeColor = Color.White : ConfigArduinoAddress = 99
cmbTestAddressSelect.Enabled = True ' Enable Test Section
End Sub
' DataHandler
Private Sub timDatahandler_Tick(sender As Object, e As EventArgs) Handles timDatahandler.Tick ' Timer to Send and Receive DataStrings (500 ms)
If lblDataString.ForeColor = Color.White Then : lblDataString.ForeColor = Color.Lime : Else : lblDataString.ForeColor = Color.White : End If ' Flash the DataString label while Communicating with the Arduinos
ReadFromArduino() ' Read any received Data from all Arduinos
TransmitToArduino() ' Transmit Data to Arduinos on change of DataString
CommsFail() ' Detect Communications Failure to Arduino and force a Reset of Arduino - after 20 consecutive transmission failures
If TestModeActive = 1 Then : DisplayTestDataString() : End If ' If in Test Mode - Update test Mode Display
End Sub
Private Sub ReadFromArduino() ' Read any received Data from all Arduinos
Try : SrPtDataIn0 = SrPt0.ReadExisting() : Catch : End Try ' Read Data String from SerialPort0
If SrPtDataIn0 <> "" Then : If SrPtDataIn0.Length >= 15 Then ' If Received Serial Data Holder is not empty and if Received Serial Data is 15 digits or longer
Do While SrPtDataIn0.Length > 15 : SrPtDataIn0 = SrPtDataIn0.Remove(SrPtDataIn0.Length - 1, 1) : Loop ' If String length is more than 15 characters...Reduce length
If SrPtDataIn0.Substring(0, 1) = "<" And SrPtDataIn0.Substring(14, 1) = ">" Then ' If String contains < and > in the correct positions...
DataStringAddTemp = Val(SrPtDataIn0.Substring(1, 1)) ' Find the address and assign to a Temp Varible
DataStringTemp = SrPtDataIn0 : TransmissionAssistRead() ' Assign String to a Temp Varible and Send to TransmissionAssistRead() to Assign to correct Address DataString
End If : End If : End If
Try : SrPtDataIn1 = SrPt1.ReadExisting() : Catch : End Try
If SrPtDataIn1 <> "" Then : If SrPtDataIn1.Length >= 15 Then
Do While SrPtDataIn1.Length > 15 : SrPtDataIn1 = SrPtDataIn1.Remove(SrPtDataIn1.Length - 1, 1) : Loop
If SrPtDataIn1.Substring(0, 1) = "<" And SrPtDataIn1.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn1.Substring(1, 1))
DataStringTemp = SrPtDataIn1 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn2 = SrPt2.ReadExisting() : Catch : End Try
If SrPtDataIn2 <> "" Then : If SrPtDataIn2.Length >= 15 Then
Do While SrPtDataIn2.Length > 15 : SrPtDataIn2 = SrPtDataIn2.Remove(SrPtDataIn2.Length - 1, 1) : Loop
If SrPtDataIn2.Substring(0, 1) = "<" And SrPtDataIn2.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn2.Substring(1, 1))
DataStringTemp = SrPtDataIn2 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn3 = SrPt3.ReadExisting() : Catch : End Try
If SrPtDataIn3 <> "" Then : If SrPtDataIn3.Length >= 15 Then
Do While SrPtDataIn3.Length > 15 : SrPtDataIn3 = SrPtDataIn3.Remove(SrPtDataIn3.Length - 1, 1) : Loop
If SrPtDataIn3.Substring(0, 1) = "<" And SrPtDataIn3.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn3.Substring(1, 1))
DataStringTemp = SrPtDataIn3 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn4 = SrPt4.ReadExisting() : Catch : End Try
If SrPtDataIn4 <> "" Then : If SrPtDataIn4.Length >= 15 Then
Do While SrPtDataIn4.Length > 15 : SrPtDataIn4 = SrPtDataIn4.Remove(SrPtDataIn4.Length - 1, 1) : Loop
If SrPtDataIn4.Substring(0, 1) = "<" And SrPtDataIn4.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn4.Substring(1, 1))
DataStringTemp = SrPtDataIn4 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn5 = SrPt5.ReadExisting() : Catch : End Try
If SrPtDataIn5 <> "" Then : If SrPtDataIn5.Length >= 15 Then
Do While SrPtDataIn5.Length > 15 : SrPtDataIn5 = SrPtDataIn5.Remove(SrPtDataIn5.Length - 1, 1) : Loop
If SrPtDataIn5.Substring(0, 1) = "<" And SrPtDataIn5.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn5.Substring(1, 1))
DataStringTemp = SrPtDataIn5 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn6 = SrPt6.ReadExisting() : Catch : End Try
If SrPtDataIn6 <> "" Then : If SrPtDataIn6.Length >= 15 Then
Do While SrPtDataIn6.Length > 15 : SrPtDataIn6 = SrPtDataIn6.Remove(SrPtDataIn6.Length - 1, 1) : Loop
If SrPtDataIn6.Substring(0, 1) = "<" And SrPtDataIn6.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn6.Substring(1, 1))
DataStringTemp = SrPtDataIn6 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn7 = SrPt7.ReadExisting() : Catch : End Try
If SrPtDataIn7 <> "" Then : If SrPtDataIn7.Length >= 15 Then
Do While SrPtDataIn7.Length > 15 : SrPtDataIn7 = SrPtDataIn7.Remove(SrPtDataIn7.Length - 1, 1) : Loop
If SrPtDataIn7.Substring(0, 1) = "<" And SrPtDataIn7.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn7.Substring(1, 1))
DataStringTemp = SrPtDataIn7 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn8 = SrPt8.ReadExisting() : Catch : End Try
If SrPtDataIn8 <> "" Then : If SrPtDataIn8.Length >= 15 Then
Do While SrPtDataIn8.Length > 15 : SrPtDataIn8 = SrPtDataIn8.Remove(SrPtDataIn8.Length - 1, 1) : Loop
If SrPtDataIn8.Substring(0, 1) = "<" And SrPtDataIn8.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn8.Substring(1, 1))
DataStringTemp = SrPtDataIn8 : TransmissionAssistRead()
End If : End If : End If
Try : SrPtDataIn9 = SrPt9.ReadExisting() : Catch : End Try
If SrPtDataIn9 <> "" Then : If SrPtDataIn9.Length >= 15 Then
Do While SrPtDataIn9.Length > 15 : SrPtDataIn9 = SrPtDataIn9.Remove(SrPtDataIn9.Length - 1, 1) : Loop
If SrPtDataIn9.Substring(0, 1) = "<" And SrPtDataIn9.Substring(14, 1) = ">" Then
DataStringAddTemp = Val(SrPtDataIn9.Substring(1, 1))
DataStringTemp = SrPtDataIn9 : TransmissionAssistRead()
End If : End If : End If
End Sub
Private Sub TransmissionAssistRead() ' Assign incoming Data from Serial Ports to correct DataString
If DataStringAddTemp = 0 Then : DataStringIn0 = DataStringTemp : End If ' If If Incoming Serial Port Data has an Address of X then...
If DataStringAddTemp = 1 Then : DataStringIn1 = DataStringTemp : End If ' Assign the Incoming Serial Port Data to DataString X
If DataStringAddTemp = 2 Then : DataStringIn2 = DataStringTemp : End If
If DataStringAddTemp = 3 Then : DataStringIn3 = DataStringTemp : End If
If DataStringAddTemp = 4 Then : DataStringIn4 = DataStringTemp : End If
If DataStringAddTemp = 5 Then : DataStringIn5 = DataStringTemp : End If
If DataStringAddTemp = 6 Then : DataStringIn6 = DataStringTemp : End If
If DataStringAddTemp = 7 Then : DataStringIn7 = DataStringTemp : End If
If DataStringAddTemp = 8 Then : DataStringIn8 = DataStringTemp : End If
If DataStringAddTemp = 9 Then : DataStringIn9 = DataStringTemp : End If
End Sub
Private Sub TransmitToArduino() ' Transmit Data to Arduinos on change of DataString
If DataStringIn0 <> DataString0 Then ' If Incoming Data from the Serial Port is not the same as the Primary Data String...
ConfigStringTemp = ConfigString0 : DataStringTemp = DataString0 : DataStringInTemp = DataStringIn0 : ReadFromArduinAssist() ' Prepare temp values for ConfigString, DataString and DataStringIn and send all to ReadFromArduinAssist()
DataString0 = DataStringTemp : ForceArduinoWrite0 = 1 : End If ' Accept ReadFromArduinAssist() resulting DataString as the new DataString - Force VB to Write to Arduino on next Tx cycle
If DataStringIn0 <> DataString0 Or ForceArduinoWrite0 = 1 Then : ForceArduinoWrite0 = 0 ' If Incoming Data from the Serial Port is not the same as the Primary Data String or VB Write to arduino is being forced .... Reset Write force....
If DataString0.Length = 15 Then : If DataString0.Substring(0, 1) = "<" And DataString0.Substring(14, 1) = ">" Then ' If Data String length is 15 Characters and String contains < and > in the correct positions...
cmdAdd0DataString.ForeColor = Color.Red : cmdAdd0DataString.Text = DataString0 : CommsFailCnt0 = CommsFailCnt0 + 1 ' DataString Text is coloured Red - DataString Textbox is set to DataString - CommsFail Counter is Incremented
DataStringTemp = DataString0 : DataStringAddTemp = Val(DataString0.Substring(1, 1)) : TransmissionAssistWrite() ' Create Temp String and Address...Send Temp String and Address to TransmissionAssistWrite() to be written to correct Serial Port/Arduino
End If : End If : Else
cmdAdd0DataString.ForeColor = Color.White : CommsFailCnt0 = 0 : End If ' If Incoming Data from the Serial Port IS the same as the Primary Data String... DataString Text is set to White Colour and CommsFailCnt is reset
If DataStringIn1 <> DataString1 Then
ConfigStringTemp = ConfigString1 : DataStringTemp = DataString1 : DataStringInTemp = DataStringIn1 : ReadFromArduinAssist()
DataString1 = DataStringTemp : ForceArduinoWrite1 = 1 : End If
If DataStringIn1 <> DataString1 Or ForceArduinoWrite1 = 1 Then : ForceArduinoWrite1 = 0
If DataString1.Length = 15 Then : If DataString1.Substring(0, 1) = "<" And DataString1.Substring(14, 1) = ">" Then
cmdAdd1DataString.ForeColor = Color.Red : cmdAdd1DataString.Text = DataString1 : CommsFailCnt1 = CommsFailCnt1 + 1
DataStringTemp = DataString1 : DataStringAddTemp = Val(DataString1.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd1DataString.ForeColor = Color.White : CommsFailCnt1 = 0 : End If
If DataStringIn2 <> DataString2 Then
ConfigStringTemp = ConfigString2 : DataStringTemp = DataString2 : DataStringInTemp = DataStringIn2 : ReadFromArduinAssist()
DataString2 = DataStringTemp : ForceArduinoWrite2 = 1 : End If
If DataStringIn2 <> DataString2 Or ForceArduinoWrite2 = 1 Then : ForceArduinoWrite2 = 0
If DataString2.Length = 15 Then : If DataString2.Substring(0, 1) = "<" And DataString2.Substring(14, 1) = ">" Then
cmdAdd2DataString.ForeColor = Color.Red : cmdAdd2DataString.Text = DataString2 : CommsFailCnt2 = CommsFailCnt2 + 1
DataStringTemp = DataString2 : DataStringAddTemp = Val(DataString2.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd2DataString.ForeColor = Color.White : CommsFailCnt2 = 0 : End If
If DataStringIn3 <> DataString3 Then
ConfigStringTemp = ConfigString3 : DataStringTemp = DataString3 : DataStringInTemp = DataStringIn3 : ReadFromArduinAssist()
DataString3 = DataStringTemp : ForceArduinoWrite3 = 1 : End If
If DataStringIn3 <> DataString3 Or ForceArduinoWrite3 = 1 Then : ForceArduinoWrite3 = 0
If DataString3.Length = 15 Then : If DataString3.Substring(0, 1) = "<" And DataString3.Substring(14, 1) = ">" Then
cmdAdd3DataString.ForeColor = Color.Red : cmdAdd3DataString.Text = DataString3 : CommsFailCnt3 = CommsFailCnt3 + 1
DataStringTemp = DataString3 : DataStringAddTemp = Val(DataString3.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd3DataString.ForeColor = Color.White : CommsFailCnt3 = 0 : End If
If DataStringIn4 <> DataString4 Then
ConfigStringTemp = ConfigString4 : DataStringTemp = DataString4 : DataStringInTemp = DataStringIn4 : ReadFromArduinAssist()
DataString4 = DataStringTemp : ForceArduinoWrite4 = 1 : End If
If DataStringIn4 <> DataString4 Or ForceArduinoWrite4 = 1 Then : ForceArduinoWrite4 = 0
If DataString4.Length = 15 Then : If DataString4.Substring(0, 1) = "<" And DataString4.Substring(14, 1) = ">" Then
cmdAdd4DataString.ForeColor = Color.Red : cmdAdd4DataString.Text = DataString4 : CommsFailCnt4 = CommsFailCnt4 + 1
DataStringTemp = DataString4 : DataStringAddTemp = Val(DataString4.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd4DataString.ForeColor = Color.White : CommsFailCnt4 = 0 : End If
If DataStringIn5 <> DataString5 Then
ConfigStringTemp = ConfigString5 : DataStringTemp = DataString5 : DataStringInTemp = DataStringIn5 : ReadFromArduinAssist()
DataString5 = DataStringTemp : ForceArduinoWrite5 = 1 : End If
If DataStringIn5 <> DataString5 Or ForceArduinoWrite5 = 1 Then : ForceArduinoWrite5 = 0
If DataString5.Length = 15 Then : If DataString5.Substring(0, 1) = "<" And DataString5.Substring(14, 1) = ">" Then
cmdAdd5DataString.ForeColor = Color.Red : cmdAdd5DataString.Text = DataString5 : CommsFailCnt5 = CommsFailCnt5 + 1
DataStringTemp = DataString5 : DataStringAddTemp = Val(DataString5.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd5DataString.ForeColor = Color.White : CommsFailCnt5 = 0 : End If
If DataStringIn6 <> DataString6 Then
ConfigStringTemp = ConfigString6 : DataStringTemp = DataString6 : DataStringInTemp = DataStringIn6 : ReadFromArduinAssist()
DataString6 = DataStringTemp : ForceArduinoWrite6 = 1 : End If
If DataStringIn6 <> DataString6 Or ForceArduinoWrite6 = 1 Then : ForceArduinoWrite6 = 0
If DataString6.Length = 15 Then : If DataString6.Substring(0, 1) = "<" And DataString6.Substring(14, 1) = ">" Then
cmdAdd6DataString.ForeColor = Color.Red : cmdAdd6DataString.Text = DataString6 : CommsFailCnt6 = CommsFailCnt6 + 1
DataStringTemp = DataString6 : DataStringAddTemp = Val(DataString6.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd6DataString.ForeColor = Color.White : CommsFailCnt6 = 0 : End If
If DataStringIn7 <> DataString7 Then
ConfigStringTemp = ConfigString7 : DataStringTemp = DataString7 : DataStringInTemp = DataStringIn7 : ReadFromArduinAssist()
DataString7 = DataStringTemp : ForceArduinoWrite7 = 1 : End If
If DataStringIn7 <> DataString7 Or ForceArduinoWrite7 = 1 Then : ForceArduinoWrite7 = 0
If DataString7.Length = 15 Then : If DataString7.Substring(0, 1) = "<" And DataString7.Substring(14, 1) = ">" Then
cmdAdd7DataString.ForeColor = Color.Red : cmdAdd7DataString.Text = DataString7 : CommsFailCnt7 = CommsFailCnt7 + 1
DataStringTemp = DataString7 : DataStringAddTemp = Val(DataString7.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd7DataString.ForeColor = Color.White : CommsFailCnt7 = 0 : End If
If DataStringIn8 <> DataString8 Then
ConfigStringTemp = ConfigString8 : DataStringTemp = DataString8 : DataStringInTemp = DataStringIn8 : ReadFromArduinAssist()
DataString8 = DataStringTemp : ForceArduinoWrite8 = 1 : End If
If DataStringIn8 <> DataString8 Or ForceArduinoWrite8 = 1 Then : ForceArduinoWrite8 = 0
If DataString8.Length = 15 Then : If DataString8.Substring(0, 1) = "<" And DataString8.Substring(14, 1) = ">" Then
cmdAdd8DataString.ForeColor = Color.Red : cmdAdd8DataString.Text = DataString8 : CommsFailCnt8 = CommsFailCnt8 + 1
DataStringTemp = DataString8 : DataStringAddTemp = Val(DataString8.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd8DataString.ForeColor = Color.White : CommsFailCnt8 = 0 : End If
If DataStringIn9 <> DataString9 Then
ConfigStringTemp = ConfigString9 : DataStringTemp = DataString9 : DataStringInTemp = DataStringIn9 : ReadFromArduinAssist()
DataString9 = DataStringTemp : ForceArduinoWrite9 = 1 : End If
If DataStringIn9 <> DataString9 Or ForceArduinoWrite9 = 1 Then : ForceArduinoWrite9 = 0
If DataString9.Length = 15 Then : If DataString9.Substring(0, 1) = "<" And DataString9.Substring(14, 1) = ">" Then
cmdAdd9DataString.ForeColor = Color.Red : cmdAdd9DataString.Text = DataString9 : CommsFailCnt9 = CommsFailCnt9 + 1
DataStringTemp = DataString9 : DataStringAddTemp = Val(DataString9.Substring(1, 1)) : TransmissionAssistWrite()
End If : End If : Else
cmdAdd9DataString.ForeColor = Color.White : CommsFailCnt9 = 0 : End If
End Sub
Private Sub ReadFromArduinAssist() ' Ensure that Arduino can only adjust Input Digits of DataString
If ConfigStringTemp.Substring(2, 1) <> "2" Then ' If DataString Digit is not configured as an Output...
DataStringTemp = DataStringTemp.Remove(2, 1) : DataStringTemp = DataStringTemp.Insert(2, DataStringInTemp.Substring(2, 1)) : End If ' Remove Digit of DataString - Insert Digit from Incoming Serial Port String into Primary Data String
If ConfigStringTemp.Substring(3, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(3, 1) : DataStringTemp = DataStringTemp.Insert(3, DataStringInTemp.Substring(3, 1)) : End If
If ConfigStringTemp.Substring(4, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(4, 1) : DataStringTemp = DataStringTemp.Insert(4, DataStringInTemp.Substring(4, 1)) : End If
If ConfigStringTemp.Substring(5, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(5, 1) : DataStringTemp = DataStringTemp.Insert(5, DataStringInTemp.Substring(5, 1)) : End If
If ConfigStringTemp.Substring(6, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(6, 1) : DataStringTemp = DataStringTemp.Insert(6, DataStringInTemp.Substring(6, 1)) : End If
If ConfigStringTemp.Substring(7, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(7, 1) : DataStringTemp = DataStringTemp.Insert(7, DataStringInTemp.Substring(7, 1)) : End If
If ConfigStringTemp.Substring(8, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(8, 1) : DataStringTemp = DataStringTemp.Insert(8, DataStringInTemp.Substring(8, 1)) : End If
If ConfigStringTemp.Substring(9, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(9, 1) : DataStringTemp = DataStringTemp.Insert(9, DataStringInTemp.Substring(9, 1)) : End If
If ConfigStringTemp.Substring(10, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(10, 1) : DataStringTemp = DataStringTemp.Insert(10, DataStringInTemp.Substring(10, 1)) : End If
If ConfigStringTemp.Substring(11, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(11, 1) : DataStringTemp = DataStringTemp.Insert(11, DataStringInTemp.Substring(11, 1)) : End If
If ConfigStringTemp.Substring(12, 1) <> "2" Then
DataStringTemp = DataStringTemp.Remove(12, 1) : DataStringTemp = DataStringTemp.Insert(12, DataStringInTemp.Substring(12, 1)) : End If
End Sub
Private Sub TransmissionAssistWrite() ' Write String to correct Serial Port/Arduino
If SrPt0Add = DataStringAddTemp Then : If SrPt0Connected = True Then : Try : SrPt0.WriteLine(DataStringTemp) : Catch : End Try : End If : End If ' If DataString is addressed for Serial Port X...
If SrPt1Add = DataStringAddTemp Then : If SrPt1Connected = True Then : Try : SrPt1.WriteLine(DataStringTemp) : Catch : End Try : End If : End If ' And Serial Port X is Connected to an Arduino...
If SrPt2Add = DataStringAddTemp Then : If SrPt2Connected = True Then : Try : SrPt2.WriteLine(DataStringTemp) : Catch : End Try : End If : End If ' Write the DataString to the Arduino X
If SrPt3Add = DataStringAddTemp Then : If SrPt3Connected = True Then : Try : SrPt3.WriteLine(DataStringTemp) : Catch : End Try : End If : End If
If SrPt4Add = DataStringAddTemp Then : If SrPt4Connected = True Then : Try : SrPt4.WriteLine(DataStringTemp) : Catch : End Try : End If : End If
If SrPt5Add = DataStringAddTemp Then : If SrPt5Connected = True Then : Try : SrPt5.WriteLine(DataStringTemp) : Catch : End Try : End If : End If
If SrPt6Add = DataStringAddTemp Then : If SrPt6Connected = True Then : Try : SrPt6.WriteLine(DataStringTemp) : Catch : End Try : End If : End If
If SrPt7Add = DataStringAddTemp Then : If SrPt7Connected = True Then : Try : SrPt7.WriteLine(DataStringTemp) : Catch : End Try : End If : End If
If SrPt8Add = DataStringAddTemp Then : If SrPt8Connected = True Then : Try : SrPt8.WriteLine(DataStringTemp) : Catch : End Try : End If : End If
If SrPt9Add = DataStringAddTemp Then : If SrPt9Connected = True Then : Try : SrPt9.WriteLine(DataStringTemp) : Catch : End Try : End If : End If
End Sub
Private Sub CommsFail() ' Detects if any Arduino is in Comms Fail by counting how many consecutive transmission failures it experiences - after 20 consecutive transmission failures
If CommsFailCnt0 >= 20 Then : CommsFailCnt0 = 0 : CommsFailTemp = 0 : CommsFailAssist() : End If ' if CommsFailCnt from TransmitToArduino() is 20 or greater...
If CommsFailCnt1 >= 20 Then : CommsFailCnt1 = 0 : CommsFailTemp = 1 : CommsFailAssist() : End If ' Reset CommsFailCnt...
If CommsFailCnt2 >= 20 Then : CommsFailCnt2 = 0 : CommsFailTemp = 2 : CommsFailAssist() : End If ' Assigns CommsFailTemp to Station Address Number and sends to CommsFailAssist() to Reset appropriate Arduino
If CommsFailCnt3 >= 20 Then : CommsFailCnt3 = 0 : CommsFailTemp = 3 : CommsFailAssist() : End If
If CommsFailCnt4 >= 20 Then : CommsFailCnt4 = 0 : CommsFailTemp = 4 : CommsFailAssist() : End If
If CommsFailCnt5 >= 20 Then : CommsFailCnt5 = 0 : CommsFailTemp = 5 : CommsFailAssist() : End If
If CommsFailCnt6 >= 20 Then : CommsFailCnt6 = 0 : CommsFailTemp = 6 : CommsFailAssist() : End If
If CommsFailCnt7 >= 20 Then : CommsFailCnt7 = 0 : CommsFailTemp = 7 : CommsFailAssist() : End If
If CommsFailCnt8 >= 20 Then : CommsFailCnt8 = 0 : CommsFailTemp = 8 : CommsFailAssist() : End If
If CommsFailCnt9 >= 20 Then : CommsFailCnt9 = 0 : CommsFailTemp = 9 : CommsFailAssist() : End If
End Sub
Private Sub CommsFailAssist() ' Resets Arduinos that have been flagged to be a Comms Fail State
If SrPt0Add = CommsFailTemp Then : SrPt0Add = 99 : timInitiateArduinos.Enabled = True : End If ' Identify which Serial Port the Station Address is attached to...
If SrPt1Add = CommsFailTemp Then : SrPt1Add = 99 : timInitiateArduinos.Enabled = True : End If ' Set that Serial Port Address to 99 to force a reseting of the Arduino...
If SrPt2Add = CommsFailTemp Then : SrPt2Add = 99 : timInitiateArduinos.Enabled = True : End If ' Begin the Arduino Initiate Timer
If SrPt3Add = CommsFailTemp Then : SrPt3Add = 99 : timInitiateArduinos.Enabled = True : End If
If SrPt4Add = CommsFailTemp Then : SrPt4Add = 99 : timInitiateArduinos.Enabled = True : End If
If SrPt5Add = CommsFailTemp Then : SrPt5Add = 99 : timInitiateArduinos.Enabled = True : End If
If SrPt6Add = CommsFailTemp Then : SrPt6Add = 99 : timInitiateArduinos.Enabled = True : End If
If SrPt7Add = CommsFailTemp Then : SrPt7Add = 99 : timInitiateArduinos.Enabled = True : End If
If SrPt8Add = CommsFailTemp Then : SrPt8Add = 99 : timInitiateArduinos.Enabled = True : End If
If SrPt9Add = CommsFailTemp Then : SrPt9Add = 99 : timInitiateArduinos.Enabled = True : End If
End Sub
' TestSection
Private Sub cmbTestAddressSelect_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbTestAddressSelect.SelectedIndexChanged ' Selecting Address in the Test Section Combo Box initiates the Test Section which allows for Station monitoring and Manual Output Switching
TestArduinoAddress = Val(cmbTestAddressSelect.Text.Substring(8, 1)) ' Determine the address of the Arduino to be configured from the Combo Box text
DisableTestButtons() ' Disables the buttons in the Test Section Group Box while Enabling the Config Section Combo Box
TestModeActive = 0 ' Takes Program out of Test Mode
If TestArduinoAddress = 0 And AddIsActive0 = 1 Then : TestConfigString = ConfigString0 : CollectTestConfigString() : End If ' If Arduino Test Address is X and that Address is Active...
If TestArduinoAddress = 1 And AddIsActive1 = 1 Then : TestConfigString = ConfigString1 : CollectTestConfigString() : End If ' Set TestConfigString to ConfigStringX and send to CollectTestConfigString() to Colour and Enable Buttons
If TestArduinoAddress = 2 And AddIsActive2 = 1 Then : TestConfigString = ConfigString2 : CollectTestConfigString() : End If
If TestArduinoAddress = 3 And AddIsActive3 = 1 Then : TestConfigString = ConfigString3 : CollectTestConfigString() : End If
If TestArduinoAddress = 4 And AddIsActive4 = 1 Then : TestConfigString = ConfigString4 : CollectTestConfigString() : End If
If TestArduinoAddress = 5 And AddIsActive5 = 1 Then : TestConfigString = ConfigString5 : CollectTestConfigString() : End If
If TestArduinoAddress = 6 And AddIsActive6 = 1 Then : TestConfigString = ConfigString6 : CollectTestConfigString() : End If
If TestArduinoAddress = 7 And AddIsActive7 = 1 Then : TestConfigString = ConfigString7 : CollectTestConfigString() : End If
If TestArduinoAddress = 8 And AddIsActive8 = 1 Then : TestConfigString = ConfigString8 : CollectTestConfigString() : End If
If TestArduinoAddress = 9 And AddIsActive9 = 1 Then : TestConfigString = ConfigString9 : CollectTestConfigString() : End If
End Sub
Private Sub CollectTestConfigString() ' Prepare the Test Section to manually test the selected Address
If TestConfigString.Substring(2, 1) = 2 Then : cmdTestDin02.ForeColor = Color.Red : Else : cmdTestDin02.ForeColor = Color.Lime : End If ' If Temp DataString digit is X then...
If TestConfigString.Substring(3, 1) = 2 Then : cmdTestDin03.ForeColor = Color.Red : Else : cmdTestDin03.ForeColor = Color.Lime : End If ' Colour it Red if it is an Output or Green if it is an Input
If TestConfigString.Substring(4, 1) = 2 Then : cmdTestDin04.ForeColor = Color.Red : Else : cmdTestDin04.ForeColor = Color.Lime : End If
If TestConfigString.Substring(5, 1) = 2 Then : cmdTestDin05.ForeColor = Color.Red : Else : cmdTestDin05.ForeColor = Color.Lime : End If
If TestConfigString.Substring(6, 1) = 2 Then : cmdTestDin06.ForeColor = Color.Red : Else : cmdTestDin06.ForeColor = Color.Lime : End If
If TestConfigString.Substring(7, 1) = 2 Then : cmdTestDin07.ForeColor = Color.Red : Else : cmdTestDin07.ForeColor = Color.Lime : End If
If TestConfigString.Substring(8, 1) = 2 Then : cmdTestDin08.ForeColor = Color.Red : Else : cmdTestDin08.ForeColor = Color.Lime : End If
If TestConfigString.Substring(9, 1) = 2 Then : cmdTestDin09.ForeColor = Color.Red : Else : cmdTestDin09.ForeColor = Color.Lime : End If
If TestConfigString.Substring(10, 1) = 2 Then : cmdTestDin10.ForeColor = Color.Red : Else : cmdTestDin10.ForeColor = Color.Lime : End If
If TestConfigString.Substring(11, 1) = 2 Then : cmdTestDin11.ForeColor = Color.Red : Else : cmdTestDin11.ForeColor = Color.Lime : End If
If TestConfigString.Substring(12, 1) = 2 Then : cmdTestDin12.ForeColor = Color.Red : Else : cmdTestDin12.ForeColor = Color.Lime : End If
If TestConfigString.Substring(13, 1) = 2 Then : cmdTestDin13.ForeColor = Color.Red : Else : cmdTestDin13.ForeColor = Color.Lime : End If
EnableTestButtons() ' Enable the Buttons in the Test Section
TestModeActive = 1 ' Place the Program is Test Mode
End Sub
Private Sub EnableTestButtons() ' Enables the buttons in the Test Section Group Box while Disabling the Config Section Combo Box
cmdTestDin02.Enabled = True : cmdTestDin03.Enabled = True : cmdTestDin04.Enabled = True : cmdTestDin05.Enabled = True : cmdTestDin06.Enabled = True : cmdTestDin07.Enabled = True
cmdTestDin08.Enabled = True : cmdTestDin09.Enabled = True : cmdTestDin10.Enabled = True : cmdTestDin11.Enabled = True : cmdTestDin12.Enabled = True : cmdTestDin13.Enabled = True
cmdTestDone.Enabled = True : cmbTestAddressSelect.Enabled = False
cmbArduinoAddressSelect.Enabled = False ' Disable Config Section
DisplayTestDataString() ' Updates the Test Section Display if Test Mode is Active - Also assigns a DataString to TestDataString
End Sub
Private Sub cmdTestDone_Click(sender As Object, e As EventArgs) Handles cmdTestDone.Click ' Exits the Manual Test Mode
DisableTestButtons() ' Disables the Test Section Buttons
TestModeActive = 0 ' Takes Program out of Test Mode
End Sub
Private Sub DisableTestButtons() ' Disables the buttons in the Test Section Group Box while Enabling the Config Section Combo Box
cmdTestDin02.Enabled = False : cmdTestDin03.Enabled = False : cmdTestDin04.Enabled = False : cmdTestDin05.Enabled = False : cmdTestDin06.Enabled = False : cmdTestDin07.Enabled = False
cmdTestDin08.Enabled = False : cmdTestDin09.Enabled = False : cmdTestDin10.Enabled = False : cmdTestDin11.Enabled = False : cmdTestDin12.Enabled = False : cmdTestDin13.Enabled = False
cmdTestDone.Enabled = False : cmbTestAddressSelect.Enabled = True
cmbArduinoAddressSelect.Enabled = True ' Enable Config Section
cmdTestDin02.ForeColor = Color.White : cmdTestDin03.ForeColor = Color.White : cmdTestDin04.ForeColor = Color.White : cmdTestDin05.ForeColor = Color.White : cmdTestDin06.ForeColor = Color.White : cmdTestDin07.ForeColor = Color.White
cmdTestDin08.ForeColor = Color.White : cmdTestDin09.ForeColor = Color.White : cmdTestDin10.ForeColor = Color.White : cmdTestDin11.ForeColor = Color.White : cmdTestDin12.ForeColor = Color.White : cmdTestDin13.ForeColor = Color.White
End Sub
Private Sub DisplayTestDataString() ' Updates the Test Section Display if Test Mode is Active
If TestArduinoAddress = 0 Then : TestDataString = DataString0 : DisplayTestDataStringAssist() : End If ' If Test Adruino X is Selected
If TestArduinoAddress = 1 Then : TestDataString = DataString1 : DisplayTestDataStringAssist() : End If ' Assign Data String X to Temp and send to DisplayTestDataStringAssist() to update Test Section Display
If TestArduinoAddress = 2 Then : TestDataString = DataString2 : DisplayTestDataStringAssist() : End If
If TestArduinoAddress = 3 Then : TestDataString = DataString3 : DisplayTestDataStringAssist() : End If
If TestArduinoAddress = 4 Then : TestDataString = DataString4 : DisplayTestDataStringAssist() : End If
If TestArduinoAddress = 5 Then : TestDataString = DataString5 : DisplayTestDataStringAssist() : End If
If TestArduinoAddress = 6 Then : TestDataString = DataString6 : DisplayTestDataStringAssist() : End If
If TestArduinoAddress = 7 Then : TestDataString = DataString7 : DisplayTestDataStringAssist() : End If
If TestArduinoAddress = 8 Then : TestDataString = DataString8 : DisplayTestDataStringAssist() : End If
If TestArduinoAddress = 9 Then : TestDataString = DataString9 : DisplayTestDataStringAssist() : End If
End Sub
Private Sub DisplayTestDataStringAssist() ' Update Test Section Display with information from Selected Station
cmdTestDin02.Text = TestDataString.Substring(2, 1) ' Write Data String Digit Value to appropriate Test Section Button
cmdTestDin03.Text = TestDataString.Substring(3, 1)
cmdTestDin04.Text = TestDataString.Substring(4, 1)
cmdTestDin05.Text = TestDataString.Substring(5, 1)
cmdTestDin06.Text = TestDataString.Substring(6, 1)
cmdTestDin07.Text = TestDataString.Substring(7, 1)
cmdTestDin08.Text = TestDataString.Substring(8, 1)
cmdTestDin09.Text = TestDataString.Substring(9, 1)
cmdTestDin10.Text = TestDataString.Substring(10, 1)
cmdTestDin11.Text = TestDataString.Substring(11, 1)
cmdTestDin12.Text = TestDataString.Substring(12, 1)
cmdTestDin13.Text = TestDataString.Substring(13, 1)
End Sub
Private Sub cmdTestDin02_Click(sender As Object, e As EventArgs) Handles cmdTestDin02.Click ' Manually change the value of the Ouput of the Selected Station
If TestConfigString.Substring(2, 1) = "2" Then ' If Digit is configured as an Output...
If TestDataString.Substring(2, 1) = "0" Then : TestDataString = TestDataString.Remove(2, 1) : TestDataString = TestDataString.Insert(2, 1) : Else ' Toggle the Value of the output Digit and launch UpdateTestDataString() to update Primary Data String
TestDataString = TestDataString.Remove(2, 1) : TestDataString = TestDataString.Insert(2, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin03_Click(sender As Object, e As EventArgs) Handles cmdTestDin03.Click
If TestConfigString.Substring(3, 1) = "2" Then
If TestDataString.Substring(3, 1) = "0" Then : TestDataString = TestDataString.Remove(3, 1) : TestDataString = TestDataString.Insert(3, 1) : Else
TestDataString = TestDataString.Remove(3, 1) : TestDataString = TestDataString.Insert(3, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin04_Click(sender As Object, e As EventArgs) Handles cmdTestDin04.Click
If TestConfigString.Substring(4, 1) = "2" Then
If TestDataString.Substring(4, 1) = "0" Then : TestDataString = TestDataString.Remove(4, 1) : TestDataString = TestDataString.Insert(4, 1) : Else
TestDataString = TestDataString.Remove(4, 1) : TestDataString = TestDataString.Insert(4, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin05_Click(sender As Object, e As EventArgs) Handles cmdTestDin05.Click
If TestConfigString.Substring(5, 1) = "2" Then
If TestDataString.Substring(5, 1) = "0" Then : TestDataString = TestDataString.Remove(5, 1) : TestDataString = TestDataString.Insert(5, 1) : Else
TestDataString = TestDataString.Remove(5, 1) : TestDataString = TestDataString.Insert(5, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin06_Click(sender As Object, e As EventArgs) Handles cmdTestDin06.Click
If TestConfigString.Substring(6, 1) = "2" Then
If TestDataString.Substring(6, 1) = "0" Then : TestDataString = TestDataString.Remove(6, 1) : TestDataString = TestDataString.Insert(6, 1) : Else
TestDataString = TestDataString.Remove(6, 1) : TestDataString = TestDataString.Insert(6, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin07_Click(sender As Object, e As EventArgs) Handles cmdTestDin07.Click
If TestConfigString.Substring(7, 1) = "2" Then
If TestDataString.Substring(7, 1) = "0" Then : TestDataString = TestDataString.Remove(7, 1) : TestDataString = TestDataString.Insert(7, 1) : Else
TestDataString = TestDataString.Remove(7, 1) : TestDataString = TestDataString.Insert(7, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin08_Click(sender As Object, e As EventArgs) Handles cmdTestDin08.Click
If TestConfigString.Substring(8, 1) = "2" Then
If TestDataString.Substring(8, 1) = "0" Then : TestDataString = TestDataString.Remove(8, 1) : TestDataString = TestDataString.Insert(8, 1) : Else
TestDataString = TestDataString.Remove(8, 1) : TestDataString = TestDataString.Insert(8, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin09_Click(sender As Object, e As EventArgs) Handles cmdTestDin09.Click
If TestConfigString.Substring(9, 1) = "2" Then
If TestDataString.Substring(9, 1) = "0" Then : TestDataString = TestDataString.Remove(9, 1) : TestDataString = TestDataString.Insert(9, 1) : Else
TestDataString = TestDataString.Remove(9, 1) : TestDataString = TestDataString.Insert(9, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin10_Click(sender As Object, e As EventArgs) Handles cmdTestDin10.Click
If TestConfigString.Substring(10, 1) = "2" Then
If TestDataString.Substring(10, 1) = "0" Then : TestDataString = TestDataString.Remove(10, 1) : TestDataString = TestDataString.Insert(10, 1) : Else
TestDataString = TestDataString.Remove(10, 1) : TestDataString = TestDataString.Insert(10, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin11_Click(sender As Object, e As EventArgs) Handles cmdTestDin11.Click
If TestConfigString.Substring(11, 1) = "2" Then
If TestDataString.Substring(11, 1) = "0" Then : TestDataString = TestDataString.Remove(11, 1) : TestDataString = TestDataString.Insert(11, 1) : Else
TestDataString = TestDataString.Remove(11, 1) : TestDataString = TestDataString.Insert(11, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin12_Click(sender As Object, e As EventArgs) Handles cmdTestDin12.Click
If TestConfigString.Substring(12, 1) = "2" Then
If TestDataString.Substring(12, 1) = "0" Then : TestDataString = TestDataString.Remove(12, 1) : TestDataString = TestDataString.Insert(12, 1) : Else
TestDataString = TestDataString.Remove(12, 1) : TestDataString = TestDataString.Insert(12, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub cmdTestDin13_Click(sender As Object, e As EventArgs) Handles cmdTestDin13.Click
If TestConfigString.Substring(13, 1) = "2" Then
If TestDataString.Substring(13, 1) = "0" Then : TestDataString = TestDataString.Remove(13, 1) : TestDataString = TestDataString.Insert(13, 1) : Else
TestDataString = TestDataString.Remove(13, 1) : TestDataString = TestDataString.Insert(13, 0) : End If : UpdateTestDataString() : End If
End Sub
Private Sub UpdateTestDataString() ' Update Primary Data String for Station if changes are made manually in the Test Section
If TestArduinoAddress = 0 Then : DataString0 = TestDataString : End If ' If the Selected Station to be tested is X...
If TestArduinoAddress = 1 Then : DataString1 = TestDataString : End If ' Update Data String X
If TestArduinoAddress = 2 Then : DataString2 = TestDataString : End If
If TestArduinoAddress = 3 Then : DataString3 = TestDataString : End If
If TestArduinoAddress = 4 Then : DataString4 = TestDataString : End If
If TestArduinoAddress = 5 Then : DataString5 = TestDataString : End If
If TestArduinoAddress = 6 Then : DataString6 = TestDataString : End If
If TestArduinoAddress = 7 Then : DataString7 = TestDataString : End If
If TestArduinoAddress = 8 Then : DataString8 = TestDataString : End If
If TestArduinoAddress = 9 Then : DataString9 = TestDataString : End If
End Sub
Private Sub cmdExit_Click(sender As Object, e As EventArgs) Handles cmdExit.Click
End
End Sub
#End Region
End Class