Snippet of Code from iBdoor v1.40 (C) 2002 Simon Atkin.

Home > 1-Wire Projects > Door Access Controller > Code

Private Sub TMTimer_Timer()
'// declarations
Dim search, flag, i, j As Integer, Doloop As Integer
ReDim ROM(8) As Integer
Dim romstr As String

'// start a session
'sHandle = TMExtendedStartSession(portnum, porttype, vbNullString)
iBfunc.startsession

'// if SHandle valid then search for a device
If (sHandle > 0) Then
'// check to see if setup needs to be done
If (Not SetupDone) Then
flag = TMSetup(sHandle)
SetupDone = True
End If

AlarmDeviceCount = 0
Doloop = 1
stime = GetTickCount()

'// look for the 'next 1F (DS2409)' device on the 1-Wire
flag = TMFamilySearchSetup(sHandle, state_buffer(0), &H1F)

Do
Doloop = TMNextAlarm(sHandle, state_buffer(0))
If Doloop = 1 Then
'// device found, so get its ROM code
ROM(0) = 0
flag = TMRom(sHandle, state_buffer(0), ROM(0))

'// create a string out of ROM number
romstr = ""
For i = 7 To 0 Step -1
If (ROM(i) <= &HF) Then romstr = romstr + "0"
romstr = romstr + Hex$(ROM(i))
Next i

AlarmDeviceCount = AlarmDeviceCount + 1

ReDim Preserve devices(AlarmDeviceCount + 1)
devices(AlarmDeviceCount) = romstr

presenceCount = presenceCount + 1
Form1.Label2.Caption = presenceCount

If Form1.debug.Value Then debuglog ("TMNextAlarm " & romstr)

End If

If GetTickCount() > stime + 500 Then Exit Do

Loop While Doloop = 1

End If

For i = 1 To AlarmDeviceCount
If devices(i) = "EA000000013A7C1F" Then DoorCheck1 = 1 'door1
If devices(i) = "CC000000013A601F" Then DoorCheck2 = 1 'door2
Next i

'//if DoorCheck flags have been set check 2409 main ports

If DoorCheck1 = 1 Then

flag = iBfunc.setcoupler("EA000000013A7C1F", 3) 'smart on main

KeyDeviceCount = 0

Doloop = 1
stime = GetTickCount()

Do
Doloop = TMNext(sHandle, state_buffer(0))
If Doloop = 1 Then
'// device found, so get its ROM code
ROM(0) = 0
flag = TMRom(sHandle, state_buffer(0), ROM(0))

'// create a string out of ROM number
romstr = ""
For i = 7 To 0 Step -1
If (ROM(i) <= &HF) Then romstr = romstr + "0"
romstr = romstr + Hex$(ROM(i))
Next i

KeyDeviceCount = KeyDeviceCount + 1

ReDim Preserve FoundKeys(KeyDeviceCount + 1)
FoundKeys(KeyDeviceCount) = romstr

presenceCount2 = presenceCount2 + 1
Form1.Label3.Caption = presenceCount2

If Form1.debug.Value Then debuglog ("TMNextD1 " & romstr)

End If

If GetTickCount() > stime + 500 Then Exit Do

Loop While Doloop = 1


'Form1.Text4.Text = KeyDeviceCount
'For i = 1 To KeyDeviceCount
'Text1 = KeyDevices(i) + Chr(13) + Chr(10) + Text1
'Next i

flag = iBfunc.setcoupler("EA000000013A7C1F", 0) 'all-lines off

'Form1.Command1.BackColor = &H8000000F
DoorCheck1 = 0

'//search keys array to find 2401 to confirm segment was working O.K

search = 0

For i = 1 To KeyDeviceCount
If FoundKeys(i) = "4400000786868201" Then search = search And 1
If FoundKeys(i) = "1600000786874201" Then search = search And 2
Next i

If search <> 0 Then
Open AppDir + "\security.log" For Append As #1
If Not search And 1 Then
Print #1, "DEBUG: Door 1 Segment Error Reading DS2401 4400000786868201 Near End "; Date; Time
errorCount = errorCount + 1
Form1.Label4.Caption = errorCount
End If
If Not search And 2 Then
Print #1, "DEBUG: Door 1 Segment Error Reading DS2401 1600000786874201 Far End "; Date; Time
errorCount = errorCount + 1
Form1.Label4.Caption = errorCount
End If
Close #1
End If

'// if 2401 is found then continue

'(A)If search = 1 Then

'//remove excluded keys from found keys array

'//devices to exclude as keys

'CC000000013A601F 2409 Coupler
'EA000000013A7C1F 2409 Coupler
'4400000786868201 2401 Segment A Near End (Door1) on Main of EA
'1600000786874201 2401 Segment A Far End (Door1) on Main of EA
'C90000078686C901 2401 Segment B (Door2) on Main of CC
'5600000786860901 2401 Segment C (Unused) on Aux of CC
'C500000017E21B12 2406 Switch
'27000001B8C8EB09 2502 9097U Adapter
'52000001B9232109 2502 9097U Adapter

