Weight Scale Interfacing Example: Digi D770 generate/enh73.jpg


The following script communicates with a Digi Model D770 weight scale. During invoicing or other transaction related activities a function key can be programmed to launch the script below, using the ScriptRun() function.

Example Interfacing Script

* D770 scale interface

DECLARE nWeight TYPE Numeric

DECLARE cResponse TYPE String

DECLARE cDevice TYPE String

nWeight := 0

cResponse := ""

cDevice := "D770"

*

IF ComOpen(cDevice)

   * ENQ

   If .Not. ComWrite(cDevice, Chr(5))

      Goto ERROR

   Endif

   * read response ( 1 char, no terminator, wait max.1 sec )

   cResponse := ComRead(cDevice, 1,, 1)

   If IsEmpty(cResponse)

     Echo("No response from the scale")

     Goto ERROR

   Endif

*

   * NAK received

   If cResponse == Chr(21)

      Echo("Transmission error")

      Goto ERROR

   Endif

*

   * NUL received

   If cResponse == Chr(0)

      Echo("Scale not stable")

      Goto ERROR

   Endif

*

   * CAN received

   If cResponse == Chr(24)

      Echo("No new weight on the scale")

      Goto ERROR

   Endif

*

   * ACK received

   If cResponse == Chr(6)

      * request the weight - send DC1

      If .Not. ComWrite(cDevice, Chr(17))

         Goto ERROR

      Endif

 

      * wait for up to 9 characters, timeout 1 sec.

      cResponse := ComRead(cDevice, 9,, 1)

      If IsEmpty(cResponse)

         Echo("No response from the scale")

         Goto ERROR

      Endif

 

      * NAK received

      If cResponse == Chr(21)

         Echo("Transmission error")

         Goto ERROR

      Endif

           

      * ACK received (unexpected)

      If cResponse == Chr(6)

         Echo("Transmission error")

         GoTo ERROR

      Endif

 

      * end of transmission

      If cResponse == Chr(13)

         Goto ERROR

      Endif

        

      If Len(cResponse) == 9

         nWeight := Val(Substr(cResponse, 3, 5 )) / 1000

      Endif

   Else

      Echo("Unknown response from the scale")

      Goto ERROR

   Endif

Endif

 

:ERROR

   If .Not. Empty(ComError(cDevice))

      Echo(ComError(cDevice))

   Endif

   ComClose(cDevice)

  * echo(Str(nWeight))

  Return nWeight

____________________________

Related Topics:

CAPITAL Business Script

Serial Communication Functions

 



CAPITAL VISUAL BUILDER