astur80 Geschrieben September 17, 2021 at 10:20 Geschrieben September 17, 2021 at 10:20 (bearbeitet) Hi everybody! I'm working with a RS232 bricklet, connected to a weighing machine. I have to send a code to connect (S02) and ask for a measure (MSV?) to this device, but i don't receive responde with the value of the weight. ipcon = IPConnection() # Create IP connection rs232 = BrickletRS232(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd rs232.write(*string_to_char_list('S02')) rs232.write(*string_to_char_list('MSV?')) time.sleep(1) buffer=rs232.read() The value of buffer is always the same and the length of the message is 0, so i understand is not a valid response. I have tried adding the characters \ r \ n to the end of the string and the result is the same. However from Brickviewer if I enter both codes in the input field (first S02, Intro and then MSV? and Intro again) I get a valid response with the correct measure. What could be the problem? Thank you very much and excuse my english bearbeitet September 17, 2021 at 10:29 von astur80 Zitieren
photron Geschrieben September 17, 2021 at 10:42 Geschrieben September 17, 2021 at 10:42 Please try this example. I think the critical difference is the disable_read_callback() function call here. If you have the Brick Viewer tab for the RS232 Bricklet open, then the read callback is enable and the read() function call will return empty in that case. You should close Brick Viewer before testing this example. #!/usr/bin/env python # -*- coding: utf-8 -*- HOST = "localhost" PORT = 4223 UID = "XYZ" # Change XYZ to the UID of your RS232 Bricklet import time from tinkerforge.ip_connection import IPConnection from tinkerforge.bricklet_rs232 import BrickletRS232 # Convert string to char array with length 60, as needed by write def string_to_char_list(message): chars = list(message) chars.extend(['\0']*(60 - len(message))) return chars, len(message) # Assume that the message consists of ASCII characters and # convert it from an array of chars to a string def char_list_to_string(message, length): return ''.join(message[:length]) if __name__ == "__main__": ipcon = IPConnection() # Create IP connection rs232 = BrickletRS232(UID, ipcon) # Create device object ipcon.connect(HOST, PORT) # Connect to brickd # Don't use device before ipcon is connected # Ensure read callback is disabled, otherwise the read call will return empty rs232.disable_read_callback() # Send request rs232.write(*string_to_char_list('S02\r\n')) rs232.write(*string_to_char_list('MSV?\r\n')) # Wait for response time.sleep(1) # Read response message = char_list_to_string(*rs232.read()) print('Message: ' + repr(message)) ipcon.disconnect() 1 Zitieren
astur80 Geschrieben September 17, 2021 at 10:49 Autor Geschrieben September 17, 2021 at 10:49 It works perfectly! Thank you very much! Zitieren
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.