'//arrays

'FoundKeys() is the array of devices found by TMNext
'KeyDevices() is array of devices without excluded keys (trimmed)

j = KeyDeviceCount
KeyDeviceCount = 0

For i = 1 To j

search = 0

If FoundKeys(i) = "CC000000013A601F" Then search = 1
If FoundKeys(i) = "EA000000013A7C1F" Then search = 1
If FoundKeys(i) = "4400000786868201" Then search = 1
If FoundKeys(i) = "1600000786874201" Then search = 1
If FoundKeys(i) = "C90000078686C901" Then search = 1
If FoundKeys(i) = "5600000786860901" Then search = 1
If FoundKeys(i) = "C500000017E21B12" Then search = 1
If FoundKeys(i) = "27000001B8C8EB09" Then search = 1
If FoundKeys(i) = "52000001B9232109" Then search = 1

If search = 0 Then
KeyDeviceCount = KeyDeviceCount + 1
ReDim Preserve KeyDevices(KeyDeviceCount)
KeyDevices(KeyDeviceCount) = FoundKeys(i)
End If

Next i

'//match found devices against key list

If KeyDeviceCount >= 1 Then
For i = 1 To KeyDeviceCount
search = 0
For j = 1 To KeyUserCount
If KeyRomId(j) = KeyDevices(i) And KeySecLevel(j) And 1 Then

search = 1

'// check size of textbox control
If Len(Text1) > 5000 Then Text1 = ""

'// Log Access
Text2.Text = KeyUserName(j)
Text3 = Str(Val(Text3) + 1)
Text1 = "Door 1 " + KeyUserName(j) + " " + Str(Date) + " " + Str(Time) + Chr(13) + Chr(10) + Text1

Open AppDir + "\security.log" For Append As #1
Print #1, "Door 1 Access "; KeyUserName(j); " "; Date; Time
Close #1

Open AppDir + "\door1.log" For Append As #1
Print #1, "Door 1 Access", KeyUserName(j), Date, Time
Close #1

If Form1.debug.Value Then debuglog ("D1 Relay Activated")

'//activate door relay
flag = iBfunc.setcoupler("EA000000013A7C1F", 7) 'set ctrl o/p to aux
flag = iBfunc.setcoupler("EA000000013A7C1F", 4) 'smart on aux
flag = iBfunc.setswitch("C500000017E21B12", 1) 'active pio a
flag = iBfunc.setswitch("C500000017E21B12", 0) 'de-active pio a
flag = iBfunc.setcoupler("EA000000013A7C1F", 6) 'set ctrl o/p to main
flag = iBfunc.setcoupler("EA000000013A7C1F", 0) 'all-lines off

End If

Next j

'//report illegal key attempt

If search = 0 Then
Text1 = "Door 1 Unknown Key " + KeyDevices(i) + " " + Str(Date) + " " + Str(Time) + Chr(13) + Chr(10) + Text1
Text2.Text = "Unknown Key"

Open AppDir + "\security.log" For Append As #1
Print #1, "WARNING: Door 1 Unknown Key "; KeyDevices(i); " "; Date; Time
Close #1
End If

Next i

Else

'log false triggers
falseTriggers = falseTriggers + 1
Form1.Label5.Caption = falseTriggers

If Form1.debug.Value Then
For i = 1 To j
debuglog ("FalseTriggerKeysD1 " & FoundKeys(i))
Next i
End If

End If

'(A)End If

End If


If DoorCheck2 = 1 Then
'Form1.Command2.BackColor = &HFF

flag = iBfunc.setcoupler("CC000000013A601F", 3) 'smart on main

KeyDeviceCount = 0

Doloop = 1
stime = GetTickCount

Do
Doloop = TMNext(sHandle, state_buffer(0))
If Doloop = 1 Then
'// device found, so get its ROM code
ROM(0) = 0
flag = TMRom(sHandle, state_buffer(0), ROM(0))

'// create a string out of ROM number
romstr = ""
For i = 7 To 0 Step -1
If (ROM(i) <= &HF) Then romstr = romstr + "0"
romstr = romstr + Hex$(ROM(i))
Next i

KeyDeviceCount = KeyDeviceCount + 1

ReDim Preserve FoundKeys(KeyDeviceCount + 1)
FoundKeys(KeyDeviceCount) = romstr

presenceCount2 = presenceCount2 + 1
Form1.Label3.Caption = presenceCount2

If Form1.debug.Value Then debuglog ("TMNextD2 " & romstr)

End If

If GetTickCount() > stime + 500 Then Exit Do

Loop While Doloop = 1


'Form1.Text4.Text = KeyDeviceCount
'For i = 1 To KeyDeviceCount
'Text1 = Text1 + KeyDevices(i) + Chr(13) + Chr(10)
'Next i

