Serial Protocol:

The serial port is a low-level way to send data between the Raspberry Pi and another computer system. The Raspberry Pi serial port consists of two signals, a transmit signals and a receive signal, made available on the GPIO header. To connect to another serial device you connect the transmit of one to the receive of the other and vice versa. You will also need to connect the Ground pins of the two devices together. For this project two serial converters were used to connect  the USB port of the Pi and the PC through the null-modem cable.
The Broadcom chip on the Raspberry Pi uses 0V and 3,3V logic levels. The following parameters were set: 

baud rate = 115200
bits = 8
parity = none
stop bit = 1
flow control = none

This was done with the code below:

#define SPEED B115200
cfsetospeed (&tty, SPEED);                      // Set output speed 
cfsetispeed (&tty, SPEED);                      // Set input speed 
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
tty.c_cflag &= ~(PARENB | PARODD);              // No parity
tty.c_cflag &= ~CRTSCTS;                        // No handshake
tty.c_iflag &= ~IGNBRK;                         // Disable break processing
tty.c_lflag = 0;                                // No signaling chars, no echo, no canonical processing
tty.c_oflag = 0;                                // No remapping, no delays
tty.c_iflag &= ~(IXON | IXOFF | IXANY);         // No xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);                // Ignore modem controls, enable reading
tty.c_cflag &= ~CSTOPB;                         // Set bit stop

By default the Raspberry Pi’s serial port is configured to be used for console input/output. While this is useful if you want to login using the serial port, it means you can't use the Serial Port in your programs. To be able to use the serial port to connect and talk to other devices, the serial port console login needs to be disabled. There are two files that need to be edited:

  1. /etc/inittab
  2. /boot/cmdline.txt
In the first file the command "/sbin/getty -L ttyAMA0 115200 vt100" must be disabled, in the second the command "console=ttyAMA0,115200 kgdboc=ttyAMA0,115200" must be deleted.

Sending data through the serial port is really simple: all you need to do is open the file "/dev/ttyUSB0" and write in it. The commands are the following:

#define PORT "/dev/ttyUSB0"
int fd;
fd = open (PORT, O_RDWR);
write (fd, DATA, strlen(DATA) );

To read a serial signal using a Windows system an external program is needed. For this project I used Putty, which allow the user to open an emulated serial terminal.

Image elaboration:

For the elaboration of  images I used the 'opencv' library. It allows the program to easily take pictures with a webcam (not specified) and then manipulate them without having to save it on a file first (which would have been too slow). The elaboration is really simple: the program takes a picture and save, for every pixel, the value of green level (since I used a green laser pointer I didn't need to worry about the blue and red levels; that saves some execution time), then it takes another picture and search if the green levels are different by a certain threshold; if that's so it sends the coordinates. Then the old picture is replaced by the new one and so on. By comparing two pictures in succession the program identifies only moving lights, so even if there are other green lights in the picture besides the one I control, if these light are fixed the program will ignore them.