-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDAQDigitalControl.py
More file actions
145 lines (119 loc) · 5.95 KB
/
DAQDigitalControl.py
File metadata and controls
145 lines (119 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import nidaqmx
from nidaqmx.constants import LineGrouping
import tkinter as tk
from tkinter import ttk
# Edit by Sam Kang Aug 2024
# This script uses a live GUI to help control Digital Output channels of the NIDAQ while monitoring the Digital Input of the DAQ
# 4 DO and 4 DI channel
# Click box to switch between 0/1
# Click Switch Port to switch between port0 and port1
# Function to set digital output
def set_digital_output(channel, value, port):
with nidaqmx.Task() as task:
task.do_channels.add_do_chan(f"Dev1/{port}/line{channel}", line_grouping=LineGrouping.CHAN_PER_LINE)
task.write(bool(value))
print(f"{port}.{channel} set to {'High' if value else 'Low'}")
# Function to read digital input
def read_digital_input(channel, port):
with nidaqmx.Task() as task:
task.di_channels.add_di_chan(f"Dev1/{port}/line{channel}", line_grouping=LineGrouping.CHAN_PER_LINE)
value = task.read()
print(f"{port}.{channel} is {'High' if value else 'Low'}")
return value
# Update the state labels for DO
def update_do_labels():
do_state_labels[0].config(text=f"{'High' if do_channel_0_var.get() else 'Low'}")
do_state_labels[1].config(text=f"{'High' if do_channel_1_var.get() else 'Low'}")
do_state_labels[2].config(text=f"{'High' if do_channel_2_var.get() else 'Low'}")
do_state_labels[3].config(text=f"{'High' if do_channel_3_var.get() else 'Low'}")
# Update the state labels for DI
def update_di_labels():
di_channel_0_var.set(read_digital_input(4, current_port.get()))
di_channel_1_var.set(read_digital_input(5, current_port.get()))
di_channel_2_var.set(read_digital_input(6, current_port.get()))
di_channel_3_var.set(read_digital_input(7, current_port.get()))
di_state_labels[0].config(text=f"{current_port.get()}.4: {'High' if di_channel_0_var.get() else 'Low'}")
di_state_labels[1].config(text=f"{current_port.get()}.5: {'High' if di_channel_1_var.get() else 'Low'}")
di_state_labels[2].config(text=f"{current_port.get()}.6: {'High' if di_channel_2_var.get() else 'Low'}")
di_state_labels[3].config(text=f"{current_port.get()}.7: {'High' if di_channel_3_var.get() else 'Low'}")
# Callback functions for DO button clicks
def toggle_do_channel_0():
current_value = do_channel_0_var.get()
set_digital_output(0, current_value, current_port.get())
update_do_labels()
def toggle_do_channel_1():
current_value = do_channel_1_var.get()
set_digital_output(1, current_value, current_port.get())
update_do_labels()
def toggle_do_channel_2():
current_value = do_channel_2_var.get()
set_digital_output(2, current_value, current_port.get())
update_do_labels()
def toggle_do_channel_3():
current_value = do_channel_3_var.get()
set_digital_output(3, current_value, current_port.get())
update_do_labels()
def switch_port():
current_port.set("port1" if current_port.get() == "port0" else "port0")
update_di_labels()
update_do_labels()
# Create the main application window
root = tk.Tk()
root.title("NI DAQ Digital I/O Control")
# Initialize the port variable after creating the root window
current_port = tk.StringVar(value="port0")
# Variables to hold the state of each DO channel
do_channel_0_var = tk.IntVar()
do_channel_1_var = tk.IntVar()
do_channel_2_var = tk.IntVar()
do_channel_3_var = tk.IntVar()
# Create checkbuttons for each DO channel
do_channel_0_cb = ttk.Checkbutton(root, text="DO Channel 0", variable=do_channel_0_var, command=toggle_do_channel_0)
do_channel_1_cb = ttk.Checkbutton(root, text="DO Channel 1", variable=do_channel_1_var, command=toggle_do_channel_1)
do_channel_2_cb = ttk.Checkbutton(root, text="DO Channel 2", variable=do_channel_2_var, command=toggle_do_channel_2)
do_channel_3_cb = ttk.Checkbutton(root, text="DO Channel 3", variable=do_channel_3_var, command=toggle_do_channel_3)
# Create labels to show the current state of each DO channel
do_state_labels = [
ttk.Label(root, text="Low"),
ttk.Label(root, text="Low"),
ttk.Label(root, text="Low"),
ttk.Label(root, text="Low")
]
# Variables to hold the state of each DI channel
di_channel_0_var = tk.IntVar()
di_channel_1_var = tk.IntVar()
di_channel_2_var = tk.IntVar()
di_channel_3_var = tk.IntVar()
# Create labels to show the current state of each DI channel
di_state_labels = [
ttk.Label(root, text=f"{current_port.get()}.4: Low"),
ttk.Label(root, text=f"{current_port.get()}.5: Low"),
ttk.Label(root, text=f"{current_port.get()}.6: Low"),
ttk.Label(root, text=f"{current_port.get()}.7: Low")
]
# Arrange the checkbuttons and labels in the window for DO channels
ttk.Label(root, text="Digital Output Channels").grid(row=0, column=0, columnspan=2)
do_channel_0_cb.grid(row=1, column=0, padx=20, pady=10)
do_channel_1_cb.grid(row=2, column=0, padx=20, pady=10)
do_channel_2_cb.grid(row=3, column=0, padx=20, pady=10)
do_channel_3_cb.grid(row=4, column=0, padx=20, pady=10)
do_state_labels[0].grid(row=1, column=1, padx=20, pady=10)
do_state_labels[1].grid(row=2, column=1, padx=20, pady=10)
do_state_labels[2].grid(row=3, column=1, padx=20, pady=10)
do_state_labels[3].grid(row=4, column=1, padx=20, pady=10)
# Arrange the labels in the window for DI channels
ttk.Label(root, text="Digital Input Channels").grid(row=5, column=0, columnspan=2)
di_state_labels[0].grid(row=6, column=0, columnspan=2, padx=20, pady=10)
di_state_labels[1].grid(row=7, column=0, columnspan=2, padx=20, pady=10)
di_state_labels[2].grid(row=8, column=0, columnspan=2, padx=20, pady=10)
di_state_labels[3].grid(row=9, column=0, columnspan=2, padx=20, pady=10)
# Port selection button
ttk.Label(root, text="Current Port:").grid(row=10, column=0, padx=20, pady=10)
port_label = ttk.Label(root, textvariable=current_port)
port_label.grid(row=10, column=1, padx=20, pady=10)
switch_port_button = ttk.Button(root, text="Switch Port", command=switch_port)
switch_port_button.grid(row=11, column=0, columnspan=2, padx=20, pady=10)
# Update DI labels initially
update_di_labels()
# Start the Tkinter event loop
root.mainloop()