flag = iBfunc.setcoupler("CC000000013A601F", 0) 'all-lines off

'Form1.Command2.BackColor = &H8000000F
DoorCheck2 = 0

'//search keys array to find 2401 to confirm segment was working O.K

search = 0

For i = 1 To KeyDeviceCount
If FoundKeys(i) = "C90000078686C901" Then search = 1
Next i

If search = 0 Then
Open AppDir + "\security.log" For Append As #1
Print #1, "DEBUG: Door 2 Segment Error Reading DS2401 C90000078686C901 "; Date; Time
errorCount = errorCount + 1
Form1.Label4.Caption = errorCount
Close #1
End If

'// if 2401 is found then continue

'(B)If search = 1 Then

'//remove excluded keys from found keys array

'//devices to exclude as keys

'CC000000013A601F 2409 Coupler
'EA000000013A7C1F 2409 Coupler
'4400000786868201 2401 Segment A Near End (Door1) on Main of EA
'1600000786874201 2401 Segment A Far End (Door1) on Main of EA
'C90000078686C901 2401 Segment B (Door2) on Main of CC
'5600000786860901 2401 Segment C (Unused) on Aux of CC
'C500000017E21B12 2406 Switch
'27000001B8C8EB09 2502 9097U Adapter
'52000001B9232109 2502 9097U Adapter

'//arrays

'FoundKeys() is the array of devices found by TMNext
'KeyDevices() is array of devices without excluded keys (trimmed)

j = KeyDeviceCount
KeyDeviceCount = 0

For i = 1 To j

search = 0

If FoundKeys(i) = "CC000000013A601F" Then search = 1
If FoundKeys(i) = "EA000000013A7C1F" Then search = 1
If FoundKeys(i) = "4400000786868201" Then search = 1
If FoundKeys(i) = "1600000786874201" Then search = 1
If FoundKeys(i) = "C90000078686C901" Then search = 1
If FoundKeys(i) = "5600000786860901" Then search = 1
If FoundKeys(i) = "C500000017E21B12" Then search = 1
If FoundKeys(i) = "27000001B8C8EB09" Then search = 1
If FoundKeys(i) = "52000001B9232109" Then search = 1

If search = 0 Then
KeyDeviceCount = KeyDeviceCount + 1
ReDim Preserve KeyDevices(KeyDeviceCount)
KeyDevices(KeyDeviceCount) = FoundKeys(i)
End If

Next i

'//match found devices against key list

If KeyDeviceCount >= 1 Then
For i = 1 To KeyDeviceCount
search = 0
For j = 1 To KeyUserCount
If KeyRomId(j) = KeyDevices(i) And KeySecLevel(j) And 2 Then

search = 1

'// check size of textbox control
If Len(Text1) > 5000 Then Text1 = ""

'// Log Access
Text4.Text = KeyUserName(j)
Text5 = Str(Val(Text5) + 1)
Text1 = "Door 2 " + KeyUserName(j) + " " + Str(Date) + " " + Str(Time) + Chr(13) + Chr(10) + Text1

Open AppDir + "\security.log" For Append As #1
Print #1, "Door 2 Access "; KeyUserName(j); " "; Date; Time
Close #1

Open AppDir + "\door2.log" For Append As #1
Print #1, "Door 2 Access", KeyUserName(j), Date, Time
Close #1

If Form1.debug.Value Then debuglog ("D2 Relay Activated")

'//activate door relay
flag = iBfunc.setcoupler("EA000000013A7C1F", 7) 'set ctrl o/p to aux
flag = iBfunc.setcoupler("EA000000013A7C1F", 4) 'smart on aux
flag = iBfunc.setswitch("C500000017E21B12", 2) 'active pio b
flag = iBfunc.setswitch("C500000017E21B12", 0) 'de-active pio b
flag = iBfunc.setcoupler("EA000000013A7C1F", 6) 'set ctrl o/p to main
flag = iBfunc.setcoupler("EA000000013A7C1F", 0) 'all-lines off

End If

Next j

'//report illegal key attempt

If search = 0 Then
Text1 = "Door 2 Unknown Key " + KeyDevices(i) + " " + Str(Date) + " " + Str(Time) + Chr(13) + Chr(10) + Text1
Text4.Text = "Unknown Key"

Open AppDir + "\security.log" For Append As #1
Print #1, "WARNING: Door 2 Unknown Key "; KeyDevices(i); " "; Date; Time
Close #1
End If

Next i

Else

'log false triggers
falseTriggers = falseTriggers + 1
Form1.Label5.Caption = falseTriggers

If Form1.debug.Value Then
For i = 1 To j
debuglog ("FalseTriggerKeysD2 " & FoundKeys(i))
Next i
End If

End If

'(B)End If

End If

'// end the current session
flag = TMEndSession(sHandle)

End Sub