Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Lab04 - Serial port access

Use cat and picocom for serial line access.

Communcation methods

cat - old method

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:

  • 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 stty command.

stty -F /dev/ttyACM0 -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echoctl -echoke 115200

picocom

Perform the same task with picocom terminal (help man piccocom). Don't forget to set the proper baud rate (115200).

C app

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: 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.

Lab Instructions

  • Download the files provided in link lab04_x.zip.
  • Compile and upload main.c file to NUCLEO board. Can you explain whats happening in main.c?
  • Send and receive data to NUCLEO board by using the lab04_sender.cpp and lab04_receiver.cpp. Can you explain what lab04_receiver.cpp and lab04_sender.cppcodes are doing?
  • NOTICE 1: in every terminal that you want to run lab04_sender.cpp and lab04_receiver.cpp, you need to run the command below to set up your serial communication.

stty -F /dev/ttyACM0 -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echoctl -echoke 115200

  • NOTICE 2: make sure that you only send and receive from one source. You wont recieve any data if you listen to NUCLEO with two terminals.
  • NOTICE 3: reset your board each time before running your code to make sure datas from previous running are not still in board buffer.
  • NOTICE 4: check that make file for nucleo board is written relative and it does not contain user name of any other person.

Lab Tasks

  • Change lab04_sender.cpp in a way that strings that are supposed to be sent are passed through the terminal.
  • Make board blink by sending led1 and led0 commands by lab04_sender.cpp from computer. You can make delays in cpp by

#include<unistd.h>
unsigned int microsecond = 1000000;
usleep(3 * microsecond);//sleeps for 3 second

  • Modify main.c of NUCLEO board in a way that if an integer (1 digit) is sent to it, adds one and returns back through UART. If one is added to 9, it should return 0, not 10.
  • expand the exercise above for four digit numbers.
courses/be5b99cpl/labs/lab04.txt · Last modified: 2021/10/14 15:00 by amjadara