Jump to content

RS232 Python Communication


Recommended Posts

Hi,

I am using the TinkerForge RS232 V2 Module and want to read sensor data.

This is my code

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# For this example connect the RX1 and TX pin to receive the send message

HOST = "localhost"
PORT = 4223
UID = "XXXX" # Change XYZ to the UID of your RS232 Bricklet 2.0

from tinkerforge.ip_connection import IPConnection
from tinkerforge.bricklet_rs232_v2 import BrickletRS232V2

import time

# Callback function for read callback
def rs232_data_received(data):

    test = ''.join(data)
    print(test)

if __name__ == "__main__":
    ipcon = IPConnection() # Create IP connection
    rs232 = BrickletRS232V2(UID, ipcon) # Create device object
    ipcon.connect(HOST, PORT) # Connect to brickd

    rs232.set_configuration(baudrate=9600,
                            parity=BrickletRS232V2.PARITY_NONE,
                            stopbits=BrickletRS232V2.STOPBITS_1,
                            wordlength=BrickletRS232V2.WORDLENGTH_8,
                            flowcontrol=BrickletRS232V2.FLOWCONTROL_OFF)

    # Don't use device before ipcon is connected

    print(rs232.get_chip_temperature())

    # Register read callback to function cb_read
    
    rs232.register_callback(rs232.CALLBACK_READ, rs232_data_received)

    # Enable read callback
    rs232.enable_read_callback()

    input("Press key to exit\n") # Use raw_input() in Python 2
    ipcon.disconnect()

 

The problem is, when I get the data output I only see unusual characters like this:

 

[
ý


¿

¿


å
ë
[
ý


å
ë

[
ý

 

When I use a FTDI Chip I can read the correct output (using pyserial). I also attached a LogicAnalyzer and the data shows fine. So I think it is a problem of converting the Data (Baudrate is correct).

Since I do not now what in the middle "layer" of TinkerForge is happening with the data, can someone please help me how to convert the data into correct ascii Numbers?

Many thanks

Link zu diesem Kommentar
Share on other sites

Hey,

thanks for the fast reply. Getting the hex numbers is not my Problem. The character (when I convert the hex numbers) just make no sense. They should be numbers and or normal characters. Here is the datasheet from the sensor.

https://www.digikey.de/de/products/detail/gas-sensing-solutions-ltd/EXPLORIR-M-20/9952879

 

 

Link zu diesem Kommentar
Share on other sites

How did you conect the sensor to the Bricklet? You have to connect it to the pin header which expects 3.3V TTL signals. You cannot connect it to the D-Sub 9 connector nor the terminal block, both expect RS232 voltage levels. I think you also need to power the sensor with 3.3V to make it output 3.3V TTL signals.

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...