Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial communication with Raspberry pi

How can we do serial communication using the raspberry pi with a python script to send/receive data to my laptop? If yes can we use the RS232 cable to connect to the TX/RX ports on the raspberry pi directly? If yes, what pins must be used from the RS232 cable? It would be help full if anyone can post a example python script?

Since my first aim is to send/receive data to the laptop to/from the raspberry pi, i'm using a RS232 to USB cable at both end to connect to the laptop as well as the raspberry pi.

like image 521
Randu Avatar asked May 03 '26 11:05

Randu


2 Answers

RS-232/RS-485 to PC and USB to PC

For RS-232

1) Download Putty.

2) Buy a Serial Port RS232 to TTL Converter Module and a RS-232 serial cable for PC.

3) Follow the steps in Connection to a microcontroller or other peripheral on this link. Actually read the whole thing for better understanding.

4) Power your converter module with either 3.3V (pin 1) or 5.0V (pin 2), connect Rxd pin of the module to Rxd (pin 8) on Rpi and Txd pin to Txd (pin 10) on Rpi.

5) Connect your RS-232 (from PC) cable to the converter module

Now you are ready to do some coding in Python. But before that make sure that you have the library called serial for python to create the communication. You can easliy get it via terminal by typing sudo apt-get install python-serial. Also you will see the baudrate in the code is 7200. It could be less or more depending on the synchronization. Also make sure that baudrate should be same in putty and COM1, which is the port that RS-232 is connected to your PC. You can check and set it from device manager in Windows. By the way, timeout is the time gap between each message you are receiving.

In case you can't run the code from idle (that happens for some libraries), do it in terminal. For that go to the folder where you keep your python code and type python name.py.

    import serial
    import time


    def readlineCR(port):
        rv = ""
        while True:
        ch = port.read()
        rv += ch
        if ch == '\r' or ch == '':
             return rv


    port = serial.Serial("/dev/ttyAMA0", baudrate = 7200, timeout = 2)

    while True: 
         rcv = readlıneCR(port)
         port.write("I typed: " + repr(rcv))
         print(rcv)

For USB serial to PC

You have two options

First, you can buy a USB dongle for RS-232/RS-485 so that you would not use GPIO pins. But it is better to get a USB hub for all mouse, keyboard and dongle.

Second and easier, you can buy a FTDI USB to TTL converter and use GPIOs to have a serial communication with Rpi. The code for this is exactly the same the one with above. Connection for this is easy.

Module -- Rpi

Txd -- > Txd

Rxd -- > Rxd

Gnd ---> Gnd

like image 131
Cugomastik Avatar answered May 06 '26 10:05

Cugomastik


Are you trying to issue commands to the Raspberry Pi? (like a console?) I would suggest a plain jane 3.3V FTDI cable. You can get them from Digikey or Sparkfun. Note: the Raspberry Pi runs on 3.3V so you must be sure that anything you connect to it is running 3.3V or has a level shifter. See more information about level shifters here. (go down to the Logic voltage levels section)

First, you need to make sure that Raspbian has released the serial console. You can do that with the script located here.

If you're more interested with communicating with other devices then maybe the following suits you:

Do you have an Arduino? You can run a simple test by putting this sketch on your Arduino:

#define SERIAL_BAUD 115200

void setup() {

  //Init serial connection
  Serial.begin(SERIAL_BAUD);
  Serial.setTimeout(1);

}

void loop() {

  if ( Serial.available() ) {
    byte type = Serial.read();
    Serial.write(type);
  }

}

And wire it up using the following diagram:

Wiring Arduino to Raspberry Pi

Note: make sure you don't connect a usb cable to the Arduino. It will be powered by the Raspberry Pi.

You can then install and run screen. Screen is a dead simple way of connecting to a serial port.

apt-get install screen

Then run the following:

screen /dev/ttyAMA0 115200

The screen will show up blank. But, when you start typing you will notice that the characters you're writing are getting looped back to your terminal.

Note: If the screen is still blank you should double check the connections (power led on the Arduino is a good thing to check).

When in doubt you can see my whole example here.

like image 39
jaredwolff Avatar answered May 06 '26 11:05

jaredwolff