- Main
- Tutorials
- Library
- Samples

- File List
- Globals

Namespaces | Functions | Variables
counter.py File Reference

Counter Sample More...

Go to the source code of this file.

## Namespaces
| counter

## Functions
def | main ()

## Variables
int | DI_MODE_COUNTER = 1
int | CNT_TRIGGER_BOTH = 2

## Detailed Description

Counter Sample

Copyright
  2019 Moxa Inc. All rights reserved.

Date
  2019-02-11

Author
  William Chang

Version
  V1.0
  1 '''
  2 Copyright (C) 2019 Moxa Inc. All rights reserved.
  3 SPDX-License-Identifier: Apache-2.0
  4
  5 Counter Python Sample Application
  6
  7 Date Author Comment
  8 2019-02-11 William Chang Created it.
  9 '''
  10
  11
  19
  20 from ioThinx_4530 import ioThinx_4530_API
  21 import argparse
  22
  23 DI_MODE_COUNTER = 1
  24 CNT_TRIGGER_BOTH = 2
  25
  26
  27 def main():
  28 parser = argparse.ArgumentParser(description="Counter sample program.")
  29 parser.add_argument("-s", "--slot", dest="di_slot", type=int, default=11)
  30 parser.add_argument("-c", "--channel_start", dest="counter_channel_start", type=int, default=0)
  31 parser.add_argument("-n", "--channel_count", dest="counter_channel_count", type=int, default=2)
  32 args = parser.parse_args()
  33
  34 di_slot = args.di_slot
  35 counter_channel_start = args.counter_channel_start
  36 counter_channel_count = args.counter_channel_count
  37 print("DI slot = {}".format(di_slot))
  38 print("Counter start channel = {}".format(counter_channel_start))
  39 print("Counter channel count = {}".format(counter_channel_count))
  40
  41 # initialize ioThinx I/O
  42 device = ioThinx_4530_API.ioThinx_4530_API()
  43
  44 # temporarily set config
  45 di_modes = [DI_MODE_COUNTER] * counter_channel_count
  46 device.ioThinx_DI_Config_SetModes(di_slot, counter_channel_start, counter_channel_count, di_modes)
  47
  48 counter_triggers = [CNT_TRIGGER_BOTH] * counter_channel_count
  49 device.ioThinx_DI_Config_SetCntTriggers(di_slot, counter_channel_start, counter_channel_count, counter_triggers)
  50 counter_values = [0] * counter_channel_count
  51 device.ioThinx_DI_Config_SetCntValues(di_slot, counter_channel_start, counter_channel_count, counter_values)
  52
  53 # reload config
  54 device.ioThinx_IO_Config_Reload()
  55
  56 # start counter
  57 counter_start = [0] * 4
  58 for i in range(counter_channel_start, counter_channel_start + counter_channel_count):
  59 counter_start[i] = 1
  60 device.ioThinx_DI_SetCntStarts(di_slot, counter_start)
  61
  62 # get value
  63 while True:
  64 counter_values = device.ioThinx_DI_GetCntValues(di_slot, counter_channel_start, counter_channel_count)
  65 for i in range(counter_channel_count):
  66 print("[ {}:{}] counter = {}".format(di_slot, counter_channel_start + i, counter_values[i]))
  67
  68 if input("Press 'q' to exit. other keys to continue") == 'q':
  69 break
  70
  71
  72 if __name__ == '__main__':
  73 main()

Definition in file counter.py.