NCDLib – Random Code for Controlling NCD Devices

Sometimes I like to write code to simplify my life, particularly when I need to call the same routines over and over.  Over the years, I have pieced a few code elements together to help make the NCD Component Library easier for me to use.  Some of this code is old, some is new, some is tested heavily, and some of it is random ideas I have had over the years.  A recent post on the forum prompted me to release the code, as I have been meaning to do this for a long time anyway.  This code represents a new class for Visual Studio, and I frequently call many of the routines in this class.  I would really like to point out two class items that have been added, as they greatly simplify communications to the I2C port on Fusion, BridgeX5, and other USART to I2C converters we manufacture (some of which are very small).  The I2C code class works with all of them, so have fun and hopefully you find something of use.  Please note a working implementation of this library is ALWAYS included with Base Station source code, which will soon be posted on GitHub.

Using this Library, the following function calls are now possible, making I2C communications much more like Python or Arduino controllers:

NCDLib.I2C.Write

NCDLib.I2C.Read

The above function will wrap up your RAW I2C Address and variables (as found in the datasheet of your I2C chip) and communicate to the I2C device via serial communications.  I hope to add direct support for many common chips over the next few years, but for now, here is a sample of the commands working with a MCP3428:

NCDLib.I2C.Chip.MCP3428.Setup_Channel(NCDComponent1, 0, GetAddressInteger(ADDR_A.SelectedItem), 1, Resolution, Gain)

