====== Lab04 - Serial port access ======
Use cat and picocom for serial line access.
==== cat ====
cat allows to read data from a file or device. The Nucleo board will be present at /dev/ttyACM0. The default speed of the [[https://en.wikipedia.org/wiki/Serial_port|serial port]] set by the kernel driver is 115200 bd.
cat /dev/ttyACM0
The main issue with serial communication is the proper settings of the communication paramters such as:
* communication speed - baudrate
* number of bites - usually 8
* number of stop-bits - usually 1
* parity - in some applications none
Unix environment allows to control the serial line settings by [[http://www.computerhope.com/unix/ustty.htm|stty]] command.
stty -F /dev/ttyS0 9600 cs8 -cstopb -parenb
Task:
* Set the proper parameters of the communication device by stty command
* Display the readings from the Nucleo board and demonstrate the Nucleo board returns different reply for different characters. When entering:
* 'w' the board will reply by "Welcome to the Nucleo STM32F401RE" and when entering
* 'a' the board will reply by your name
* 'i' the board will reply by a random character
==== picocom ====
Perform the same task with picocom terminal (help man piccocom).
==== C app ====
Use the application from HW01 and add functionality which opens the serial device and allows you to send and receive data from the board.
== Serial port open ==
int hSerial = open( "/dev/ttyUSB0", O_RDWR| O_NONBLOCK | O_NDELAY );
// h = handle
== Serial port settings ==
struct termios o_tty;
// o = object
memset (&o_tty, 0, sizeof o_tty);
iRetVal = tcgetattr (hSerial , &o_tty);
Now, modify the settings appropriately. The best guide for serial port programming in Unix is here:
[[https://support.dce.felk.cvut.cz/pos/cv5/doc/serial.html|serial port programming guide]].
== Serial port write ==
unsigned char chArrCmd[] = {'*', 'I', 'D', 'N', '?', '\r', '\n', '\0'};
int n_written = write( hSerial, chArrCmd, sizeof(chArrCmd)-1 ); // why -1?
== Serial port read ==
char chArrBuf [256];
memset (&chArrBuf , '\0', sizeof(chArrBuf) );
int n = read( hSerial, &chArrBuf , sizeof(chArrBuf) );
== Serial port close ==
close(hSerial);
Recommended includes for this task are: stdio.h, stdlib.h, string.h, unistd.h, fcntl.h, errno.h, termios.h.