For the past months i have been working on a cnc-machine that's controlled by a raspberry pi. I finished the mechanical and electronic part, which brings me to programming it. After some thinking I decided to make it so a pc nearby has an easy to use interface, which sends small commands to the raspberry pi. For this to work I have three questions:

  1. How can I send commands to a raspberry pi with a usb to ttl cable?

    I did some research on this question but couldn't seem to find a fitting answer. What I basically want to do is to send a 3D-vector to the raspberry pi in which the difference in all three axis are sent (can be a list or array as well).

  2. How can I configure the raspberry pi to listen to the commands?

    Some research brought me to some topics explainging how i had to connect the usb to ttl cable to the raspberry pi, but nowhere i could find how i had to listen to the connection and determine what information is being sent.

I hope I included all the needed details and didn't miss something.

  • 1) What is a USB to TTL cable? 2) What commands? 3) Search box. – joan Jul 8 '16 at 14:19
  • I presume you mean something like this ("USB to TTL serial cable"). This is meant for connection to the Pi's UART port, so you can communicate that way. They may have as few as 3 wires since that is all that's needed (Rx, Tx, ground), but beware that they probably are available with 5V logic as well -- if so do not connect it directly to the pi, use a level shifter or something similar in between. – goldilocks Jul 8 '16 at 14:30
  • I have edited out the third part of your question since it is unrelated to the first two. However, since it was implied there you don't want to run "a full OS" but something else, you will have to decide what that "something else" is; something other than a full OS which runs python is very unlikely, however, so you may want to just dustbin that idea now. – goldilocks Jul 8 '16 at 14:35

Serial is just a way to send data (often text) between two devices bidirectionally. Operating systems generally expose the serial device to user space programs for them to use as they see fit. Under Linux they can be accessed through /dev/ttyACM0 or /dev/ttyUSB0 or /dev/ttyAMA0. Under Mac through /dev/cu.usbserial or similar. And under windows it's called something like COM1.

The Raspberry Pi's serial device is at /dev/ttyAMA0 from the pi's perspecitive. By default it writes the startup logs and then attached a terminal (aka a getty) to the serial device which allows you to control the pi over the serial line from a pc.

You can view and send commands to this terminal from your computer with a USB to serial adapter and a serial program like picocom or screen (or one of many others) on Linux/Mac or putty on Windows. The baud rate that the getty uses is 115200, this is the speed of the serial connection and must match on both sides of the serial line or you will just get corrupted data.

This is nice for general access to the pi without a network connection but is not the best way to control the pi programmatically. Instead it is better to write your own program to listen on the serial port on the pi and do things when it receives messages on it.

Disable the serial getty

To do this we need to first turn off the getty and logs that are configured by default. You can do this on rasbian by following this guide. On the latest raspbian you can do this by running the following. Or on older versions follow this

sudo systemctl stop serial-getty@ttyAMA0.service
sudo systemctl disable serial-getty@ttyAMA0.service
sudo sed 's/console=serial0,115200 //' -i /boot/cmdline.txt

Then reboot. The terminal program running on your computer should be silent for this boot.

Python Serial Listener

Using python you can read from and write to a serial device using pyserial. Start by installing pyserial on the pi:

sudo apt-get install python-serial

Then save the following to a file called echo.py (or anything you want).

#!/usr/bin/env python2
import serial

with serial.Serial('/dev/ttyAMA0', 115200) as ser:
    while True:
        line = ser.readline()
        ser.write(line.upper() + '\n')

Run this on your pi python2 echo.py and then use the serial program we used before to talk to it on your computer. Note that most serial program send '\r' when you hit enter, but the above waits for '\n', you should be able to send that by pressing Ctrl+J.

Python Serial Writer

Once you have this working you can write another python program on your computer (not the pi this time) to send the messages for you. Remember to replace /dev/ttyUSB0 with you actual serial device. Save the following to send.py

#!/usr/bin/env python2
import serial

with serial.Serial('/dev/ttyUSB0', 115200) as ser:
    ser.write('Hello world!' + '\n')
    line = ser.readline()

With echo.py running on the pi you can run python2 send.py on your computer and see them communitcate.

Now you can expand these programs to do anything you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy