Browse Source

More example cleanup.

pyserial_fix
Scott Petersen 11 years ago
parent
commit
15115432bd
4 changed files with 29 additions and 11 deletions
  1. +6
    -2
      examples/serialport.py
  2. +6
    -2
      examples/socket_example.py
  3. +10
    -3
      examples/ssl_socket.py
  4. +7
    -4
      examples/virtual_zone_expander.py

+ 6
- 2
examples/serialport.py View File

@@ -2,17 +2,21 @@ import time
from pyad2 import AD2 from pyad2 import AD2
from pyad2.devices import SerialDevice from pyad2.devices import SerialDevice


# Configuration values
SERIAL_DEVICE = '/dev/ttyUSB0'
BAUDRATE = 115200

def main(): def main():
""" """
Example application that opens a serial device and prints messages to the terminal. Example application that opens a serial device and prints messages to the terminal.
""" """
try: try:
# Retrieve the specified serial device. # Retrieve the specified serial device.
device = AD2(SerialDevice(interface='/dev/ttyUSB0'))
device = AD2(SerialDevice(interface=SERIAL_DEVICE))


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_message += handle_message device.on_message += handle_message
device.open(baudrate=115200) # Override the default SerialDevice baudrate.
device.open(baudrate=BAUDRATE) # Override the default SerialDevice baudrate.
device.get_config() device.get_config()


# Wait for events. # Wait for events.


+ 6
- 2
examples/socket_example.py View File

@@ -2,14 +2,18 @@ import time
from pyad2 import AD2 from pyad2 import AD2
from pyad2.devices import SocketDevice from pyad2.devices import SocketDevice


# Configuration values
HOSTNAME = 'localhost'
PORT = 10000

def main(): def main():
""" """
Example application that opens a device that has been exposed to the network Example application that opens a device that has been exposed to the network
with ser2sock or similar serial->ip software.
with ser2sock or similar serial-to-IP software.
""" """
try: try:
# Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000. # Retrieve an AD2 device that has been exposed with ser2sock on localhost:10000.
device = AD2(SocketDevice(interface=('localhost', 10000)))
device = AD2(SocketDevice(interface=(HOSTNAME, PORT)))


# Set up an event handler and open the device # Set up an event handler and open the device
device.on_message += handle_message device.on_message += handle_message


+ 10
- 3
examples/ssl_socket.py View File

@@ -2,6 +2,13 @@ import time
from pyad2 import AD2 from pyad2 import AD2
from pyad2.devices import SocketDevice from pyad2.devices import SocketDevice


# Configuration values
HOSTNAME = 'localhost'
PORT = 10000
SSL_KEY = 'cert.key'
SSL_CERT = 'cert.pem'
SSL_CA = 'ca.pem'

def main(): def main():
""" """
Example application that opens a device that has been exposed to the network Example application that opens a device that has been exposed to the network
@@ -16,9 +23,9 @@ def main():
# The key/cert attributes can either be a filesystem path or an X509/PKey # The key/cert attributes can either be a filesystem path or an X509/PKey
# object from pyopenssl. # object from pyopenssl.
ssl_device.ssl = True ssl_device.ssl = True
ssl_device.ssl_key = 'cert.key' # Client private key
ssl_device.ssl_certificate = 'cert.pem' # Client certificate
ssl_device.ssl_ca = 'ca.pem' # CA certificate
ssl_device.ssl_ca = SSL_CA # CA certificate
ssl_device.ssl_key = SSL_KEY # Client private key
ssl_device.ssl_certificate = SSL_CERT # Client certificate


device = AD2(ssl_device) device = AD2(ssl_device)




+ 7
- 4
examples/virtual_zone_expander.py View File

@@ -2,6 +2,10 @@ import time
from pyad2 import AD2 from pyad2 import AD2
from pyad2.devices import USBDevice from pyad2.devices import USBDevice


# Configuration values
TARGET_ZONE = 41
WAIT_TIME = 10

def main(): def main():
""" """
Example application that periodically faults a virtual zone and then Example application that periodically faults a virtual zone and then
@@ -36,12 +40,11 @@ def main():
# Wait for events. # Wait for events.
last_update = time.time() last_update = time.time()
while True: while True:
if time.time() - last_update > 10:
# Fault zone 41 every 10 seconds.
device.fault_zone(41)

if time.time() - last_update > WAIT_TIME:
last_update = time.time() last_update = time.time()


device.fault_zone(TARGET_ZONE)

time.sleep(1) time.sleep(1)


except Exception, ex: except Exception, ex:


Loading…
Cancel
Save