Close

More Serial Ports

A project log for Small Autonomous Survey Vessel

An autonomous little boat that scans and maps lakes and rivers.

cees-meijerCees Meijer 06/17/2020 at 19:490 Comments

So far I connected the GPS to the standard serial port of the Raspberry Pi (Rx/Tx), the IMU to the I2C lines and the Sonar to the USB port. But I will also need a serial port for my radio. So that is one port short. The solution to this could be using a 'software serial port', as you can easily get on any Arduino. But that's not so simple on the Pi. I could find just one library that supports a software serial port : PiGPIO. And that only supports reading. But that could be enough, as I can use that to read the GPS.


And that works surprisingly well. Connect the Tx of the GPS to GPIO18, and test it using the  following code :

#include <iostream>
#include <stdio.h>
#include <pigpio.h>

int main(int argc, char *argv[])
{
   char serial_buff[1024];
   int chars_read =0;
   if (gpioInitialise() < 0) return 1;
   gpioSerialReadOpen(18,9600,8);
   while(true)
   {
    chars_read=gpioSerialRead(18,serial_buff,1024);
    if(chars_read>0){for(int i=0;i<chars_read;i++){printf("%c",serial_buff[i]);}}
   }
   gpioTerminate();
}
Make sure the pigio library is linked in when compiling. In Code:Blocks this is done in 'Project->Build Options', tab 'Linker Settings', add it to the 'Link Libraries' list.

Works great. There is only one drawback in using the pigpio library: it needs root privilege to run. So Every time I start it from the Code::Blocks IDE, it refuses to run, and I have to open a terminal and enter the 'sudo' command to start it.  The solution to that appears to be starting Code:Blocks from the XTerm console using 'sudo codeblocks'. That sometimes works, but most of the time gives me a  'Failed to init GTK+' error. But that could be caused by the fact I'm working through Remote Desktop.

So now I have GPS input, and the standard Tx/Rx serial port available for other purposes.

Discussions