## RASPEBRRY PI PLOTTING SERIAL COMUNICATION OVER TTL
On this repo we are going to document the process to run a plotter by serial of a raspberry pi.
:::spoiler Code to detect changes on USB ports:
````
#!/usr/bin/python
# .-------------------------------------------------------------------------.
# | This program monitors for changes in 'lusb' reports and files in the |
# | /dev directory when a USB device is connected or removed. It basically |
# | saves having to manually run 'lsusb' and 'ls' and having to compare the |
# | results. |
# `-------------------------------------------------------------------------'
import os
import time
def GetUsbList() : return os.popen("lsusb").read().strip().split("\n")
def GetDevList() : return os.listdir("/dev")
def Changed(old, now):
add = []
rem = []
for this in now:
if not this in old:
add.append(this)
for this in old:
if not this in now:
rem.append(this)
return add, rem
try:
print("Monitoring for USB changes and changes in /dev directory")
usbOld, devOld = GetUsbList(), GetDevList()
while True:
time.sleep(1)
usbNow, devNow = GetUsbList(), GetDevList()
usbAdd, usbRem = Changed(usbOld, usbNow)
devAdd, devRem = Changed(devOld, devNow)
if len(usbAdd) + len(usbRem) + len(devAdd) + len(devRem) > 0:
print("-------------------")
t = time.strftime("%Y-%m-%d %H:%M:%S - ")
for this in usbAdd : print(t + "Added : " + this)
for this in usbRem : print(t + "Removed : " + this)
for this in devAdd : print(t + "Added : /dev/" + this)
for this in devRem : print(t + "Removed : /dev/" + this)
usbOld, devOld = usbNow, devNow
except KeyboardInterrupt:
print("")
````
:::
If not, you can just use ``ls /dev/tty*`` and this should list you all USB devices connected to the raspberry pi.
One you have found the device name for serial communication, we can continue. In my case is **/dev/ttyACM0**.
___
:::spoiler **USEFULL LINKS**
https://www.seeedstudio.com/blog/2019/12/11/rs232-vs-ttl-beginner-guide-to-serial-communication/?srsltid=AfmBOopyHmfMTBT-HMNNl2oH9s7zu6fIzVYlkEmmFhWcwuwP7TMOU-b3
https://roboticsbackend.com/raspberry-pi-arduino-serial-communication/
:::
Step 1:
Hardware permissions for Serial
````sudo adduser your_username dialout````
This should give u rights to read serial.
Step 2:
Install Python Serial library on Raspberry Pi
``python3 -m pip install pyserial``

https://www.probotix.com/gcode_cheatsheet.pdf