Complete source code for these functions are shown below.  I would advise picking out what you need, as there are many functions below that are completely irrelevant and random (as they are used by Base Station).

  1. Imports System.Reflection
  2. Imports System.IO
  3. Imports System.Windows.Forms
  4. Imports NCD
  5.  
  6. Public Class NCDLib
  7.  
  8. Public Class Wireless
  9.  
  10.  
  11. Public Class S3B_API
  12.  
  13. Public Class ZigBeeData
  14. Public Addr As Array ' Address in data frame
  15. Public Data As Array ' Data in data frame
  16. Public Valid As Boolean ' if the data is valid
  17. End Class
  18. Public Shared Sub Send(ByRef _ncd As NCD.NCDComponent, ByVal Address As Array, ByVal TXData As Array)
  19. 'This function is used to format and send API data for S3B Modules, making communications with remote devices much easier
  20.  
  21. Dim API As New ArrayList
  22. Dim DataLength As Integer
  23. Dim MSB_Length As Byte
  24. Dim LSB_Length As Byte
  25. Dim CkSum As Integer
  26. Dim Temporary As Integer
  27.  
  28. DataLength = TXData.Length + 14 'Calculate the Length of the Array
  29. MSB_Length = DataLength >> 8
  30. LSB_Length = DataLength And 255
  31.  
  32. Debug.Print(MSB_Length.ToString)
  33. Debug.Print(LSB_Length.ToString)
  34.  
  35.  
  36. API.Add(126) '0,Header 0x7e
  37. API.Add(MSB_Length) '1,Length of Packet MSB
  38. API.Add(LSB_Length) '2,Length of Packet LSB
  39. API.Add(16) '3,Reserved
  40. API.Add(0) '4,Reserved no response come back to make read eaiser
  41. API.AddRange(Address)
  42. API.Add(255) ' 0xfffe
  43. API.Add(254) ' 0xfffe
  44. API.Add(0) ' 15 set max hops, 0 for maxium
  45. API.Add(0) ' 16, options 0, for disable ack
  46. API.AddRange(TXData)
  47.  
  48. 'Calculate Checksum
  49. CkSum = 0
  50. For Temporary = 3 To API.Count - 1
  51. CkSum = CkSum + API(Temporary)
  52. Next
  53. CkSum = 255 - CkSum Mod 256
  54.  
  55. API.Add(CkSum)
  56. Debug.Print(CkSum)
  57.  
  58. 'Send API Frame to Device
  59. 'im d As Byte() = CType(API.ToArray(GetType(Byte)), Byte())
  60. Dim d(API.Count - 1) As Byte
  61. For i = 0 To API.Count - 1
  62. d(i) = API(i)
  63. Next
  64. NCDLib.WriteBytes(_ncd, 0, d)
  65.  
  66.  
  67. End Sub
  68.  
  69. Public Shared Function Receive(ByRef _ncd As NCD.NCDComponent) As ZigBeeData
  70. Dim rec As New ZigBeeData
  71. rec.Valid = False
  72. 'This function is used to receive and decode API data from S3B Modules, this function will return Address and Data to the caller
  73. Dim r As Integer
  74. r = _ncd.ReadByte()
  75. If (r <> 126) Then
  76. Return rec
  77. End If
  78. Dim msb, lsb As Integer
  79. msb = _ncd.ReadByte
  80. lsb = _ncd.ReadByte
  81. If (msb = -1) Or (lsb = -1) Then
  82. Return rec
  83. End If
  84.  
  85. 'read length
  86. Dim nLen As Integer = msb * 256 + lsb
  87. Dim frameType As Integer
  88. frameType = _ncd.ReadByte
  89. If (frameType <> &H90) Then
  90. Return rec
  91. End If
  92. ' read 8 bytes address
  93. Dim addr As New ArrayList
  94. For i = 1 To 8
  95. Dim na As Integer
  96. na = _ncd.ReadByte()
  97. If (na <> -1) Then
  98. addr.Add(na)
  99. Else
  100. Return rec
  101. End If
  102. Next
  103. ' read 0xfffe
  104. Dim reserved As Integer
  105. reserved = _ncd.ReadByte()
  106. If (reserved = -1) Then
  107. Return rec
  108. End If
  109. reserved = _ncd.ReadByte()
  110. If (reserved = -1) Then
  111. Return rec
  112. End If
  113. Dim options As Integer
  114.  
  115. ' read options
  116. options = _ncd.ReadByte()
  117. If (options = -1) Then
  118. Return rec
  119. End If
  120. Dim recData As New ArrayList
  121. For i = 1 To nLen - 12
  122. Dim rd As Integer = _ncd.ReadByte
  123. If (rd <> -1) Then
  124. recData.Add(rd)
  125. Else
  126. Return rec
  127. End If
  128. Next
  129. Dim checksum As Integer = _ncd.ReadByte()
  130. If (checksum = -1) Then
  131. Return rec
  132. End If
  133. Dim addr1(8 - 1) As Byte
  134. For i = 0 To 7
  135. addr1(i) = addr(i)
  136. Next
  137. Dim data1(recData.Count - 1) As Byte
  138. For i = 0 To recData.Count - 1
  139. data1(i) = recData(i)
  140. Next
  141. rec.Addr = addr1
  142. rec.Data = data1
  143. rec.Valid = True
  144. Return rec
  145. End Function
  146. End Class
  147.  
  148. End Class
  149.  
  150.  
  151. Public Class Safe
  152.  
  153.  
  154. Public Shared Function FusionFirmwareVersionCheck(ByVal RequiredFirmwareVersionLarge As Integer, ByVal RequiredFirmwareVersionSmall As Integer, ByVal CurrentFirmwareVersionLarge As Integer, ByVal CurrentFirmwareVersionSmall As Integer, DialogBox As Boolean)
  155. 'The purpose of this function is to check a require firmware version against a current firmware version
  156. 'If it's safe, this function returns True, otherwise, it returns false
  157.  
  158. 'Default this Function to Fail, Make it PROVE True
  159. FusionFirmwareVersionCheck = False
  160.  
  161. Dim RequiredWord As Integer = 0
  162. Dim CurrentWord As Integer = 0
  163.  
  164. RequiredWord = (RequiredFirmwareVersionLarge * 256) + RequiredFirmwareVersionSmall
  165. CurrentWord = (CurrentFirmwareVersionLarge * 256) + CurrentFirmwareVersionSmall
  166.  
  167. If CurrentWord >= RequiredWord Then
  168. FusionFirmwareVersionCheck = True
  169. Else
  170. If DialogBox = True Then
  171. MsgBox("Fusion Firmware Version 4." + Str(RequiredFirmwareVersionLarge) + "." + Str(RequiredFirmwareVersionSmall) + " or Later is Required to Set Counter Events, Please Contact NCD Support to Arrange for a Firmware Upgrade. Your Fusion Controller is Using Firmware Version 4." + Str(CurrentFirmwareVersionLarge) + "." + Str(CurrentFirmwareVersionSmall) + ".")
  172. End If
  173. End If
  174.  
  175.  
  176. Return FusionFirmwareVersionCheck
  177.  
  178. End Function
  179.  
  180. Public Shared Function Limit_8BIT(ByVal Value As Integer)
  181. If Value > 255 Then Value = 255
  182. If Value < 0 Then Value = 0
  183. Limit_8BIT = Value
  184. End Function
  185. Public Shared Function Limit_10BIT(ByVal Value As Integer)
  186. If Value > 1023 Then Value = 1023
  187. If Value < 0 Then Value = 0
  188. Limit_10BIT = Value
  189. End Function
  190. Public Shared Function Limit_12BIT(ByVal Value As Integer)
  191. If Value > 4095 Then Value = 4095
  192. If Value < 0 Then Value = 0
  193. Limit_12BIT = Value
  194. End Function
  195. Public Shared Function Limit_16BIT(ByVal Value As Integer)
  196. If Value > 65535 Then Value = 65535
  197. If Value < 0 Then Value = 0
  198. Limit_16BIT = Value
  199. End Function
  200.  
  201. Public Shared Function Limit_Value(ByVal Value As Integer, LowerLimit As Integer, UpperLimit As Integer)
  202.  
  203.  
  204. If Value > UpperLimit Then Value = UpperLimit
  205. If Value < LowerLimit Then Value = LowerLimit
  206. Limit_Value = Value
  207. End Function
  208. End Class
  209. Public Class Fusion
  210.  
  211.  
  212. Public Const CON_BlockCount = 64
  213. Public Const CON_AllConfiguration = 0
  214. Public Const CON_DeviceConfiguration = 1
  215. Public Const CON_ProxrRelayMap = 2
  216. Public Const CON_Taralist = 3
  217. Public Const CON_ReactorStandard = 4
  218. Public Const CON_ReactorTLEE = 5
  219. Public Const CON_RemoteAccess = 6
  220. Public Const MAX_DATA_SIZE = &H8100 ' max size of data block is 16K
  221.  
  222.  
  223.  
  224.  
  225. Public Shared Sub SaveTLEEBlock(ByRef _ncd As NCD.NCDComponent, _BlockID As Integer, F As Array)
  226.  
  227. Debug.Print("SaveTLEEBlock: " & _BlockID.ToString)
  228. Dim ckSum As Integer = 0
  229.  
  230. 'Filter F Array
  231. For temp As Integer = 0 To 15
  232. If F(temp) < 0 Then F(temp) = 0
  233. If F(temp) > 255 Then F(temp) = 255
  234. Next
  235.  
  236. 'Compute Checksum
  237. ckSum = 0
  238. For temp = 0 To 15
  239. ckSum = ckSum + F(temp)
  240. Next temp
  241. ckSum = (ckSum And 255)
  242.  
  243. NCDLib.WriteBytesAPI(_ncd, True, 0, 4, NCDLib.Math.GetLSB(_BlockID), NCDLib.Math.GetMSB(_BlockID), F(0), F(1), F(2), F(3), F(4), F(5), F(6), F(7), F(8), F(9), F(10), F(11), F(12), F(13), F(14), F(15), ckSum)
  244.  
  245. Debug.Print("END SaveTLEEBlock")
  246.  
  247. End Sub
  248. Public Shared Function LoadTLEEBlock(ByRef _ncd As NCD.NCDComponent, _BlockID As Integer)
  249.  
  250. Debug.Print("LoadTLEEBlock: " & _BlockID.ToString)
  251. Dim ckSum As Integer = 0
  252. Dim F As Array
  253.  
  254. NCDLib.WriteBytesAPI(_ncd, True, 0, 3, NCDLib.Math.GetLSB(_BlockID), NCDLib.Math.GetMSB(_BlockID))
  255. F = NCDLib.ReadBytesAPI(_ncd, True, 17)
  256.  
  257. If (Not (F Is Nothing)) Then
  258. If (F.Length < 16) Then
  259. Debug.Print(F.Length)
  260. End If
  261. For Temp As Integer = 0 To 15
  262. ckSum = ckSum + F(Temp)
  263. Next
  264.  
  265. ckSum = (ckSum And 255)
  266.  
  267. If ckSum = F(16) Then
  268. Debug.Print("Block Loaded")
  269. Return F
  270. Else
  271. Debug.Print("--------------> CKSUM ERROR: " & F(16) & " " & ckSum)
  272. End If
  273. End If
  274.  
  275. For Temp As Integer = 0 To 15
  276. F(Temp) = 255 'Return FF to User for Code Handling
  277. Next
  278. Debug.Print("ERROR: LoadTLEEBlock")
  279. Return F
  280.  
  281. End Function
  282.  
  283. End Class
  284. Public Class ReactorV2
  285.  
  286. Public Shared Function Translate(Method As String, Dat As Integer, LongDescription As Boolean) 'As String
  287.  
  288. Try
  289.  
  290. ' (Distance3.Value * 0.25) / 12) : CM3.Text = String.Format("{0:f1}", (Distance3.Value * 0.25) * 2.54)
  291. 'OK 8-Bit Analog Voltage
  292. 'OK 10-Bit Analog Voltage
  293. 'OK 12-Bit Analog Voltage
  294. 'OK 16-Bit Analog Voltage
  295. 'Distance in Inches
  296. 'Distance in Feet
  297. 'Distance in Centimeters
  298. 'Distance in Meters
  299. 'Temperature in Celsius
  300. 'Temperature in Fahrenheit
  301. 'Current in Amps/ma
  302.  
  303. Debug.Print("Translate")
  304. Debug.Print(Method)
  305. Debug.Print(Dat)
  306.  
  307.  
  308. Select Case Method
  309.  
  310. Case "8-Bit Analog Voltage"
  311. Dim Voltage As Integer = 5
  312. Dim Level As Integer = Dat
  313. Dim Result As String
  314. If LongDescription = False Then
  315. Result = String.Format("{0:f2} V", (Voltage / 256) * Level)
  316. Else
  317. Result = String.Format("{0:f2} Volts. No Not Exceed +5VDC On ANY Analog Input.", (Voltage / 256) * Level)
  318. End If
  319. Return Result
  320.  
  321. Case "10-Bit Analog Voltage"
  322. Dim Voltage As Integer = 5
  323. Dim Level As Integer = Dat
  324. Dim Result As String
  325. If LongDescription = False Then
  326. Result = String.Format("{0:f2} V", (Voltage / 1024) * Level)
  327. Else
  328. Result = String.Format("{0:f2} Volts. No Not Exceed +5VDC On ANY Analog Input.", (Voltage / 1024) * Level)
  329. End If
  330. Return Result
  331.  
  332. Case "12-Bit Analog Voltage"
  333. Dim Voltage As Integer = 5
  334. Dim Level As Integer = Dat
  335. Dim Result As String
  336. If LongDescription = False Then
  337. Result = String.Format("{0:f2} V", (Voltage / 4096) * Level)
  338. Else
  339. Result = String.Format("{0:f2} Volts. No Not Exceed +5VDC On ANY Analog Input.", (Voltage / 4096) * Level)
  340. End If
  341.  
  342. Return Result
  343.  
  344. Case "16-Bit Analog Voltage"
  345. Dim Voltage As Integer = 5
  346. Dim Level As Integer = Dat
  347. Dim Result As String
  348. If LongDescription = False Then
  349. Result = String.Format("{0:f2} V", (Voltage / 65536) * Level)
  350. Else
  351. Result = String.Format("{0:f2} Volts. No Not Exceed +5VDC On ANY Analog Input.", (Voltage / 65536) * Level)
  352. End If
  353. Return Result
  354.  
  355. Case "Distance in Inches"
  356. Dim Level As Integer = Dat
  357. Dim Result As String
  358. If LongDescription = False Then
  359. Result = String.Format("{0:f2} In", (0.25 * Level))
  360. Else
  361. Result = String.Format("{0:f2} Inches", (0.25 * Level))
  362. End If
  363. Return Result
  364.  
  365. Case "Distance in Feet"
  366. Dim Level As Integer = Dat
  367. Dim Result As String
  368. If LongDescription = False Then
  369. Result = String.Format("{0:f2} Ft", (0.25 * Level) / 12)
  370. Else
  371. Result = String.Format("{0:f2} Feet", (0.25 * Level) / 12)
  372. End If
  373. Return Result
  374.  
  375. Case "Distance in Centimeters"
  376. Dim Level As Integer = Dat
  377. Dim Result As String
  378. If LongDescription = False Then
  379. Result = String.Format("{0:f2} cm", (0.25 * Level) * 2.54)
  380. Else
  381. Result = String.Format("{0:f2} Centimeters", (0.25 * Level) * 2.54)
  382. End If
  383. Return Result
  384.  
  385. Case "Distance in Meters"
  386. Dim Level As Integer = Dat
  387. Dim Result As String
  388. If LongDescription = False Then
  389. Result = String.Format("{0:f2} M", ((0.25 * Level) * 2.54) / 10)
  390. Else
  391. Result = String.Format("{0:f2} Meters", ((0.25 * Level) * 2.54) / 10)
  392. End If
  393. Return Result
  394.  
  395. Case "Current in Amps/ma ACS712ELCTR-05B 185mv per Amp"
  396. Dim Sensativity As Double = 0.185
  397. 'If Translator.SelectedItem = "ACS712ELCTR-05B 185mv per Amp" Then Sensativity = 0.185
  398. 'If Translator.SelectedItem = "ACS712ELCTR-20A 100mv per Amp" Then Sensativity = 0.1
  399. 'If Translator.SelectedItem = "ACS712ELCTR-30A 66mv per Amp" Then Sensativity = 0.066
  400. Dim BitVolts As Double = 5 / 256 'BitVolts = 5 Volts Divided by 256 (8-Bit has 256 Posibilities)
  401. Dim CalcCurrent As Double = (Dat * BitVolts) / Sensativity
  402. Dim Result As String
  403. If LongDescription = False Then
  404. Result = String.Format("{0:f2} A", CalcCurrent)
  405. Else
  406. Result = String.Format("{0:f2} Amps. Amperage Values are Approximate.", CalcCurrent)
  407. End If
  408. Return Result
  409.  
  410. End Select
  411.  
  412.  
  413. Catch
  414.  
  415. Debug.Print("Unable to Translate")
  416.  
  417. End Try
  418.  
  419. Return 0
  420.  
  421. End Function
  422.  
  423.  
  424. Public Shared InterpreterListSize As Integer = 10
  425. Public Shared Function InterpreterList()
  426. Dim DropOptions(InterpreterListSize) As String
  427.  
  428. ' B(13) = 0 8-Bit Analog Voltage
  429. ' B(13) = 1 10-Bit Analog Voltage
  430. ' B(13) = 2 12-Bit Analog Voltage
  431. ' B(13) = 3 16-Bit Analog Voltage
  432. ' B(13) = 4 Distance in Inches
  433. ' B(13) = 5 Distance in Feet
  434. ' B(13) = 6 Distance in Centimeters
  435. ' B(13) = 7 Distance in Meters
  436. ' B(13) = 8 Temperature in Celsius
  437. ' B(13) = 9 Temperature in Fahrenheit
  438. ' B(13) = 10 Current in Amps/ma ACS712ELCTR-05B 185mv per Amp
  439.  
  440. DropOptions(0) = "8-Bit Analog Voltage"
  441. DropOptions(1) = "10-Bit Analog Voltage"
  442. DropOptions(2) = "12-Bit Analog Voltage"
  443. DropOptions(3) = "16-Bit Analog Voltage"
  444. DropOptions(4) = "Distance in Inches"
  445. DropOptions(5) = "Distance in Feet"
  446. DropOptions(6) = "Distance in Centimeters"
  447. DropOptions(7) = "Distance in Meters"
  448. DropOptions(8) = "Temperature in Celsius"
  449. DropOptions(9) = "Temperature in Fahrenheit"
  450. DropOptions(10) = "Current in Amps/ma ACS712ELCTR-05B 185mv per Amp"
  451. Return DropOptions
  452. End Function
  453.  
  454.  
  455. Public Class MasterEventList_4_0_0
  456. 'Versions BEFORE 4.0.3
  457. Public Shared EventListSize_Base As Integer = 231 'Contains Events that do not require Parameters
  458. Public Shared EventListSize_All As Integer = 240 'Contains All Events including those that require Parameters
  459.  
  460. 'Versions 4.0.3 and AFTER!
  461. 'Public Shared EventListSize_Base As Integer = 245 'Contains Events that do not require Parameters
  462. 'Public Shared EventListSize_All As Integer = 245 'Contains All Events including those that require Parameters
  463.  
  464. ''4.0.3 and Later Use these variables
  465. 'Public Shared EventListSize_MIN As Integer = 241
  466. 'Public Shared EventListSize_MAX As Integer = 245 '241-245 Do NOT Require Parameters
  467.  
  468. End Class
  469.  
  470. Public Class MasterEventList_4_0_3
  471. 'Versions BEFORE 4.0.3
  472. 'Public Shared EventListSize_Base As Integer = 231 'Contains Events that do not require Parameters
  473. 'Public Shared EventListSize_All As Integer = 240 'Contains All Events including those that require Parameters
  474.  
  475. 'Versions 4.0.3 and AFTER!
  476. Public Shared EventListSize_Base As Integer = 245 'Contains Events that do not require Parameters
  477. Public Shared EventListSize_All As Integer = 245 'Contains All Events including those that require Parameters
  478.  
  479. ''4.0.3 and Later Use these variables
  480. 'Public Shared EventListSize_MIN As Integer = 241
  481. 'Public Shared EventListSize_MAX As Integer = 245 '241-245 Do NOT Require Parameters
  482.  
  483.  
  484. End Class
  485.  
  486. Public Class MasterEventList_4_0_5
  487. 'Versions BEFORE 4.0.3
  488. 'Public Shared EventListSize_Base As Integer = 231 'Contains Events that do not require Parameters
  489. 'Public Shared EventListSize_All As Integer = 240 'Contains All Events including those that require Parameters
  490.  
  491. 'Versions 4.0.3 and AFTER!
  492. Public Shared EventListSize_Base As Integer = 249 'Contains Events that do not require Parameters
  493. Public Shared EventListSize_All As Integer = 249 'Contains All Events including those that require Parameters
  494.  
  495. ''4.0.3 and Later Use these variables
  496. 'Public Shared EventListSize_MIN As Integer = 241
  497. 'Public Shared EventListSize_MAX As Integer = 245 '241-245 Do NOT Require Parameters
  498.  
  499. End Class
  500.  
  501.  
  502.  
  503. End Class
  504.  
  505. Public Class Math
  506.  
  507. Public Shared Function GetMSB(ByVal V As UInt16) As Byte
  508. Dim Result As Byte = 0
  509. Result = V >> 8
  510. Return Result
  511. End Function
  512. Public Shared Function GetLSB(ByVal V As UInt16) As Byte
  513. Dim Result As Byte = 0
  514. Result = V And 255
  515. Return Result
  516. End Function
  517. ''' <summary>
  518. ''' Set bit of byte to specific value
  519. ''' </summary>
  520. ''' <param name="v">byte to set</param>
  521. ''' <param name="pos">position of bit, start from 0, </param>
  522. ''' <param name="bitValue">value of bit, 0 or 1</param>
  523. ''' <returns>byte with specific bit set</returns>
  524. ''' <remarks></remarks>
  525. Public Shared Function SetBitEnum(ByVal v As Byte, ByVal pos As Byte, ByVal bitValue As enumBitValue) As Byte
  526. If (pos > 7) Then
  527. Throw New ArgumentOutOfRangeException("Position", pos, "Position should range from 0 to 7")
  528. End If
  529.  
  530. Return SetBit(v, pos, CByte(bitValue))
  531. End Function
  532. Public Enum enumBitValue
  533. [ON]
  534. Off
  535. End Enum
  536. ''' <summary>
  537. ''' Get specfic bit of byte
  538. ''' </summary>
  539. ''' <param name="v">value of byte</param>
  540. ''' <param name="pos">position of byte</param>
  541. ''' <returns>0 or 1</returns>
  542. ''' <remarks></remarks>
  543. Public Shared Function GetBit(ByVal v As Byte, ByVal pos As Byte) As Byte
  544. If (pos > 7) Then
  545. Throw New ArgumentOutOfRangeException("Position", pos, "Position should range from 0 to 7")
  546. End If
  547.  
  548. Dim result As Byte = v
  549. ' set the bit to sepecific value
  550. ' bitValue << pos ' calculate the value of bit in byte
  551. ' v1 And v2 do an and operation
  552. Dim value As Byte = 1 << pos
  553. result = CByte(v And value)
  554. If (result > 0) Then
  555. result = 1
  556. End If
  557. Return result
  558. End Function
  559. Public Shared Function GetBit16(ByVal v As UInt16, ByVal pos As Byte) As Byte
  560. If (pos > 15) Then
  561. Throw New ArgumentOutOfRangeException("Position", pos, "Position should range from 0 to 15")
  562. End If
  563.  
  564. Dim result As UInt16 = v
  565. ' set the bit to sepecific value
  566. ' bitValue << pos ' calculate the value of bit in byte
  567. ' v1 And v2 do an and operation
  568. Dim value As UInt16 = 1 << pos
  569. result = CUInt(v And value)
  570. If (result > 0) Then
  571. result = 1
  572. End If
  573. Return result
  574. End Function
  575. ''' <summary>
  576. ''' Set bit of byte to specific value
  577. ''' </summary>
  578. ''' <param name="v">byte to set</param>
  579. ''' <param name="pos">position of bit, start from 0, </param>
  580. ''' <param name="bitValue">value of bit, 0 or 1</param>
  581. ''' <returns>byte with specific bit set</returns>
  582. ''' <remarks></remarks>
  583. Public Shared Function SetBit(ByRef v As Byte, ByVal pos As Byte, ByVal bitValue As Byte) As Byte
  584. If (pos > 7) Then
  585. Throw New ArgumentOutOfRangeException("Position", pos, "Position should range from 0 to 7")
  586. End If
  587.  
  588. If (bitValue > 1) Then
  589. Throw New ArgumentOutOfRangeException("Bit Value", bitValue, "Bit Value should range from 0 to 1")
  590. End If
  591.  
  592. Dim result As Byte = v
  593. ' set the bit to sepecific value
  594. ' bitValue << pos ' calculate the value of bit in byte
  595. ' v1 And v2 do an and operation
  596. Dim value As Byte = 1 << pos
  597. result = CByte(v Or value)
  598. result = CByte(result - value)
  599. ' we use CByte to conver the result to byte forcely
  600. ' calculate the value if a byte with that bit only set to the specfic value
  601. 'If Bit Value is 0, it is not modified, but if Bit Value is 1 then Bit is Turned On
  602. value = bitValue << pos
  603.  
  604. result = CByte(result + value)
  605. v = result
  606. Return result
  607. End Function
  608. Public Shared Function SetBit16(ByRef v As UInt16, ByVal pos As Byte, ByVal bitValue As Byte) As UInt16
  609. If (pos > 15) Then
  610. Throw New ArgumentOutOfRangeException("Position", pos, "Position should range from 0 to 7")
  611. End If
  612.  
  613. If (bitValue > 1) Then
  614. Throw New ArgumentOutOfRangeException("Bit Value", bitValue, "Bit Value should range from 0 to 1")
  615. End If
  616.  
  617. Dim result As UInt16 = v
  618. ' set the bit to sepecific value
  619. ' bitValue << pos ' calculate the value of bit in byte
  620. ' v1 And v2 do an and operation
  621. Dim value As UInt16 = 1 << pos
  622. result = CUInt(v Or value)
  623. result = CUInt(result - value)
  624. ' we use CByte to conver the result to byte forcely
  625. ' calculate the value if a byte with that bit only set to the specfic value
  626. 'If Bit Value is 0, it is not modified, but if Bit Value is 1 then Bit is Turned On
  627. ' to convert bitValue to CUint is very IMPORTANT, otherwise it will works like byte only
  628. value = CUInt(bitValue) << pos
  629.  
  630. result = CUInt(result + value)
  631. v = result
  632. Return result
  633. End Function
  634. Public Shared Function DecToBinaryString(ByVal V As Byte) As String
  635. Dim str As String = String.Empty
  636. Dim i As Integer = 0
  637. For i = 0 To 7
  638. If GetBit(V, i) = 0 Then
  639. str = str + "0"
  640. Else
  641. str = str + "1"
  642. End If
  643. Next
  644. Return str
  645. End Function
  646. Public Shared Function DecToBinaryStringStandard(ByVal V As Byte) As String
  647. Dim str As String = String.Empty
  648. Dim i As Integer = 0
  649. For i = 7 To 0 Step -1
  650. If GetBit(V, i) = 0 Then
  651. str = str + "0"
  652. Else
  653. str = str + "1"
  654. End If
  655. Next
  656. Return str
  657. End Function
  658. Public Shared Function BinaryStringToDec(ByVal V As String) As Byte
  659. Return System.Convert.ToByte(V, 2)
  660. End Function
  661. Public Shared Function DecToHexString(ByVal V As Byte) As String
  662. 'Return String.Format("{0:X2}", V)
  663. Return V.ToString("X2")
  664. End Function
  665.  
  666. Public Shared Function HexStringToDec(ByVal V As String) As Byte
  667. Return Byte.Parse(V, Globalization.NumberStyles.AllowHexSpecifier)
  668. End Function
  669.  
  670. ''' <summary>
  671. ''' Convert an byte to binary code decimal
  672. ''' </summary>
  673. ''' <param name="v"></param>
  674. ''' <returns></returns>
  675. ''' <remarks></remarks>
  676. Public Shared Function ConvertToBCD(ByVal v As Byte) As Byte
  677. Dim a As Byte = System.Math.Floor(v / 10)
  678. Dim b As Byte = v Mod 10
  679. Return a * 16 + b
  680. End Function
  681.  
  682. ''' <summary>
  683. ''' Convert an binary code decimal to decimal
  684. ''' </summary>
  685. ''' <param name="v"></param>
  686. ''' <returns></returns>
  687. ''' <remarks></remarks>
  688. Public Shared Function ConvertFromBCD(ByVal v As Byte) As Byte
  689. Dim a As Byte = System.Math.Floor(v / 16)
  690. Dim b As Byte = v Mod 16
  691. Return a * 10 + b
  692. End Function
  693.  
  694. End Class
  695.  
  696.  
  697. ''' <summary>
  698. ''' File IO class handle all File's IO operation
  699. ''' </summary>
  700. ''' <remarks></remarks>
  701. Public Class FileIO
  702. Public Shared Function SaveDialog()
  703.  
  704. End Function
  705. Public Shared Function LoadDialog()
  706.  
  707. End Function
  708. Public Shared Function SaveQuiet()
  709.  
  710. End Function
  711. Public Shared Function LoadQuiet()
  712.  
  713. End Function
  714. Public Shared Function CheckFileExistance()
  715.  
  716. End Function
  717. End Class
  718. Public Class DeviceInitialization
  719. Public Shared Function Discover_Baud_Rate(ByRef _ncd As NCDComponent) As Boolean
  720. '_ncd.SerialPort.StopBits = Ports.StopBits.Two
  721. Dim Result As Boolean = False
  722. Dim test As Integer = 0
  723. '_ncd.SerialPort.BaudRate = 115200
  724. test = Test_2WAY_RAW(_ncd)
  725. test = Test_2WAY_RAW(_ncd)
  726. If test = 85 Or test = 86 Then
  727. Result = True
  728. Return Result
  729. Exit Function
  730. End If
  731. _ncd.BaudRate = 57600
  732. test = Test_2WAY_RAW(_ncd)
  733. test = Test_2WAY_RAW(_ncd)
  734. If test = 85 Or test = 86 Then
  735. Result = True
  736. Return Result
  737. Exit Function
  738. End If
  739. _ncd.BaudRate = 9600
  740. test = Test_2WAY_RAW(_ncd)
  741. test = Test_2WAY_RAW(_ncd)
  742. If test = 85 Or test = 86 Then
  743. Result = True
  744. Return Result
  745. Exit Function
  746. End If
  747. End Function
  748. Public Shared Function Test_Compatibility(ByRef _ncd As NCDComponent) As Boolean
  749. '_ncd.SerialPort.StopBits = Ports.StopBits.Two
  750.  
  751. Dim Result As Boolean = False
  752. Dim test As Integer = 0
  753.  
  754. '_ncd.SerialPort.DiscardInBuffer() 'Clear Serial Port
  755. _ncd.Purge()
  756. System.Threading.Thread.Sleep(10)
  757. '_ncd.WriteByte(254)
  758. 'System.Threading.Thread.Sleep(1)
  759. '_ncd.WriteByte(246)
  760. NCDLib.WriteBytes(_ncd, 1, 254, 246)
  761. _ncd.SetTcpWritePace(1)
  762. _ncd.Flush()
  763. System.Threading.Thread.Sleep(10)
  764. '_ncd.WriteBytes(New Byte() {254, 246})
  765.  
  766. Dim IDA As Integer = _ncd.ReadByte()
  767. Dim IDB As Integer = _ncd.ReadByte()
  768. Dim IDC As Integer = _ncd.ReadByte()
  769. Dim IDD As Integer = _ncd.ReadByte()
  770.  
  771.  
  772. If IDA >= 64 And IDA <= 69 Then
  773. Result = True
  774. Else
  775. 'Result = True
  776. MsgBox("This software is not designed to work with the controller you are using.")
  777. End If
  778.  
  779. Return Result
  780. Exit Function
  781.  
  782. '_ncd.SerialPort.BaudRate = 115200
  783. 'test = Test_2WAY_RAW(_ncd)
  784. 'test = Test_2WAY_RAW(_ncd)
  785. 'If test = 85 Or test = 86 Then
  786. ' Result = True
  787. ' Return Result
  788. ' Exit Function
  789. 'End If
  790. '_ncd.SerialPort.BaudRate = 57600
  791. 'test = Test_2WAY_RAW(_ncd)
  792. 'test = Test_2WAY_RAW(_ncd)
  793. 'If test = 85 Or test = 86 Then
  794. ' Result = True
  795. ' Return Result
  796. ' Exit Function
  797. 'End If
  798. '_ncd.SerialPort.BaudRate = 9600
  799. 'test = Test_2WAY_RAW(_ncd)
  800. 'test = Test_2WAY_RAW(_ncd)
  801. 'If test = 85 Or test = 86 Then
  802. ' Result = True
  803. ' Return Result
  804. ' Exit Function
  805. 'End If
  806. End Function
  807. Public Shared Function Test_2WAY_RAW(ByRef _ncd As NCDComponent) As Integer
  808. '_ncd.SerialPort.ReadTimeout = 500
  809. '_ncd.SerialPort.DiscardInBuffer()
  810. _ncd.Purge()
  811. NCDLib.WriteBytes(_ncd, 1, 254, 33)
  812. _ncd.SetTcpWritePace(1)
  813. _ncd.Flush()
  814. Dim result As Integer = _ncd.ReadByte
  815. Return result
  816. End Function
  817. Public Shared Function Test_For_Configuration_Mode(ByRef _ncd As NCDComponent) As Boolean
  818.  
  819. Dim result As Boolean = False
  820. Dim retry As Integer = 0
  821. If (Not _ncd.IsOpen) Then
  822. Return False
  823. End If
  824. Restart:
  825. '_ncd.SerialPort.ReadTimeout = 1000
  826. '_ncd.SerialPort.DiscardInBuffer()
  827. _ncd.Purge()
  828. NCDLib.WriteBytes(_ncd, 1, 254, 33)
  829. _ncd.SetTcpWritePace(1)
  830. _ncd.Flush()
  831.  
  832. Dim result2 As Integer = _ncd.ReadByte
  833. Debug.Print("Result of Communications " + result2.ToString)
  834.  
  835. If result2 <> 85 Then
  836. If result2 <> 86 Then
  837. retry = retry + 1
  838. If retry < 4 Then
  839. GoTo Restart
  840. End If
  841. End If
  842. End If
  843.  
  844. If result2 = 85 Then
  845. MsgBox("Cannot Save. Controller is in Runtime Mode and Must be Set to Program Mode. Move PGM/RUN Jumpter to PGM positon and Try Again.")
  846. result = False
  847. Return result
  848. Else
  849. If result2 = 86 Then
  850. Debug.Print("Controller is ready for this program.")
  851. result = True
  852. Return result
  853. Else
  854. MsgBox("Unable to Communicate with Controller.")
  855. result = False
  856. Return result
  857. End If
  858. End If
  859.  
  860. End Function
  861. Public Shared Function SerialPortDialog()
  862.  
  863. End Function
  864. Public Shared Function ZigBee_S1Dialog()
  865.  
  866. End Function
  867. Public Shared Function ZB_S2Dialog()
  868.  
  869. End Function
  870. Public Shared Function XSC_Dialog()
  871.  
  872. End Function
  873. Public Shared Function DM_Dialog()
  874.  
  875. End Function
  876. End Class
  877. Public Class EEPROMIO
  878.  
  879. Const Filter As String = "NCD EEPROM FILES(*.ncd)|*.ncd|All Files(*.*)|(*.*)"
  880.  
  881. Const DefaultFilename As String = "EEPROM.ncd"
  882.  
  883. Public EEPROM As Byte() = New Byte(255) {}
  884.  
  885. Public Function SaveDialog() As Boolean
  886. Dim result As Boolean = False
  887. Dim SaveFileDialog1 As SaveFileDialog = New SaveFileDialog
  888. SaveFileDialog1.InitialDirectory = GetDefaultNCDFileFolder()
  889. SaveFileDialog1.Filter = Filter
  890. SaveFileDialog1.FileName = "EEPROM.NCD"
  891. If (SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
  892. SaveFile(SaveFileDialog1.FileName, EEPROM)
  893. result = True
  894. Else
  895. result = False
  896. End If
  897. Return result
  898. End Function
  899. Public Sub SaveFileQuite()
  900. Dim file As String = GetDefaultNCDFilePath()
  901. SaveFile(file, EEPROM)
  902. End Sub
  903. ''' <summary>
  904. ''' Save file to specific path
  905. ''' </summary>
  906. ''' <param name="filename"></param>
  907. ''' <param name="data"></param>
  908. ''' <remarks></remarks>
  909. Public Sub SaveFile(ByVal filename As String, ByVal data As Byte())
  910. Try
  911. Dim sw As BinaryWriter = New BinaryWriter(File.Open(filename, FileMode.Create))
  912. Dim version As Integer = 1
  913. Dim length As Integer = data.Length
  914. Dim i As Integer = 0
  915. sw.Write(version)
  916. sw.Write(length)
  917. sw.Write(data, 0, length)
  918. Dim chksum As Integer = GetCheckSum(data)
  919. sw.Write(chksum)
  920. sw.Flush()
  921. sw.Close()
  922. DumpEEPROM()
  923. Catch ex As Exception
  924. Throw New Exception("Write ncd file failure")
  925. End Try
  926. End Sub
  927. Public Sub LoadFileQuite()
  928. Dim filename As String = GetDefaultNCDFilePath()
  929. EEPROM = LoadFile(filename)
  930. End Sub
  931. ''' <summary>
  932. ''' Load file from specific path
  933. ''' </summary>
  934. ''' <param name="filename"></param>
  935. ''' <returns>bytes read from file</returns>
  936. ''' <remarks></remarks>
  937. ''' <exception cref="Exception">Corrupt NCD File</exception>
  938. Public Function LoadFile(ByVal filename As String) As Byte()
  939. Dim data As Byte() = Nothing
  940. Try
  941. Dim sr As BinaryReader = New BinaryReader(File.Open(filename, FileMode.Open))
  942. Dim version As Integer = sr.ReadInt32()
  943. Dim length As Integer = sr.ReadInt32()
  944. data = sr.ReadBytes(length)
  945. Dim checksum As Integer = sr.ReadInt32()
  946. If (checksum <> GetCheckSum(data)) Then
  947. Throw New Exception("Corrupt NCD File")
  948. End If
  949. sr.Close()
  950. Catch ex As Exception
  951. Throw New Exception("Corrupt NCD File")
  952. End Try
  953. Return data
  954. End Function
  955. Public Function LoadDialog() As Boolean
  956. Dim result As Boolean = False
  957. Dim OpenFileDialog1 As OpenFileDialog = New OpenFileDialog
  958. OpenFileDialog1.InitialDirectory = GetDefaultNCDFileFolder()
  959. OpenFileDialog1.FileName = DefaultFilename
  960. OpenFileDialog1.Filter = Filter
  961. If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
  962. EEPROM = LoadFile(OpenFileDialog1.FileName)
  963. result = True
  964. Else
  965. result = False
  966. End If
  967. Return result
  968. End Function
  969. Public Sub DumpEEPROM()
  970. Debug.Print("begin dump")
  971. Dim i As Integer = 0
  972. For i = 0 To EEPROM.Length - 1
  973. System.Diagnostics.Debug.Write(NCDLib.Math.DecToHexString(EEPROM(i)) + " ")
  974. If (i Mod 16) = 0 Then
  975. System.Diagnostics.Debug.WriteLine("")
  976. End If
  977. Next
  978. Debug.Print("end dump")
  979.  
  980. End Sub
  981. Private Function GetDefaultNCDFileFolder() As String
  982. ' get current executing assembly
  983. Dim app As Assembly = Assembly.GetExecutingAssembly()
  984. Dim locationPath As String = Path.GetDirectoryName(app.ManifestModule.FullyQualifiedName)
  985. 'Dim ncdFilePath As String = locationPath + "\EEPROM.ncd"
  986. 'Return ncdFilePath
  987. Return locationPath
  988. End Function
  989. Private Function GetDefaultNCDFilePath()
  990. Return GetDefaultNCDFileFolder() + "\" + DefaultFilename
  991. End Function
  992. ''' <summary>
  993. ''' Get the checksum of array
  994. ''' </summary>
  995. ''' <param name="a"></param>
  996. ''' <returns></returns>
  997. ''' <remarks></remarks>
  998. Private Function GetCheckSum(ByVal a As Byte()) As Integer
  999. Dim i As Integer = 0
  1000. Dim checksum As Integer = 0
  1001. For i = 0 To a.Length - 1
  1002. checksum = checksum + a(i)
  1003. Next
  1004. Return checksum
  1005. End Function
  1006.  
  1007. ''' <summary>
  1008. ''' This function is used to store EEPROM Data to NCD CPU
  1009. ''' </summary>
  1010. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1011. ''' <remarks></remarks>
  1012. Public Function To_Chip(ByRef _ncd As NCD.NCDComponent, ByRef _block As Byte) As Boolean
  1013. Dim retry As Integer = 0
  1014. RetryIt:
  1015. Dim result As Boolean = False
  1016. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1017. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1018. Return result 'Return Result to the Caller and Exit Function
  1019. End If
  1020.  
  1021. If _block = 15 Then
  1022. Debug.Print("Block 15 Cannot be Stored by this Function...Quietyly Emulating Storage of this block...")
  1023. result = True
  1024. Return result
  1025. End If
  1026.  
  1027. '_ncd.SerialPort.DiscardInBuffer()
  1028. _ncd.Purge()
  1029.  
  1030. 'If (_ncd.UsingComPort) Then
  1031. NCDLib.WriteBytes(_ncd, 2, 0, 2, _block)
  1032. _ncd.SetTcpWritePace(2)
  1033. _ncd.Flush()
  1034. DELA()
  1035. Dim Checksum As Integer = 0 'Define Checksum
  1036. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1037. _ncd.WriteByte(EEPROM(DataByte + (_block * 16))) 'One Byte at a Time
  1038. DELA()
  1039. Checksum += EEPROM(DataByte + (_block * 16)) 'Include Each Byte in the Checkum Value
  1040. 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1041. Next DataByte
  1042. Checksum = Checksum And 255
  1043. _ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1044.  
  1045.  
  1046. Dim ReturnValue As Integer = _ncd.ReadByte() 'Wait for the Controller to Respond with 85 Confirmation Byte
  1047. If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1048. result = True 'This Function was successful and the Result is True
  1049. Else
  1050. retry = retry + 1
  1051. Debug.Print("retry " + retry.ToString)
  1052. If retry < 4 Then
  1053. GoTo RetryIt
  1054. End If
  1055. End If
  1056.  
  1057. Return result 'Return Result to Caller and Exit Function
  1058. End Function
  1059.  
  1060. ''' <summary>
  1061. ''' This function is used to store EEPROM Data to NCD CPU
  1062. ''' </summary>
  1063. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1064. ''' <remarks></remarks>
  1065. Public Function To_ChipFast(ByRef _ncd As NCD.NCDComponent, ByRef _block As Byte) As Boolean
  1066. Dim retry As Integer = 0
  1067. RetryIt:
  1068. Dim result As Boolean = False
  1069. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1070. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1071. Return result 'Return Result to the Caller and Exit Function
  1072. End If
  1073.  
  1074. If _block = 15 Then
  1075. Debug.Print("Block 15 Cannot be Stored by this Function...Quietyly Emulating Storage of this block...")
  1076. result = True
  1077. Return result
  1078. End If
  1079.  
  1080. _ncd.Purge()
  1081.  
  1082. 'If (_ncd.UsingComPort) Then
  1083. _ncd.WriteBytes(0, 2, _block)
  1084. '_ncd.SetTcpWritePace(2)
  1085. _ncd.Flush()
  1086. Dim Checksum As Integer = 0 'Define Checksum
  1087. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1088. _ncd.WriteByte(EEPROM(DataByte + (_block * 16))) 'One Byte at a Time
  1089. Checksum += EEPROM(DataByte + (_block * 16)) 'Include Each Byte in the Checkum Value
  1090. Next DataByte
  1091. Checksum = Checksum And 255
  1092. _ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1093.  
  1094.  
  1095. 'Else
  1096. 'Dim Checksum As Integer = 0 'Define Checksum
  1097. 'Dim tmp(0 To 19) As Byte
  1098. 'tmp(0) = 0
  1099. 'tmp(1) = 2
  1100. 'tmp(2) = _block
  1101. 'For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1102. ' '_ncd.WriteByte(EEPROM(DataByte + (_block * 16))) 'One Byte at a Time
  1103. ' tmp(DataByte + 3) = EEPROM(DataByte + (_block * 16))
  1104. ' Checksum += EEPROM(DataByte + (_block * 16)) 'Include Each Byte in the Checkum Value
  1105. ' 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1106. 'Next DataByte
  1107. 'Checksum = Checksum And 255
  1108. 'tmp(19) = Checksum
  1109. ''_ncd.SetTcpWritePace(0)
  1110.  
  1111. ''_ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1112. 'Threading.Thread.Sleep(1000)
  1113. 'End If
  1114.  
  1115. Dim ReturnValue As Integer = _ncd.ReadByte() 'Wait for the Controller to Respond with 85 Confirmation Byte
  1116. If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1117. result = True 'This Function was successful and the Result is True
  1118. Else
  1119. retry = retry + 1
  1120. Debug.Print("retry " + retry.ToString)
  1121. If retry < 4 Then
  1122. GoTo RetryIt
  1123. End If
  1124. End If
  1125.  
  1126. Return result 'Return Result to Caller and Exit Function
  1127. End Function
  1128.  
  1129.  
  1130.  
  1131. ''' <summary>
  1132. ''' This function is used to store EEPROM Data to NCD CPU
  1133. ''' </summary>
  1134. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1135. ''' <remarks></remarks>
  1136. Public Shared Function To_EEPROM_Chip(ByRef _ncd As NCD.NCDComponent, ByVal _blockId As Integer, ByVal blockData As Byte()) As Boolean
  1137. Dim retry As Integer = 0
  1138. RetryIt:
  1139. Dim result As Boolean = False
  1140. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1141. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1142. Return result 'Return Result to the Caller and Exit Function
  1143. End If
  1144.  
  1145. '_ncd.SerialPort.DiscardInBuffer()
  1146. 'DELA()
  1147. '_ncd.WriteByte(0) 'Send Header Byte
  1148. 'DELA()
  1149. '_ncd.WriteByte(4) 'Send Store Command
  1150. 'DELA()
  1151. '_ncd.WriteByte(NCDLib.Math.GetLSB(_blockId)) 'Send Block
  1152. 'DELA()
  1153. '_ncd.WriteByte(NCDLib.Math.GetMSB(_blockId)) 'Send Block
  1154.  
  1155. '_ncd.WriteBytes(New Byte() {0, 4, NCDLib.Math.GetLSB(_blockId), NCDLib.Math.GetMSB(_blockId)})
  1156. NCDLib.WriteBytes(_ncd, 2, 0, 4, NCDLib.Math.GetLSB(_blockId), NCDLib.Math.GetMSB(_blockId))
  1157. _ncd.SetTcpWritePace(2)
  1158. _ncd.Flush()
  1159. DELA()
  1160. Dim Checksum As Integer = 0 'Define Checksum
  1161. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1162. _ncd.WriteByte(blockData(DataByte)) 'One Byte at a Time
  1163. DELA()
  1164. Checksum += blockData(DataByte) 'Include Each Byte in the Checkum Value
  1165. 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1166. Next DataByte
  1167. Checksum = Checksum And 255
  1168. _ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1169.  
  1170. Dim ReturnValue As Integer = _ncd.ReadByte() 'Wait for the Controller to Respond with 85 Confirmation Byte
  1171. If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1172. result = True 'This Function was successful and the Result is True
  1173. Else
  1174. retry = retry + 1
  1175. If retry < 4 Then
  1176. GoTo RetryIt
  1177. End If
  1178. End If
  1179.  
  1180. Return result 'Return Result to Caller and Exit Function
  1181. End Function
  1182.  
  1183.  
  1184. ''' <summary>
  1185. ''' This function is used to store EEPROM Data to NCD CPU
  1186. ''' </summary>
  1187. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1188. ''' <remarks></remarks>
  1189. Public Shared Function To_EEPROM_ChipFast(ByRef _ncd As NCD.NCDComponent, ByVal _blockId As Integer, ByVal blockData As Byte()) As Boolean
  1190. Dim retry As Integer = 0
  1191. RetryIt:
  1192. Dim result As Boolean = False
  1193. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1194. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1195. Return result 'Return Result to the Caller and Exit Function
  1196. End If
  1197.  
  1198. '_ncd.TRIIWriteBytes(0, 4, NCDLib.Math.GetLSB(_blockId), NCDLib.Math.GetMSB(_blockId))
  1199. 'Dim Checksum As Integer = 0 'Define Checksum
  1200. 'For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1201. ' _ncd.TRIIWriteBytes(blockData(DataByte)) 'One Byte at a Time
  1202. ' Checksum += blockData(DataByte) 'Include Each Byte in the Checkum Value
  1203. ' 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1204. 'Next DataByte
  1205. 'Checksum = Checksum And 255
  1206. '_ncd.TRIIWriteBytes(Checksum) 'Send the Checksum Value to the Controller
  1207. '_ncd.SetTcpWritePace(0)
  1208. '_ncd.Flush()
  1209.  
  1210. 'Dim ReturnValue As Integer = _ncd.ReadByte() 'Wait for the Controller to Respond with 85 Confirmation Byte
  1211. 'If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1212. ' result = True 'This Function was successful and the Result is True
  1213. 'Else
  1214. ' Debug.Print(ReturnValue.ToString)
  1215.  
  1216. ' retry = retry + 1
  1217. ' Debug.Print("retry save to eeprom " & retry)
  1218. ' If retry < 4 Then
  1219. ' GoTo RetryIt
  1220. ' End If
  1221. 'End If
  1222.  
  1223. Dim a As ArrayList = New ArrayList
  1224. a.Add(0)
  1225. a.Add(4)
  1226. a.Add(NCDLib.Math.GetLSB(_blockId))
  1227. a.Add(NCDLib.Math.GetMSB(_blockId))
  1228.  
  1229. 'WriteBytesAPI(_ncd, True, 0, 4, NCDLib.Math.GetLSB(_blockId), NCDLib.Math.GetMSB(_blockId))
  1230. Dim Checksum As Integer = 0 'Define Checksum
  1231. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1232. 'WriteBytesAPI(_ncd, True, blockData(DataByte)) 'One Byte at a Time
  1233. a.Add(blockData(DataByte))
  1234. Checksum += blockData(DataByte) 'Include Each Byte in the Checkum Value
  1235. 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1236. Next DataByte
  1237. Checksum = Checksum And 255
  1238. 'WriteBytesAPI(_ncd, True, Checksum) 'Send the Checksum Value to the Controller
  1239. a.Add(Checksum)
  1240. Dim aB(a.Count - 1) As Byte
  1241. For i As Integer = 0 To a.Count - 1
  1242. aB(i) = a(i)
  1243. Next
  1244. WriteBytesAPI(_ncd, True, aB)
  1245. _ncd.SetTcpWritePace(0)
  1246. _ncd.Flush()
  1247.  
  1248. Dim ReturnValue As Integer = ReadByteAPI(_ncd, True) 'Wait for the Controller to Respond with 85 Confirmation Byte
  1249. If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1250. result = True 'This Function was successful and the Result is True
  1251. Else
  1252. Debug.Print(ReturnValue.ToString)
  1253.  
  1254. retry = retry + 1
  1255. Debug.Print("retry save to eeprom " & retry)
  1256. If retry < 4 Then
  1257. GoTo RetryIt
  1258. End If
  1259. End If
  1260.  
  1261.  
  1262. Return result 'Return Result to Caller and Exit Function
  1263. End Function
  1264.  
  1265. ''' <summary>
  1266. ''' This function is used to store EEPROM Data to NCD CPU
  1267. ''' </summary>
  1268. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1269. ''' <remarks></remarks>
  1270. Public Function To_Chip_PROTECTED(ByRef _ncd As NCD.NCDComponent, ByRef _block As Byte) As Boolean
  1271. Dim retry As Integer = 0
  1272. RetryIt:
  1273. Dim result As Boolean = False
  1274. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1275. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1276. Return result 'Return Result to the Caller and Exit Function
  1277. End If
  1278.  
  1279. If _block <> 15 Then
  1280. Debug.Print("This Function Only Works for Block 15")
  1281. result = False
  1282. Return result
  1283. End If
  1284. '_ncd.SerialPort.DiscardInBuffer()
  1285. 'DELA()
  1286. '_ncd.WriteByte(0) 'Send Header Byte
  1287. 'DELA()
  1288. '_ncd.WriteByte(2) 'Send Store Command
  1289. 'DELA()
  1290. '_ncd.WriteByte(15) 'Send Block 15 ONLY
  1291. 'DELA()
  1292.  
  1293. '_ncd.WriteBytes(New Byte() {0, 2, 15})
  1294. NCDLib.WriteBytes(_ncd, 2, 0, 2, 15)
  1295. _ncd.SetTcpWritePace(2)
  1296. _ncd.Flush()
  1297.  
  1298. Dim Checksum As Integer = 0 'Define Checksum
  1299. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1300. _ncd.WriteByte(EEPROM(DataByte + (_block * 16))) 'One Byte at a Time
  1301. DELA()
  1302. Checksum += EEPROM(DataByte + (_block * 16)) 'Include Each Byte in the Checkum Value
  1303. 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1304. Next DataByte
  1305. Checksum = Checksum And 255
  1306. _ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1307.  
  1308. Dim ReturnValue As Integer = _ncd.ReadByte() 'Wait for the Controller to Respond with 85 Confirmation Byte
  1309. If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1310. result = True 'This Function was successful and the Result is True
  1311. Else
  1312. retry = retry + 1
  1313. If retry < 4 Then
  1314. GoTo RetryIt
  1315. End If
  1316. End If
  1317.  
  1318. Return result 'Return Result to Caller and Exit Function
  1319. End Function
  1320.  
  1321. ''' <summary>
  1322. ''' This function is used to store EEPROM Data to NCD CPU
  1323. ''' </summary>
  1324. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1325. ''' <remarks></remarks>
  1326. Public Function To_Chip_ProtectedFast(ByRef _ncd As NCD.NCDComponent, ByRef _block As Byte) As Boolean
  1327. Dim retry As Integer = 0
  1328. RetryIt:
  1329. Dim result As Boolean = False
  1330. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1331. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1332. Return result 'Return Result to the Caller and Exit Function
  1333. End If
  1334.  
  1335. If _block <> 15 Then
  1336. Debug.Print("This Function Only Works for Block 15")
  1337. result = False
  1338. Return result
  1339. End If
  1340. _ncd.WriteBytes(0, 2, 15)
  1341. _ncd.SetTcpWritePace(0)
  1342. _ncd.Flush()
  1343.  
  1344. Dim Checksum As Integer = 0 'Define Checksum
  1345. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1346. _ncd.WriteByte(EEPROM(DataByte + (_block * 16))) 'One Byte at a Time
  1347. _ncd.Flush()
  1348. Checksum += EEPROM(DataByte + (_block * 16)) 'Include Each Byte in the Checkum Value
  1349. 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1350. Next DataByte
  1351. Checksum = Checksum And 255
  1352. _ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1353. _ncd.Flush()
  1354.  
  1355. Dim ReturnValue As Integer = _ncd.ReadByte() 'Wait for the Controller to Respond with 85 Confirmation Byte
  1356. If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1357. result = True 'This Function was successful and the Result is True
  1358. Else
  1359. retry = retry + 1
  1360. If retry < 4 Then
  1361. GoTo RetryIt
  1362. End If
  1363. End If
  1364.  
  1365. Return result 'Return Result to Caller and Exit Function
  1366. End Function
  1367.  
  1368.  
  1369.  
  1370. ''' <summary>
  1371. ''' This function is used to Get EEPROM Data from NCD CPU
  1372. ''' </summary>
  1373. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1374. ''' <remarks></remarks>
  1375. Public Function From_Chip(ByRef _ncd As NCD.NCDComponent, ByRef _block As Byte) As Boolean
  1376. Dim result As Boolean = False
  1377. Dim Retrys As Integer = 0
  1378.  
  1379. Retry:
  1380. Dim ErrorCode As Integer = 0
  1381.  
  1382. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1383. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1384. Return result 'Return Result to the Caller and Exit Function
  1385. End If
  1386.  
  1387. Try
  1388. 'If (_ncd.UsingComPort) Then
  1389. ' DELA()
  1390. ' _ncd.WriteByte(0) 'Send Header Byte
  1391. ' DELA()
  1392. ' _ncd.WriteByte(1) 'Send Retrieve Command Byte
  1393. ' DELA()
  1394. ' _ncd.WriteByte(_block) 'Send Block
  1395. ' DELA()
  1396.  
  1397. 'Else
  1398. ' _ncd.WriteBytes(New Byte() {0, 1, _block})
  1399.  
  1400. 'End If
  1401. _ncd.SetTcpWritePace(2)
  1402. NCDLib.WriteBytes(_ncd, 2, 0, 1, _block)
  1403. _ncd.Flush()
  1404. Threading.Thread.Sleep(50)
  1405. Dim Checksum As Integer = 0 'Define Checksum
  1406. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1407. EEPROM(DataByte + (_block * 16)) = _ncd.ReadByte() 'One Byte at a Time
  1408. 'Debug.Print(EEPROM(DataByte + (_block * 16)).ToString)
  1409. Checksum += EEPROM(DataByte + (_block * 16)) 'Include Each Byte in the Checkum Value
  1410. Threading.Thread.Sleep(30)
  1411. Next DataByte
  1412. Checksum = Checksum And 255
  1413.  
  1414. Dim ReturnValue As Byte = _ncd.ReadByte() 'Wait for the Controller to Respond with Checksum Value
  1415.  
  1416. If ReturnValue = Checksum Then 'If Checksum Matches Returned Value from the controller
  1417. Debug.Print("CORRECT" + _block.ToString)
  1418. result = True 'This Function was successful and the Result is True
  1419. ErrorCode = 0
  1420. Else
  1421. Debug.Print("ERROR: CHECKSUM")
  1422. ErrorCode = 1
  1423. End If
  1424. Catch
  1425. Debug.Print("Communication Error.")
  1426. ErrorCode = 2
  1427. End Try
  1428.  
  1429. If ErrorCode <> 0 Then
  1430. Debug.Print("RETRY ERROR")
  1431. Retrys = Retrys + 1
  1432. If Retrys < 10 Then
  1433. GoTo Retry
  1434. End If
  1435. End If
  1436.  
  1437. Return result 'Return Result to Caller and Exit Function
  1438. End Function
  1439.  
  1440. ''' <summary>
  1441. ''' This function is used to Get EEPROM Data from NCD CPU
  1442. ''' </summary>
  1443. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1444. ''' <remarks></remarks>
  1445. Public Function From_ChipFast(ByRef _ncd As NCD.NCDComponent, ByRef _block As Byte) As Boolean
  1446. Dim result As Boolean = False
  1447. Dim Retrys As Integer = 0
  1448.  
  1449. Retry:
  1450. Dim ErrorCode As Integer = 0
  1451.  
  1452. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1453. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1454. Return result 'Return Result to the Caller and Exit Function
  1455. End If
  1456.  
  1457. Try
  1458. _ncd.SetTcpWritePace(2)
  1459. _ncd.WriteBytes(0, 1, _block)
  1460. _ncd.Flush()
  1461. 'Threading.Thread.Sleep(50)
  1462. Dim Checksum As Integer = 0 'Define Checksum
  1463. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1464. EEPROM(DataByte + (_block * 16)) = _ncd.ReadByte() 'One Byte at a Time
  1465. 'Debug.Print(EEPROM(DataByte + (_block * 16)).ToString)
  1466. Checksum += EEPROM(DataByte + (_block * 16)) 'Include Each Byte in the Checkum Value
  1467. 'Threading.Thread.Sleep(30)
  1468. Next DataByte
  1469. Checksum = Checksum And 255
  1470.  
  1471. Dim ReturnValue As Byte = _ncd.ReadByte() 'Wait for the Controller to Respond with Checksum Value
  1472.  
  1473. If ReturnValue = Checksum Then 'If Checksum Matches Returned Value from the controller
  1474. Debug.Print("CORRECT" + _block.ToString)
  1475. result = True 'This Function was successful and the Result is True
  1476. ErrorCode = 0
  1477. Else
  1478. Debug.Print("ERROR: CHECKSUM")
  1479. ErrorCode = 1
  1480. End If
  1481. Catch
  1482. Debug.Print("Communication Error.")
  1483. ErrorCode = 2
  1484. End Try
  1485.  
  1486. If ErrorCode <> 0 Then
  1487. Debug.Print("RETRY ERROR")
  1488. Retrys = Retrys + 1
  1489. If Retrys < 10 Then
  1490. GoTo Retry
  1491. End If
  1492. End If
  1493.  
  1494. Return result 'Return Result to Caller and Exit Function
  1495. End Function
  1496.  
  1497.  
  1498. ''' <summary>
  1499. ''' Read a block, return null if fail
  1500. ''' </summary>
  1501. ''' <param name="_ncd"></param>
  1502. ''' <param name="_block"></param>
  1503. ''' <returns></returns>
  1504. ''' <remarks></remarks>
  1505. Public Shared Function ReadBlock(ByRef _ncd As NCD.NCDComponent, ByRef _block As Byte) As Byte()
  1506. Dim data(15) As Byte
  1507.  
  1508. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1509. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1510. Return Nothing 'Return Result to the Caller and Exit Function
  1511. End If
  1512.  
  1513. Try
  1514. 'DELA()
  1515. '_ncd.WriteByte(0) 'Send Header Byte
  1516. 'DELA()
  1517. '_ncd.WriteByte(1) 'Send Retrieve Command Byte
  1518. 'DELA()
  1519. '_ncd.WriteByte(_block) 'Send Block
  1520. '_ncd.WriteBytes(New Byte() {0, 1, _block})
  1521. NCDLib.WriteBytes(_ncd, 2, 0, 1, _block)
  1522. _ncd.SetTcpWritePace(2)
  1523. _ncd.Flush()
  1524. _ncd.SetTcpWritePace(0)
  1525. DELA()
  1526. Dim Checksum As Integer = 0 'Define Checksum
  1527. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1528. data(DataByte) = _ncd.ReadByte() 'One Byte at a Time
  1529. 'Debug.Print(EEPROM(DataByte + (_block * 16)).ToString)
  1530. Checksum += data(DataByte) 'Include Each Byte in the Checkum Value
  1531. Next DataByte
  1532. Checksum = Checksum And 255
  1533.  
  1534. Dim ReturnValue As Byte = _ncd.ReadByte() 'Wait for the Controller to Respond with Checksum Value
  1535.  
  1536. If ReturnValue = Checksum Then 'If Checksum Matches Returned Value from the controller
  1537. Debug.Print("CORRECT" + _block.ToString)
  1538. Else
  1539. Debug.Print("ERROR: CHECKSUM")
  1540. data = Nothing
  1541. End If
  1542. Catch
  1543. Debug.Print("Communication Error.")
  1544. data = Nothing
  1545. End Try
  1546. Return data
  1547. End Function
  1548.  
  1549.  
  1550. ''' <summary>
  1551. ''' Write a block to chip
  1552. ''' </summary>
  1553. ''' <param name="_ncd"></param>
  1554. ''' <param name="_blockId"></param>
  1555. ''' <param name="blockData"></param>
  1556. ''' <returns></returns>
  1557. ''' <remarks></remarks>
  1558. Public Shared Function WriteBlock(ByRef _ncd As NCD.NCDComponent, ByVal _blockId As Integer, ByVal blockData As Byte()) As Boolean
  1559. Dim result As Boolean = False
  1560. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1561. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1562. Return result 'Return Result to the Caller and Exit Function
  1563. End If
  1564.  
  1565. '_ncd.SerialPort.DiscardInBuffer()
  1566. 'DELA()
  1567. '_ncd.WriteByte(0) 'Send Header Byte
  1568. 'DELA()
  1569. '_ncd.WriteByte(2) 'Send Store Command
  1570. 'DELA()
  1571. '_ncd.WriteByte(_blockId) 'Send Block
  1572. 'DELA()
  1573.  
  1574. _ncd.Purge()
  1575. '_ncd.WriteBytes(0, 2, _blockId)
  1576. NCDLib.WriteBytes(_ncd, 2, 0, 2, _blockId)
  1577. _ncd.SetTcpWritePace(2)
  1578. _ncd.Flush()
  1579. If (_ncd.UsingComPort) Then
  1580. Dim Checksum As Integer = 0 'Define Checksum
  1581. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1582. _ncd.WriteByte(blockData(DataByte)) 'One Byte at a Time
  1583. DELA()
  1584. Checksum += blockData(DataByte) 'Include Each Byte in the Checkum Value
  1585. 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1586. Next DataByte
  1587. Checksum = Checksum And 255
  1588. _ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1589. Else
  1590. Dim Checksum As Integer = 0 'Define Checksum
  1591. _ncd.WriteBytes(blockData) 'One Byte at a Time
  1592. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1593. Checksum += blockData(DataByte) 'Include Each Byte in the Checkum Value
  1594. 'Debug.Print("Storing 2:" + EEPROM(DataByte + (_block * 16)).ToString)
  1595. Next DataByte
  1596. Checksum = Checksum And 255
  1597. _ncd.WriteByte(Checksum) 'Send the Checksum Value to the Controller
  1598.  
  1599. End If
  1600.  
  1601.  
  1602. Dim ReturnValue As Integer = _ncd.ReadByte() 'Wait for the Controller to Respond with 85 Confirmation Byte
  1603. If ReturnValue = 85 Then 'If 85 is Returned from the controller
  1604. result = True 'This Function was successful and the Result is True
  1605. Else
  1606. result = False
  1607. End If
  1608.  
  1609. Return result 'Return Result to Caller and Exit Function
  1610.  
  1611. End Function
  1612.  
  1613. ''' <summary>
  1614. ''' This function is used to Get EEPROM Data from NCD CPU
  1615. ''' </summary>
  1616. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1617. ''' <remarks></remarks>
  1618. Public Shared Function From_EEPROM_Chip(ByRef _ncd As NCD.NCDComponent, ByVal _blockNo As UShort) As Byte()
  1619. Dim result As Boolean = False
  1620. Dim Retrys As Integer = 0
  1621. Dim _data(15) As Byte
  1622.  
  1623. Retry:
  1624. Dim ErrorCode As Integer = 0
  1625.  
  1626. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1627. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1628. Return Nothing 'Return Result to the Caller and Exit Function
  1629. End If
  1630.  
  1631. Try
  1632. 'DELA()
  1633. '_ncd.WriteByte(0) 'Send Header Byte
  1634. 'DELA()
  1635. '_ncd.WriteByte(3) 'Send Retrieve Command Byte
  1636. 'DELA()
  1637. '_ncd.WriteByte(NCDLib.Math.GetLSB(_blockNo)) 'Send Block
  1638. 'DELA()
  1639. '_ncd.WriteByte(NCDLib.Math.GetMSB(_blockNo)) 'Send Block
  1640. 'DELA()
  1641.  
  1642. '_ncd.WriteBytes(New Byte() {0, 3, NCDLib.Math.GetLSB(_blockNo), NCDLib.Math.GetMSB(_blockNo)})
  1643. NCDLib.WriteBytes(_ncd, 2, 0, 3, NCDLib.Math.GetLSB(_blockNo), NCDLib.Math.GetMSB(_blockNo))
  1644. _ncd.SetTcpWritePace(2)
  1645. _ncd.Flush()
  1646. Dim Checksum As Integer = 0 'Define Checksum
  1647. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1648. _data(DataByte) = _ncd.ReadByte() 'One Byte at a Time
  1649. 'Debug.Print(EEPROM(DataByte + (_block * 16)).ToString)
  1650. Checksum += _data(DataByte) 'Include Each Byte in the Checkum Value
  1651. Next DataByte
  1652. Checksum = Checksum And 255
  1653.  
  1654. Dim ReturnValue As Byte = _ncd.ReadByte() 'Wait for the Controller to Respond with Checksum Value
  1655.  
  1656. If ReturnValue = Checksum Then 'If Checksum Matches Returned Value from the controller
  1657. Debug.Print("CORRECT")
  1658. result = True 'This Function was successful and the Result is True
  1659. ErrorCode = 0
  1660. Else
  1661. Debug.Print("ERROR: CHECKSUM")
  1662. _data = Nothing
  1663. ErrorCode = 1
  1664. End If
  1665. Catch
  1666. Debug.Print("Communication Error.")
  1667. ErrorCode = 2
  1668. End Try
  1669.  
  1670. If ErrorCode <> 0 Then
  1671. Debug.Print("RETRY ERROR")
  1672. Retrys = Retrys + 1
  1673. If Retrys < 10 Then
  1674. GoTo Retry
  1675. End If
  1676. End If
  1677.  
  1678. Return _data 'Return Result to Caller and Exit Function
  1679. End Function
  1680.  
  1681. ''' <summary>
  1682. ''' This function is used to Get EEPROM Data from NCD CPU
  1683. ''' </summary>
  1684. ''' <returns>Assume This Function Fails Unless Otherwise Notified by Result</returns>
  1685. ''' <remarks></remarks>
  1686. Public Shared Function From_EEPROM_ChipFast(ByRef _ncd As NCD.NCDComponent, ByVal _blockNo As UShort) As Byte()
  1687. Dim result As Boolean = False
  1688. Dim Retrys As Integer = 0
  1689. Dim _data(15) As Byte
  1690.  
  1691. Retry:
  1692. Dim ErrorCode As Integer = 0
  1693.  
  1694. If Not _ncd.IsOpen Then 'Check to Make Sure Port is Open
  1695. Debug.Print("Port is not opened") 'Debug if Port is Closed
  1696. Return Nothing 'Return Result to the Caller and Exit Function
  1697. End If
  1698.  
  1699. Try
  1700. _ncd.WriteBytes(0, 3, NCDLib.Math.GetLSB(_blockNo), NCDLib.Math.GetMSB(_blockNo))
  1701. _ncd.SetTcpWritePace(2)
  1702. _ncd.Flush()
  1703. Dim Checksum As Integer = 0 'Define Checksum
  1704. For DataByte As Integer = 0 To 15 'Send EEPROM Data to Controller
  1705. _data(DataByte) = _ncd.ReadByte() 'One Byte at a Time
  1706. 'Debug.Print(EEPROM(DataByte + (_block * 16)).ToString)
  1707. Checksum += _data(DataByte) 'Include Each Byte in the Checkum Value
  1708. Next DataByte
  1709. Checksum = Checksum And 255
  1710.  
  1711. Dim ReturnValue As Byte = _ncd.ReadByte() 'Wait for the Controller to Respond with Checksum Value
  1712.  
  1713. If ReturnValue = Checksum Then 'If Checksum Matches Returned Value from the controller
  1714. Debug.Print("CORRECT")
  1715. result = True 'This Function was successful and the Result is True
  1716. ErrorCode = 0
  1717. Else
  1718. Debug.Print("ERROR: CHECKSUM")
  1719. _data = Nothing
  1720. ErrorCode = 1
  1721. End If
  1722. Catch
  1723. Debug.Print("Communication Error.")
  1724. ErrorCode = 2
  1725. End Try
  1726.  
  1727. If ErrorCode <> 0 Then
  1728. Debug.Print("RETRY ERROR")
  1729. Retrys = Retrys + 1
  1730. If Retrys < 10 Then
  1731. GoTo Retry
  1732. End If
  1733. End If
  1734.  
  1735. Return _data 'Return Result to Caller and Exit Function
  1736. End Function
  1737.  
  1738.  
  1739.  
  1740. Public Shared Sub DELA()
  1741. System.Threading.Thread.Sleep(10)
  1742. End Sub
  1743.  
  1744.  
  1745. End Class
  1746.  
  1747.  
  1748. Shared Sub WriteBytes(ByRef ncdObj As NCD.NCDComponent, ByVal sleep As Integer, ByVal ParamArray data As Byte())
  1749. For i As Integer = 0 To data.Length - 1
  1750. ncdObj.WriteByte(data(i))
  1751. Threading.Thread.Sleep(sleep)
  1752. Next
  1753. 'If (ncdObj.UsingComPort) Then
  1754. ' For i As Integer = 0 To data.Length - 1
  1755. ' ncdObj.WriteByte(data(i))
  1756. ' Threading.Thread.Sleep(sleep)
  1757. ' Next
  1758. 'Else
  1759. ' ncdObj.WriteBytes(data)
  1760. 'End If
  1761.  
  1762. End Sub
  1763. ''' <summary>
  1764. ''' string format for sending and receiving string, 0 for decimal as default and 1 for hex
  1765. ''' </summary>
  1766. ''' <remarks></remarks>
  1767. Private Shared StringFormat As Integer = 0
  1768.  
  1769.  
  1770. ''' <summary>
  1771. ''' method to setup the showing format. 0 for deciaml, 1 for hex
  1772. ''' </summary>
  1773. ''' <param name="format"></param>
  1774. ''' <remarks></remarks>
  1775. Public Shared Sub SetStringFormat(ByVal format As Integer)
  1776. StringFormat = format
  1777. End Sub
  1778.  
  1779. ''' <summary>
  1780. ''' static varible for last send data, in decimal string
  1781. ''' </summary>
  1782. ''' <remarks></remarks>
  1783. Private Shared LastSendString As String
  1784.  
  1785. ''' <summary>
  1786. ''' static varible for last received data, in decimal string
  1787. ''' </summary>
  1788. ''' <remarks></remarks>
  1789. Private Shared LastRecString As String
  1790.  
  1791. ''' <summary>
  1792. ''' return the last send command in decimal format
  1793. ''' </summary>
  1794. ''' <returns></returns>
  1795. ''' <remarks></remarks>
  1796. Public Shared Function GetLastTransmitString() As String
  1797. Return LastSendString
  1798. End Function
  1799.  
  1800. ''' <summary>
  1801. ''' return the last received data in decimal format
  1802. ''' </summary>
  1803. ''' <returns></returns>
  1804. ''' <remarks></remarks>
  1805. Public Shared Function GetLastReceivingString() As String
  1806. Return LastRecString
  1807. End Function
  1808.  
  1809. ''' <summary>
  1810. ''' return the string with space in current format
  1811. ''' </summary>
  1812. ''' <param name="num"></param>
  1813. ''' <returns></returns>
  1814. ''' <remarks></remarks>
  1815. Private Shared Function GetShowString(ByVal num As Integer) As String
  1816. If StringFormat = 0 Then
  1817. Return num.ToString & " "
  1818. End If
  1819. Return String.Format("0x{0:X2} ", num)
  1820. End Function
  1821.  
  1822. Public Shared Function WriteBytesAPI(ByRef ncdObj As NCD.NCDComponent, ByVal UsingAPI As Boolean, ByVal ParamArray data() As Byte) As Boolean
  1823.  
  1824.  
  1825. LastSendString = ""
  1826.  
  1827. ncdObj.Purge()
  1828. If (UsingAPI) Then
  1829. ncdObj.SetTcpWritePace(0)
  1830. ' write in api mode
  1831. Dim ApiPackage As ArrayList = New ArrayList
  1832. ApiPackage.Add(170) ' header
  1833. LastSendString = LastSendString & GetShowString(170)
  1834. ApiPackage.Add(data.Length) ' length
  1835. LastSendString = LastSendString & GetShowString(data.Length)
  1836. ApiPackage.AddRange(data) ' data
  1837. Dim checksum As Integer = 170 + data.Length
  1838. For i As Integer = 0 To data.Length - 1
  1839. checksum = checksum + data(i)
  1840. LastSendString = LastSendString & GetShowString(data(i))
  1841. Next
  1842. checksum = checksum Mod &H100
  1843. ApiPackage.Add(checksum) ' check sum
  1844. LastSendString = LastSendString & GetShowString(checksum)
  1845. Dim b(ApiPackage.Count - 1) As Byte
  1846.  
  1847.  
  1848. Debug.Print("API START")
  1849. For i = 0 To ApiPackage.Count - 1
  1850. b(i) = ApiPackage(i)
  1851. Debug.Print(b(i).ToString)
  1852. Next
  1853. Debug.Print("API STOP")
  1854.  
  1855. ncdObj.WriteBytes(b)
  1856. ' Dim j = ncdObj.ReadByte()
  1857. 'For Each it In ApiPackage
  1858. ' ncdObj.WriteByte(it)
  1859. 'Next
  1860. ncdObj.Flush()
  1861. Else
  1862. ncdObj.WriteBytes(data)
  1863. ncdObj.Flush()
  1864. For Each d As Integer In data
  1865. LastSendString = LastSendString & GetShowString(d)
  1866. Next
  1867. End If
  1868.  
  1869. 'If (NCDComponent1.UsingComPort) Then
  1870. ' For i As Integer = 0 To data.Length - 1
  1871. ' NCDComponent1.WriteByte(data(i))
  1872. ' ' Threading.Thread.Sleep(sleep)
  1873. ' Next
  1874. 'Else
  1875. ' NCDComponent1.WriteBytes(data)
  1876. 'End If
  1877. Return True
  1878. End Function
  1879.  
  1880. ' return data back from module
  1881. Public Shared Function ReadBytesAPI(ByRef ncdObj As NCD.NCDComponent, ByVal UsingAPI As Boolean, ByVal bytesCount As Integer) As Byte()
  1882. 'Debug.Print("START READ BYES")
  1883. LastRecString = ""
  1884. ncdObj.Sleep(200) ' to all load/save routing work, some times it can't get data back
  1885. Dim rdata As ArrayList = New ArrayList
  1886. If (UsingAPI) Then
  1887. Dim ack As Integer = ncdObj.ReadByte()
  1888. If (ack <> 170) Then
  1889. ncdObj.Purge()
  1890. Return Nothing
  1891. End If
  1892. LastRecString += GetShowString(170)
  1893. Dim length As Integer
  1894. length = ncdObj.ReadByte()
  1895. If (length <> bytesCount) Then
  1896. ncdObj.Purge()
  1897. Return Nothing
  1898. End If
  1899. LastRecString += GetShowString(length)
  1900. Dim checkSumC As Integer = 170 + length
  1901. For m As Integer = 0 To bytesCount - 1
  1902. Dim d As Integer = ncdObj.ReadByte()
  1903. If (d = -1) Then ' time out
  1904. Return Nothing
  1905. End If
  1906. rdata.Add(d)
  1907. checkSumC = checkSumC + d
  1908. LastRecString += GetShowString(d)
  1909. Next
  1910. Dim checksum As Integer
  1911. checksum = ncdObj.ReadByte
  1912. If (checksum = -1 Or (checksum <> (checkSumC Mod &H100))) Then
  1913. ncdObj.Purge()
  1914. Return Nothing
  1915. End If
  1916. LastRecString += GetShowString(checksum)
  1917. Else
  1918. For m As Integer = 0 To bytesCount - 1
  1919. Dim d As Integer = ncdObj.ReadByte()
  1920. If (d = -1) Then ' time out
  1921. Return Nothing
  1922. End If
  1923. rdata.Add(d)
  1924. LastRecString += GetShowString(d)
  1925. Next
  1926. End If
  1927. 'Report Data to User
  1928. Dim dataRec() As Byte
  1929. ReDim dataRec(bytesCount - 1)
  1930. For i As Integer = 0 To bytesCount - 1
  1931. dataRec(i) = rdata(i)
  1932. Next
  1933. 'Debug.Print("END READ BYES")
  1934. Return dataRec
  1935. End Function
  1936. Public Shared Function Read_Digi_900HP_APIFrame(ByRef ncdObj As NCD.NCDComponent)
  1937.  
  1938. LastRecString = ""
  1939. Dim rdata As ArrayList = New ArrayList
  1940. Dim length_MS As Integer
  1941. Dim length_LS As Integer
  1942. Dim length As Integer 'Setup a Length of Packet Integer
  1943. Dim BytesCount As Integer = 0 'XXX is the Unknown Number of Bytes
  1944. Dim D As Integer
  1945.  
  1946. '126 00 23 144 00 19 162 00 65 91 117 195 255 254 193 127 01 00 01 01 91 128 99 237 03 39 81
  1947.  
  1948. Dim ack As Integer = ncdObj.ReadByte() 'Look For a Header
  1949. If (ack <> 126) Then 'If an Invalid Head is Received Exit this Routine
  1950. ncdObj.Purge()
  1951. Return Nothing
  1952. End If
  1953. LastRecString += GetShowString(126) 'Add Data into the Last Received String
  1954.  
  1955. length_MS = ncdObj.ReadByte() 'Define the Length Value Based on the Packet
  1956. LastRecString += GetShowString(length_MS) 'Add Data into the Last Received String
  1957.  
  1958. length_LS = ncdObj.ReadByte() 'Define the Length Value Based on the Packet
  1959. LastRecString += GetShowString(length_LS) 'Add Data into the Last Received String
  1960.  
  1961. length = (length_MS * 256) + length_LS
  1962.  
  1963.  
  1964. Dim checkSumC As Integer = 0 '170 + length 'Setup a Checksum Value to Include the 170 Header and Length Values
  1965.  
  1966. ' Dim d As Integer = ncdObj.ReadByte() 'Receive the Byte of Data from the User
  1967. rdata.Add(D) 'Add a NULL Byte into the D Array
  1968.  
  1969.  
  1970. For m As Integer = 1 To length 'Count Through Each Byte in the Length Value
  1971. Dim Rx As Integer = ncdObj.ReadByte() 'Receive the Byte of Data from the User
  1972. 'Debug.Print(d.ToString + " m:" + m.ToString)
  1973. rdata.Add(Rx) 'Add the Received Byte to the rData Array
  1974. checkSumC = checkSumC + Rx 'Add the Received Data into the Checksum
  1975. LastRecString += GetShowString(Rx) 'Add Data into the Last Received String
  1976. Next
  1977.  
  1978. Dim checksum As Integer 'Setup a Checksum Value
  1979. checksum = ncdObj.ReadByte 'Read the Checksum from Device
  1980. If (checksum = -1) Then 'Or (checksum <> (checkSumC Mod &H100))) Then 'Compare Received Checksum with Computed Checksum, If INVALID
  1981. ncdObj.Purge() 'Exit this Function and Return NO Data
  1982. Return Nothing
  1983. End If
  1984. LastRecString += GetShowString(checksum) 'Add the Checksum Value into the Last Received String
  1985.  
  1986. ReturnData:
  1987.  
  1988. 'Report Data to User
  1989. Dim dataRec() As Byte
  1990. ReDim dataRec(length)
  1991.  
  1992. 'Debug.Print("Total Bytes Received:")
  1993. dataRec(0) = length
  1994. ' Debug.Print(dataRec(0))
  1995.  
  1996. ' Debug.Print("Data Bytes:")
  1997. For i As Integer = 1 To length
  1998. dataRec(i) = rdata(i)
  1999. 'Debug.Print(dataRec(i))
  2000. Next
  2001. ' Debug.Print("Error???")
  2002. ' Debug.Print(dataRec(0))
  2003.  
  2004. Return dataRec
  2005.  
  2006. End Function
  2007.  
  2008. ' return data back from module
  2009. Public Shared Function ReadUnknownNumberOfBytesAPI(ByRef ncdObj As NCD.NCDComponent, ByVal UsingAPI As Boolean) As Byte()
  2010. LastRecString = ""
  2011. Dim rdata As ArrayList = New ArrayList
  2012. Dim length As Integer 'Setup a Length of Packet Integer
  2013. Dim BytesCount As Integer = 0 'XXX is the Unknown Number of Bytes
  2014.  
  2015. Dim D As Integer
  2016.  
  2017. If (UsingAPI) Then 'Am I in API Mode????'YES
  2018. Dim ack As Integer = ncdObj.ReadByte() 'Look For a Header
  2019. If (ack <> 170) Then 'If an Invalid Head is Received Exit this Routine
  2020. ncdObj.Purge()
  2021. Return Nothing
  2022. End If
  2023. LastRecString += GetShowString(170) 'Add Data into the Last Received String
  2024.  
  2025.  
  2026. length = ncdObj.ReadByte() 'Define the Length Value Based on the Packet
  2027. LastRecString += GetShowString(length) 'Add Data into the Last Received String
  2028.  
  2029. Dim checkSumC As Integer = 170 + length 'Setup a Checksum Value to Include the 170 Header and Length Values
  2030.  
  2031. ' Dim d As Integer = ncdObj.ReadByte() 'Receive the Byte of Data from the User
  2032. rdata.Add(D) 'Add a NULL Byte into the D Array
  2033.  
  2034.  
  2035. For m As Integer = 1 To length 'Count Through Each Byte in the Length Value
  2036. Dim Rx As Integer = ncdObj.ReadByte() 'Receive the Byte of Data from the User
  2037. 'Debug.Print(d.ToString + " m:" + m.ToString)
  2038. rdata.Add(Rx) 'Add the Received Byte to the rData Array
  2039. checkSumC = checkSumC + Rx 'Add the Received Data into the Checksum
  2040. LastRecString += GetShowString(Rx) 'Add Data into the Last Received String
  2041. Next
  2042.  
  2043. Dim checksum As Integer 'Setup a Checksum Value
  2044. checksum = ncdObj.ReadByte 'Read the Checksum from Device
  2045. If (checksum = -1 Or (checksum <> (checkSumC Mod &H100))) Then 'Compare Received Checksum with Computed Checksum, If INVALID
  2046. ncdObj.Purge() 'Exit this Function and Return NO Data
  2047. Return Nothing
  2048. End If
  2049. LastRecString += GetShowString(checksum) 'Add the Checksum Value into the Last Received String
  2050.  
  2051. Else
  2052. For m As Integer = 0 To 255 'Read 256 Bytes MAX
  2053. Dim Rx As Integer = ncdObj.ReadByte()
  2054. If (Rx = -1) Then ' time out
  2055. BytesCount = m + 1
  2056. GoTo ReturnData
  2057. End If
  2058. rdata.Add(Rx)
  2059. LastRecString += GetShowString(Rx)
  2060. Next
  2061. End If
  2062.  
  2063. ReturnData:
  2064.  
  2065. 'Report Data to User
  2066. Dim dataRec() As Byte
  2067. ReDim dataRec(length)
  2068.  
  2069. 'Debug.Print("Total Bytes Received:")
  2070. dataRec(0) = length
  2071. ' Debug.Print(dataRec(0))
  2072.  
  2073. ' Debug.Print("Data Bytes:")
  2074. For i As Integer = 1 To length
  2075. dataRec(i) = rdata(i)
  2076. 'Debug.Print(dataRec(i))
  2077. Next
  2078. ' Debug.Print("Error???")
  2079. ' Debug.Print(dataRec(0))
  2080.  
  2081. Return dataRec
  2082.  
  2083. End Function
  2084.  
  2085. ' return data back from module, used for command only have one byte come back
  2086. Public Shared Function ReadByteAPI(ByRef ncdObj As NCD.NCDComponent, ByVal UsingAPI As Boolean) As Integer
  2087. Dim r As Integer = -1
  2088. Dim rData As Byte() = ReadBytesAPI(ncdObj, UsingAPI, 1)
  2089.  
  2090. If (rData Is Nothing) Then
  2091. Return -1
  2092. End If
  2093. r = rData(0)
  2094. Return r
  2095. End Function
  2096.  
  2097. Public Shared Function StoreExtendedEEPROMonCPU(ByRef ncdObj As NCD.NCDComponent, Start As Integer, ParamArray Bytes() As Byte)
  2098. Debug.Print("Writing Extended EEPROM DEBUG")
  2099. 'Store Menu Selection
  2100.  
  2101. Dim MSB As Integer = (Start And 65280) / 256 '>> 8
  2102. Dim LSB As Integer = (Start And 255)
  2103. Dim DataBytes(Bytes.Length + 3) As Byte
  2104. DataBytes(0) = 254
  2105. DataBytes(1) = 56
  2106. DataBytes(2) = MSB
  2107. DataBytes(3) = LSB
  2108. ' DataBytes(4) = Bytes.Length
  2109. Array.Copy(Bytes, 0, DataBytes, 4, Bytes.Length)
  2110.  
  2111. 'NCDLib.WriteBytesAPI(NCDComponent1, True, 254, 56, MSB, LSB, Bytes, DataBytes)
  2112. NCDLib.WriteBytesAPI(ncdObj, True, DataBytes)
  2113. Dim rData2 As Byte() = NCDLib.ReadBytesAPI(ncdObj, True, 1)
  2114.  
  2115. '99fdgsdfasdfa
  2116. 'afasdf()
  2117.  
  2118. If (Not (rData2 Is Nothing)) Then
  2119. Debug.Print(rData2.ToString)
  2120. StoreExtendedEEPROMonCPU = rData2
  2121. Else
  2122. MsgBox("Firmware Update Required for Fusion Controller: Unable to store extended data (NCDLib.StoreExtendedEEPROMonCPU")
  2123. End If
  2124.  
  2125.  
  2126. End Function
  2127.  
  2128.  
  2129. Public Shared Function ReadExtendedEEPROMonCPU(ByRef ncdObj As NCD.NCDComponent, Start As Integer, Length As Integer) As Byte()
  2130.  
  2131. Dim MSB As Integer = (Start And 65280) / 256 '>> 8
  2132. Dim LSB As Integer = (Start And 255)
  2133.  
  2134. ' NCDLib.EEPROMIO.ReadBlock(NCDComponent1, 0)
  2135. '254 55 1 0 16
  2136. 'Load Menu Settings from Controller
  2137. NCDLib.WriteBytesAPI(ncdObj, True, 254, 55, MSB, LSB, Length) 'Force Communications using API (Regardless of User Settings)
  2138. Dim rData As Byte() = NCDLib.ReadBytesAPI(ncdObj, True, Length) 'Force Communications using API (Regardless of User Settings)
  2139. ' CMD_Data("Read 8-Bit 8-Channels at a Time:", "", False)
  2140. 'If (Not (rData Is Nothing)) Then
  2141. Return rData
  2142. ' End If
  2143.  
  2144.  
  2145. End Function
  2146.  
  2147. Public Class I2C
  2148. Shared I2CError As String = ""
  2149.  
  2150. Public Class Device
  2151. Public Shared Function RealWorldDisplay(Raw As Long, Type As String, Resolution As Integer, Gain As Integer) As String
  2152. Debug.Print(">>>>>>>>>>>>>>>>>>>>>TYPE:")
  2153. Debug.Print(Type)
  2154.  
  2155. Dim ResolutionMultiplier As Decimal
  2156. ' Dim GainDivider As Decimal = 1
  2157.  
  2158. ' If Gain = 0 Then GainDivider = 1 'x1 Gain
  2159. ' If Gain = 1 Then GainDivider = 2 'x2 Gain
  2160. ' If Gain = 2 Then GainDivider = 4 'x4 Gain
  2161. ' If Gain = 3 Then GainDivider = 8 'x8 Gain
  2162.  
  2163. If Type = "4-20mA" Then
  2164. If Resolution = 0 Then ResolutionMultiplier = (0.0107) '12-Bit
  2165. If Resolution = 1 Then ResolutionMultiplier = (0.0027) '14-Bit
  2166. If Resolution = 2 Then ResolutionMultiplier = (0.00068) '16-Bit
  2167. Return (Raw * ResolutionMultiplier).ToString + " mA"
  2168. End If
  2169. If Type = "0-20mA" Then
  2170. If Resolution = 0 Then ResolutionMultiplier = (0.0107) '12-Bit
  2171. If Resolution = 1 Then ResolutionMultiplier = (0.0027) '14-Bit
  2172. If Resolution = 2 Then ResolutionMultiplier = (0.00068) '16-Bit
  2173. Return (Raw * ResolutionMultiplier).ToString + " mA"
  2174. End If
  2175. If Type = "0-40mA" Then
  2176. If Resolution = 0 Then ResolutionMultiplier = (0.0216) '12-Bit
  2177. If Resolution = 1 Then ResolutionMultiplier = (0.0054) '14-Bit
  2178. If Resolution = 2 Then ResolutionMultiplier = (0.00134) '16-Bit
  2179. Return (Raw * ResolutionMultiplier).ToString + " mA"
  2180. End If
  2181. If Type = "0-5V ADC" Then
  2182. If Resolution = 0 Then ResolutionMultiplier = (0.0049) '12-Bit
  2183. If Resolution = 1 Then ResolutionMultiplier = (0.00122) '14-Bit
  2184. If Resolution = 2 Then ResolutionMultiplier = (0.00031) '16-Bit
  2185. Return (Raw * ResolutionMultiplier).ToString + " V"
  2186. End If
  2187. If Type = "0-10V ADC" Then
  2188. If Resolution = 0 Then ResolutionMultiplier = (0.0098) '12-Bit
  2189. If Resolution = 1 Then ResolutionMultiplier = (0.00244) '14-Bit
  2190. If Resolution = 2 Then ResolutionMultiplier = (0.00061) '16-Bit
  2191. Return (Raw * ResolutionMultiplier).ToString + " V"
  2192. End If
  2193. If Type = "0-20V ADC" Then
  2194. End If
  2195. If Type = "0-24V ADC" Then
  2196. End If
  2197.  
  2198. Return ""
  2199. End Function
  2200. End Class
  2201.  
  2202. Public Class Chip
  2203. Public Class MCP3428
  2204. Public Shared Sub SetChannelResolutionGain(ByRef ncdObj As NCD.NCDComponent, Port As Integer, Address As Integer, Channel As Integer, Resolution As Integer, Gain As Integer)
  2205.  
  2206. If Channel > 3 Then GoTo InvalidSettings
  2207. If Resolution > 2 Then GoTo InvalidSettings
  2208. If Gain > 3 Then GoTo InvalidSettings
  2209.  
  2210. Dim CRG As Integer
  2211. Dim BitChannel As Integer
  2212. Dim BitConversion As Integer = 16 'Conversion Mode = Bit 4 = 1
  2213. Dim BitResolution As Integer
  2214. Dim BitGain As Integer
  2215.  
  2216. 'Channel = 0 to 3 Bit 6-5
  2217. If Channel = 0 Then BitChannel = 0
  2218. If Channel = 1 Then BitChannel = 32
  2219. If Channel = 2 Then BitChannel = 64
  2220. If Channel = 3 Then BitChannel = 96
  2221.  
  2222. 'Resolution = 0 to 2 Bit 3-2
  2223. If Resolution = 0 Then BitResolution = 0 '12-Bit Resolution
  2224. If Resolution = 1 Then BitResolution = 4 '14-Bit Resolution
  2225. If Resolution = 2 Then BitResolution = 8 '16-Bit Resolution
  2226.  
  2227. 'Gain = 0 to 3 Bit 1-0
  2228. If Gain = 0 Then BitGain = 0 'x1 Gain
  2229. If Gain = 1 Then BitGain = 1 'x2 Gain
  2230. If Gain = 2 Then BitGain = 2 'x4 Gain
  2231. If Gain = 3 Then BitGain = 3 'x8 Gain
  2232.  
  2233. CRG = BitChannel + BitConversion + BitResolution + BitGain
  2234.  
  2235. 'Debug.Print("-----------CRG----------- " + Channel.ToString)
  2236. 'Debug.Print(NCDLib.Math.DecToBinaryStringStandard(CRG))
  2237.  
  2238. NCDLib.I2C.Write(ncdObj, Port, Address, CRG)
  2239.  
  2240. Exit Sub
  2241.  
  2242. InvalidSettings:
  2243. Debug.Print("---------------------------------------------------")
  2244. Debug.Print("ERROR: MCP3428 Invalid Parameters")
  2245. If Channel > 3 Then Debug.Print("ERROR: MCP3428 Invalid Channel")
  2246. If Resolution > 2 Then Debug.Print("ERROR: MCP3428 Invalid Resolution")
  2247. If Gain > 3 Then Debug.Print("ERROR: MCP3428 Invalid Gain")
  2248. Debug.Print("---------------------------------------------------")
  2249. End Sub
  2250.  
  2251. Public Shared Sub Setup_Channel(ByRef ncdObj As NCD.NCDComponent, Port As Integer, Address As Integer, Channel As Integer, Resolution As Integer, Gain As Integer)
  2252.  
  2253. NCDLib.I2C.Chip.MCP3428.SetChannelResolutionGain(ncdObj, Port, Address, Channel, Resolution, Gain)
  2254.  
  2255. End Sub
  2256.  
  2257. Public Shared Function Read_Channel(ByRef ncdObj As NCD.NCDComponent, Port As Integer, Address As Integer, Channel As Integer, Resolution As Integer, Gain As Integer)
  2258.  
  2259.  
  2260. 'NCDLib.I2C.Chip.MCP3428.SetChannelResolutionGain(ncdObj, Port, Address, Channel, Resolution, Gain)
  2261.  
  2262. 'Wait for I2C Chip to Finish ADC Conversion
  2263. If Resolution = 2 Then
  2264. ncdObj.Sleep(66) ' Delay 1/15th Second
  2265. End If
  2266.  
  2267. Dim result() As Byte = NCDLib.I2C.Read(ncdObj, Port, Address, 2)
  2268.  
  2269. If Not (result Is Nothing) Then
  2270. If (result.Length = 3) Then
  2271. Dim d As Long
  2272.  
  2273. 'Debug.Print("---------Resolution---" + Resolution.ToString)
  2274.  
  2275. If Resolution = 0 Then
  2276. 'Debug.Print("12-Bit Resolution")
  2277. d = (result(1) And &HF) * 256 + result(2) '(result(1) * 256 + result(2))
  2278. Else
  2279. ' Debug.Print("High Resolution")
  2280. d = (result(1) * 256) + result(2) '(result(1) * 256 + result(2))
  2281. End If
  2282. Debug.Print(d.ToString)
  2283. Read_Channel = d
  2284. Return Read_Channel
  2285. End If
  2286. End If
  2287. Return -1
  2288.  
  2289. End Function
  2290. End Class
  2291. End Class
  2292.  
  2293.  
  2294. Public Shared Function Write(ByRef ncdObj As NCD.NCDComponent, I2C_Port As Integer, Address As Integer, ByVal ParamArray TXDAT As Byte()) As Byte()
  2295. ' _ERROR.Visible = False
  2296. Dim PortSetting As Integer = I2C_Port + 50
  2297. Dim AddressSetting As Integer = Address * 2
  2298. Dim TransmitCount As Integer = TXDAT.Length
  2299. Dim ReceiveCount As Integer = 0 'RXBytes
  2300. Dim aI(TransmitCount + 4) As Byte
  2301.  
  2302. I2CError = ""
  2303. aI(0) = 188 'Byte 0
  2304. aI(1) = PortSetting 'Byte 1
  2305. aI(2) = TransmitCount + 1 'Byte 2
  2306. aI(3) = AddressSetting 'Byte 3
  2307. aI(TransmitCount + 4) = ReceiveCount 'Last Byte
  2308. For i1 As Integer = 0 To TransmitCount - 1
  2309. aI(i1 + 4) = TXDAT(i1) 'Int(txArray(i1).Text)
  2310. Next
  2311. NCDLib.WriteBytesAPI(ncdObj, True, aI)
  2312. 'Debug.Print("Command:")
  2313. 'For Each a In aI
  2314. ' Debug.Print(a)
  2315. 'Next
  2316. 'Debug.Print("--------------END Command")
  2317. ' _ERROR.Visible = False
  2318. Dim rData As Byte() = NCDLib.ReadUnknownNumberOfBytesAPI(ncdObj, True)
  2319. If (Not (rData Is Nothing)) Then
  2320. If rData(0) = 4 Then 'If we received 4 bytes of data
  2321. If rData(1) = 188 Then
  2322. If rData(4) = 67 Then
  2323. I2CError = ("I2C Write - Error Package:")
  2324. ' _ERROR.Visible = True
  2325. If rData(2) = 90 Then
  2326. If rData(3) = (255 - 90) Then
  2327. I2CError = ("ERROR: I2C Device Did Not Acknowledge. Make Sure the Correct I2C Port and Address are Selected. Make Sure SDA and SCL lines are not reversed.")
  2328. End If
  2329. End If
  2330. If rData(2) = 91 Then
  2331. If rData(3) = (255 - 91) Then
  2332. I2CError = ("ERROR: Device took Tool Long to Respond.")
  2333. End If
  2334. End If
  2335. If rData(2) = 92 Then
  2336. If rData(3) = (255 - 92) Then
  2337. I2CError = ("ERROR: Count Not Set the Address of the Device.")
  2338. End If
  2339. End If
  2340. End If
  2341. End If
  2342. End If
  2343. Return rData
  2344. Else
  2345. I2CError = ("NO DATA RECEIVED")
  2346. End If
  2347. End Function
  2348.  
  2349.  
  2350. Public Shared Function Read(ByRef ncdObj As NCD.NCDComponent, I2C_Port As Integer, Address As Integer, RXBytes As Integer) As Byte()
  2351. '_ERROR.Visible = False
  2352. Dim PortSetting As Integer = I2C_Port + 50
  2353. Dim AddressSetting As Integer = (Address * 2) + 1
  2354. Dim TransmitCount As Integer = 0 'TXDAT.Length
  2355. Dim ReceiveCount As Integer = RXBytes
  2356. Dim aI(TransmitCount + 4) As Byte
  2357.  
  2358. I2CError = ""
  2359. aI(0) = 188 'Byte 0
  2360. aI(1) = PortSetting 'Byte 1
  2361. aI(2) = TransmitCount + 1 'Byte 2
  2362. aI(3) = AddressSetting 'Byte 3
  2363. aI(TransmitCount + 4) = ReceiveCount 'Last Byte
  2364. 'For i1 As Integer = 0 To TransmitCount - 1
  2365. ' aI(i1 + 4) = TXDAT(i1) 'Int(txArray(i1).Text)
  2366. ' Next
  2367. NCDLib.WriteBytesAPI(ncdObj, True, aI)
  2368. 'Debug.Print("Command:")
  2369. 'For Each a In aI
  2370. ' Debug.Print(a)
  2371. 'Next
  2372. 'Debug.Print("--------------END Command")
  2373.  
  2374. '------------------------------------------------------------------------------------------
  2375. '_ERROR.Visible = False
  2376. Dim rData As Byte() = NCDLib.ReadUnknownNumberOfBytesAPI(ncdObj, True)
  2377. If (Not (rData Is Nothing)) Then
  2378. If rData(0) = 4 Then 'If we received 4 bytes of data
  2379. If rData(1) = 188 Then
  2380. If rData(4) = 67 Then
  2381. I2CError = "I2C Read - Error Package:"
  2382.  
  2383. '_ERROR.Visible = True
  2384. If rData(2) = 90 Then
  2385. If rData(3) = (255 - 90) Then
  2386. I2CError = ("ERROR: I2C Device Did Not Acknowledge. Make Sure the Correct I2C Port and Address are Selected. Make Sure SDA and SCL lines are not reversed.")
  2387. End If
  2388. End If
  2389. If rData(2) = 91 Then
  2390. If rData(3) = (255 - 91) Then
  2391. I2CError = ("ERROR: Device took Tool Long to Respond.")
  2392. End If
  2393. End If
  2394. If rData(2) = 92 Then
  2395. If rData(3) = (255 - 92) Then
  2396. I2CError = ("ERROR: Count Not Set the Address of the Device.")
  2397. End If
  2398. End If
  2399. End If
  2400. End If
  2401. End If
  2402. Return rData
  2403. Else
  2404. I2CError = ("NO DATA RECEIVED")
  2405. End If
  2406. End Function
  2407. End Class
  2408.  
  2409. End Class