Search
Use cat and picocom for serial line access.
cat allows to read data from a file or device. The Nucleo board will be present at /dev/ttyACM0. The default speed of the 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 parameters such as:
Unix environment allows to control the serial line settings by stty command.
stty -F /dev/ttyACM0 -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echoctl -echoke 115200
Perform the same task with picocom terminal (help man piccocom). Don't forget to set the proper baud rate (115200).
int hSerial = open( "/dev/ttyUSB0", O_RDWR| O_NONBLOCK | O_NDELAY ); // h = handle
struct termios o_tty; // o = object memset (&o_tty, 0, sizeof o_tty); iRetVal = tcgetattr (hSerial , &o_tty);
unsigned char chArrCmd[] = {'*', 'I', 'D', 'N', '?', '\r', '\n', '\0'}; int n_written = write( hSerial, chArrCmd, sizeof(chArrCmd)-1 ); // why -1?
char chArrBuf [256]; memset (&chArrBuf , '\0', sizeof(chArrBuf) ); int n = read( hSerial, &chArrBuf , sizeof(chArrBuf) );
close(hSerial);
Recommended includes for this task are: stdio.h, stdlib.h, string.h, unistd.h, fcntl.h, errno.h, termios.h.
main.c
lab04_sender.cpp
lab04_receiver.cpp
led1
led0
#include<unistd.h> unsigned int microsecond = 1000000; usleep(3 * microsecond);//sleeps for 3 second