Serial communication with Raspberry pi - 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.

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

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

The Raspberry Pi's serial port uses 3.3v logic; RS232 uses 12v, so a level shifter would be needed to use those pins so you cannot use RS232 directly.
Serial interfacing in Python using the PySerial module (http://pyserial.sourceforge.net) it is pretty straightforward to send and recieve data. There are examples in the documentation, but essentially to send data:
import serial
port = serial.Serial(portname, baudrate, timeout)
port.write("message to send")
port.close()
It depends what you're doing with the data and what sort of data you're recieving to decide the best way to recieve data, but a very simple example:
import serial
port = serial.Serial(portname, baudrate, timeout)
data = port.read(numberofbytes)
print data
This simply waits until the number of bytes specified has been retrieved or the timeout value is reached.
You can use port.inWaiting() to return how many bytes are currently in the buffer.

you should install python library for serial drivers. you can not use rs232 directly, instead you can use a max232 chip in between rx(gpio15) tx(gpio 14) pins and your usb to serial converter. or you can use usb to ttl serial cable from adafruit. here is the link for setup : http://learn.adafruit.com/adafruits-raspberry-pi-lesson-5-using-a-console-cable/overview

Serial Communication in Raspberry pi.
There are plenty of options for serial communication
Use Visual GDB Plugin in visual studio and deploy code remotely in raspberry pi
and loop back the txd and rxd pins and check if the send message is received or not.
If you are familiar with java Install pi4j and in the example folder there is a sample program , compile and run using terminal. and check the output.
if you are connecting using RS232 and Max232 , please note that uses 3.3 volt not 5 or 12v. , it may burn your board.

Related

How to convert USB to TTL in software?

I'm a totally newbie and interested in physical programming like raspberry pi's. Is there any way to get the data which is intended to go to a "USB to TTL converter device" through USB(e.g. the output of a flashing software), read it and convert it to TTL using Python or C language? I mean something like:
Compiler --> avrdude --> Ethernet port of the compiler host PC --> Ethernet port of a Raspberry Pi --> Pins of the Raspberry Pi --> avr microcontroller
My question is about the code which I should run on the Raspberry Pi to do all the work required on that side. I'm guessing in this specific example it's possible to install avrdude on the Raspberry Pi and pipe the compiled code from the PC to the avrdude on the pi but I'm looking for a general solution that can be used with all microcontrollers, essentially turning the Raspberry Pi into a transparent, Ethernet connected, USB to TTL converter, either existing libraries or directions to how to write such code and where to read the documentations.
Edit1: There's a C language library called pigpio already installed on the Raspberry Pi OS and it can output UART. But I don't know how to read the USB data coming to the Raspberry Pi through its Ethernet port and convert it to UART data.
Edit2: I Changed USB port to Ethernet port since you can't connect two computers directly through USB. The data coming from the PC is still intended for USB to TTL converters since that's how avrdude and similar softwares are written(I guess it'll be "USB over Ethernet").

Bidirectionally communicate a Roomba robot other than a Create model with raspberry pi

I've been trying to communicate different models of Roomba vacuum robots, 9xx and 6xx series, with a raspberry using the sci port of the roomba with no success. The sequence of steps have been:
connect pins 3 (rxd), 4 (txd), and 6 (ground) of the roomba port (5v) with a sparkfun logic level converter.
connect the output (3.3v) from the logic level converter to the gpio of the raspberry pi. Roomba Rxd to rpi txd, roomba txd to pi rxd, ground to ground.
connect rpi 5v to logic level converter HV and at the same time LV to gpio pin 1 (3.3v) for feeding high and low voltage required by the logic level converter.
disable serial console of the rpi
enable serial port hardware of the rpi
install and then open minicom in the rpi using this command
minicom -b 115200 -o -D /dev/serial0
place the roomba in the charging dock
I would now expect to read information about the charging process of the roomba in the minicom console but that's not happening.
Anyone knows if any of those steps is wrong?
My goals are to been able to read robots bump sensors in first place and then control movements of the robot from a pc using the rpi in between.
Thanks for any help.
Check this webseite. it's explaining exactly how to connect the roomba to the raspberry using an logic level converter
https://domoticproject.com/roomberry-surveillance-robot-roomba-raspberry-pi-zero-w-camera/

How can I connecti Raspberry pi 4 to pixhawk?

I want to create a connection between pixhawk and raspberry pi 4 model B.
When I connect the Pixhawk to the usb port of raspberry via micro-usb, I can send commands to the pixhawk with the drone-kit scripts on raspberry.
However, when I connect the telemtry2 port to raspberry's 14th and 15th GPIO pins (rx-tx) in pixhawks, the same script gives a heartbeat error.
I have done the configuration of uart pins with rasp-config. The /boot/config.txt file is as follows. Also, when connecting from usb, I use the connection string value "/ dev / ttyACM0" and "/ dev / serial0" when the UART pins are connected.
Via mission planner:
SERIAL 2 PROTOCOL = 2 , SERIAL2 BAUD = 921
I followed this link: https://ardupilot.org/dev/docs/raspberry-pi-via-mavlink.html
Since there is no GPS installed, I set the GPS and compass control values ​​to 0 on the mission planner. That's why the pixhawk is in arm state. (blue light)
Thank you..
Have you set the following parameters?
MAV_1_CONFIG = TELEM 2 (MAV_1_CONFIG is often used to map the TELEM 2 port)
MAV_1_MODE = Onboard
SER_TEL2_BAUD = 921600 (921600 or higher recommended for applications like log streaming or FastRTPS)
Reference:
https://docs.px4.io/master/en/companion_computer/pixhawk_companion.html#companion-computer-setup
there is an easy way to connect raspberry pi with cube autopilot. the companion board lychee
https://dronee.aero/pages/lychee

UART serial access with Rpi 3

I am trying to establish a serial connection between the RPI3 and an Arduino. Because that wasn't working I connected a USB to TTY cable from my laptop to the Rx/Tx pins of RPI. I was eventually able to use PuTTy to connect to it.
I am running Android Things on the RPI and the android code I have running is supposed to be a loopback (reading from UART and writing back what was read).
Here is where I am confused....
When PuTTy connected I was presented with a command line console on the RPI.
How do I get the Rx/Tx pins on the RPI to just be serial connections into and from my application and NOT a way to log into the console?
Is that a bad idea? I suppose if I ever needed to log into the RPI this would make it more difficult...
I figured out what I was doing wrong...
When following the setup directions at the below site I was using the Bluetooth mode instead of the Application mode. When I followed the directions for Application mode I was able to start using UART0 for serial comms
Set the console attribute to the following in cmdline.txt:
console=tty0
Add the following line in config.txt:
dtoverlay=pi3-disable-bt
Remove the following lines from config.txt:
enabled_uart=1
core_freq=400
https://developer.android.com/things/hardware/raspberrypi.html#disabling_the_console
Beginning with Developer Preview 3, in which USB-Serial devices support added, You can use external USB-UART dongles like this instead of UART of Raspberry Pi 3.

Getting 'Server is at 0.0.0.0' on serial monitor - in webserver

I bought a new Arduino Ethernet Shield and hooked it to the Arduino Uno.
I am trying to set the IP address of the device to 192.168.1.177 tried File-> Examples -> Ethernet-> Webserver.
When I upload this - I get a "server is a 0.0.0.0'message in serial monitor !
My IP is set to 192.168.1.177 and setup a mac address of 0xB8, 0x27, 0xEB, 0x98, 0x46, 0x61 in the webserver code.
Appreciate any help in setting this IP address. ( I tried other combinations as well )
The Arduino Ethernet shield is mounted on the Arduino Uno, and connections are lined up .
i fix this problem by pressing firmly the shield to the board
This can manifest from many different causes. I ran into this and figured out that in my case, my the USB port + cable were not allowing sufficient power to the Ethernet chip.
I have 3 cables, 6in, 3ft, and 6ft. Only the 6" one had sufficiently low voltage drop to power the shield.
I honestly couldn't believe it, but there was a 0.1V drop between the 6in and the 6ft cables. When plugged into A USB 3.0 port with the Arduino loaded, I measured ~4.5V for the working 6in cable vs ~4.4V for the non-working 6ft cable.
I have 3 types of USB ports: 1.1, 2.0, and 3.0. Only the USB 3.0 ports provided enough power to power the shield. Thus for my case, I had to use the shortest USB cable on the 3.0 port.
When the Arduino's 5V rail was sufficiently sagging, the 3.3 Linear Regulator entered dropout mode and on all the non-working configurations I saw ~3.1V on the Ethernet shield's 3.3V rail, which is apparently sufficient to cause this symptom.