Skip to content

Commit f82f3a8

Browse files
committed
Create BPCXXX_pythonnet.py
Simple example setting the voltage on a BPC controller.
1 parent 3af801b commit f82f3a8

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
pythonnet_template
3+
==================
4+
5+
A template useful for the creation of custom software for Thorlabs BPCXXX benchtop controllers.
6+
"""
7+
import os
8+
import time
9+
import sys
10+
import clr
11+
12+
clr.AddReference("C:\\Program Files\\Thorlabs\\Kinesis\\Thorlabs.MotionControl.DeviceManagerCLI.dll")
13+
clr.AddReference("C:\\Program Files\\Thorlabs\\Kinesis\\Thorlabs.MotionControl.GenericPiezoCLI.dll")
14+
clr.AddReference("C:\\Program Files\\Thorlabs\\Kinesis\\ThorLabs.MotionControl.Benchtop.PiezoCLI.dll")
15+
from Thorlabs.MotionControl.DeviceManagerCLI import *
16+
from Thorlabs.MotionControl.GenericPiezoCLI import *
17+
from Thorlabs.MotionControl.Benchtop.PiezoCLI import *
18+
from System import Decimal # necessary for real world units
19+
20+
def main():
21+
"""The main entry point for the application"""
22+
23+
# Uncomment this line if you are using
24+
# SimulationManager.Instance.InitializeSimulations()
25+
26+
try:
27+
28+
DeviceManagerCLI.BuildDeviceList()
29+
30+
# create new device
31+
serial_no = "71000001" # Replace this line with your device's serial number
32+
33+
# Set output voltage.
34+
voltage = 0
35+
# Connect
36+
device = BenchtopPiezo.CreateBenchtopPiezo(serial_no)
37+
device.Connect(serial_no)
38+
39+
# Retrieve channel for the device. Can call multiple times for (X) channels.
40+
channel = device.GetChannel(1)
41+
42+
# Ensure that the device settings have been initialized
43+
if not channel.IsSettingsInitialized():
44+
channel.WaitForSettingsInitialized(10000) # 10 second timeout
45+
assert channel.IsSettingsInitialized() is True
46+
47+
# Start polling and enable
48+
channel.StartPolling(250) #250ms polling rate
49+
time.sleep(25)
50+
channel.EnableDevice()
51+
time.sleep(0.25) # Wait for device to enable
52+
53+
# Get Device Information and display description
54+
device_info = channel.GetDeviceInfo()
55+
print(device_info.Description)
56+
57+
# Load any configuration settings needed by the controller/stage
58+
motor_config = channel.GetPiezoConfiguration(channel.DeviceID)
59+
time.sleep(0.25)
60+
61+
currentDeviceSettings = channel.PiezoDeviceSettings
62+
63+
64+
maxVolts = channel.GetMaxOutputVoltage()
65+
66+
if (voltage == 0):
67+
channel.SetOutputVoltage(Decimal(voltage))
68+
newVolts = channel.GetOutputVoltage()
69+
print("Voltage set to Zero")
70+
71+
elif ((voltage != 0) & (voltage <= maxVolts)):
72+
73+
#Update voltage if required using real world methods
74+
channel.SetOutputVoltage(Decimal(voltage))
75+
76+
newVolts = channel.GetOutputVoltage()
77+
print("Voltage set to {0}", newVolts)
78+
79+
80+
81+
# Stop Polling and Disconnect
82+
channel.StopPolling()
83+
device.Disconnect()
84+
85+
except Exception as e:
86+
print(e)
87+
88+
# Uncomment this line if you are using Simulations
89+
# SimulationManager.Instance.UninitializeSimulations()
90+
...
91+
92+
93+
if __name__ == "__main__":
94+
main()

0 commit comments

Comments
 (0)