Jump to content

RS232 read buffer with Python


astur80

Recommended Posts

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.

rs232.thumb.png.190d0f24010ddce569ddeb4b2beca245.png

What could be the problem?


Thank you very much and excuse my english

 

bearbeitet von astur80
Link zu diesem Kommentar
Share on other sites

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()
Link zu diesem Kommentar
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   Du kannst Bilder nicht direkt einfügen. Lade Bilder hoch oder lade sie von einer URL.

×
×
  • Neu erstellen...