Kommunikation zwischen Controller und Python über RS232CPython

Python-Programme
Guest
 Kommunikation zwischen Controller und Python über RS232C

Post by Guest »

Ich benutze einen Ionenmessgereiter und versuche, Druckmessungen aus meinem Python -Code zu erhalten, was < /p>
istimport serial
import time
import os

def open_connection(port):
""" Open the serial connection to the NGC3 device """
try:
ser = serial.Serial(port, baudrate=9600, timeout=1)
if ser.is_open:
print(f"Connected to NGC3 on {port}")
else:
print(f"Failed to connect to {port}")
return None
return ser
except serial.SerialException as e:
print(f"Error opening serial port: {e}")
return None

def send_command(ser, command):
""" Send a command to the NGC3 and capture the response """
try:
# Send the command
ser.write(command.encode())
time.sleep(0.5)
# Read the response
response = ser.read_until(b'\r\n').decode('utf-8', errors='ignore').strip()
return response
except Exception as e:
print(f"Error sending command: {e}")
return None

def get_status(ser):
""" Get the current status of the NGC3 """
command = '*S0\r\n' # Command to check status
response = send_command(ser, command)
if response:
print(f"Raw status response: {repr(response)}") # Display full raw response for analysis
return response
return None

def parse_pressure_data(response):
""" Parse the pressure data from the NGC3 response """
# Look for a valid response pattern, e.g., 'GP2A@ 1E+03,M0'
if response.startswith('GP'):
try:
# Extract the pressure value from the response, expected to be in scientific notation
pressure_value = response.split('@')[1].split(',')[0].strip()
return float(pressure_value) # Convert to float for storage
except Exception as e:
print(f"Error parsing pressure data: {e}")
return None

def record_pressure_data(ser, filename):
""" Record pressure data from NGC3 """
while True:
# Send polling command to get pressure data
command = '*P0\r\n'
response = send_command(ser, command)
if response:
print(f"Raw response: {repr(response)}")

# Attempt to parse valid pressure data
pressure = parse_pressure_data(response)
if pressure is not None:
print(f"Pressure recorded: {pressure} Torr")
with open(filename, 'a') as file:
file.write(f"Pressure: {pressure} Torr\n")
else:
print("Invalid or no pressure data received.")

time.sleep(1) # Poll every second

def main():
port = 'COM5' # COM port where the NGC3 is connected
filename = "ngc3_pressure_data.txt" # Output file for the data

# Open the serial connection
ser = open_connection(port)
if not ser:
return

# Skip remote mode check and continue with status check
print("Skipping remote mode switch, proceeding with status check...")

# Get and print status
status_response = get_status(ser)
if not status_response:
print("Failed to get status. Exiting.")
return

# Proceed to record pressure data
print("Recording pressure data...")
record_pressure_data(ser, filename)

if __name__ == '__main__':
main()
< /code>
Der Controller verfügt über eine RS232C -Schnittstelle und mein Laptop kommuniziert mit ihm über einen USB. Die Ausgabe ist < /p>
Connected to NGC3 on COM5
Skipping remote mode switch, proceeding with status check...
Raw status response: 'rA@:GI5@@ ,M0'
Recording pressure data...
Raw response: 'GP2A@ 1E+03,M0'
Pressure recorded: 1000.0 Torr
Raw response: 'GP3A@ 1E+03,M0'
Pressure recorded: 1000.0 Torr
Raw response: 'GM4@@ ,M0'
Invalid or no pressure data received.
Raw response: '1024C'
Invalid or no pressure data received.
Raw response: 'rA'
Invalid or no pressure data received.
< /code>
Wie kann ich tatsächliche Druckmessungen erhalten, die auf dem Panel des Controllers angezeigt werden, z. 1,22e-9 mbar?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post