applied mechatronics - communication. serial port programming - serial port.pdf · communication....

Post on 17-Oct-2020

21 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Applied mechatronics

Communication. Serial port programming

Sven Gestegård Robertzsven.robertz@cs.lth.se

Department of Computer Science, Lund University

2020

Outline

1 Introduction

3 Terminal I/O programming (in C)Raw I/OStructured I/O

4 Serial port programmingSerial port configurationSerial port I/OConclusion

5 Summary

(Computer) mechatronics

I Controlling the physical world from a computerI SensorsI ActuatorsI Communication

I with other equipmentI with the user (operator)

I Design systems for debugging

Introduction Communication. Serial port programming 2/43

The lab project

I Building and programming a small servo systemI Building from PC towards embedded system

I Communication (serial port)I Interface hardwareI Host (PC) programming

I Embedded systemI Building a simple microcontroller cardI Programming itI Sensors and actuators

I The system depends on communication betweenPC and microcontroller

Introduction Communication. Serial port programming 3/43

Communication between host and embedded system

I During operationI Operator communicationI Mode changes, start/stopI Implementing a feedback loop

I Setting parametersI Make it possible to tune controllers during operationI Less hard-coded parameters, less reprogramming,

faster development cycleI During development

I Monitoring the state of the programI Printing or logging process valuesI Facilitates debugging

Introduction Communication. Serial port programming 4/43

A “typical” protocol

I Host to embedded systemcontrol word: mode changes

data: reference (set-point) valuesI Embedded system to host

status word: current mode of operation, warnings and faultsdata: process values (feedback)

Introduction Communication. Serial port programming 5/43

A “typical” protocol, simple example

I Message framesI Master telegram: master (PC) → slave (embedded system)

Control word Position setpoint Velocity feedforwardI Slave telegram: slave → master

Status word Position feedback Velocity feedback

I Mode of operation controlled by control and status wordsI Data format may depend on mode of operation

Introduction Communication. Serial port programming 6/43

A “typical” protocol example (CANopen DS 402)

Control word

Bit Name Bit Name0 Switch on 8 Pause/halt1 Disable voltage 9 Reserved2 Quick stop 10 Reserved3 Enable Operation 11 Ack. error4 Op. mode specific 12 Reset position5 Op. mode specific 13 Manufacturer specific6 Op. mode specific 14 Manufacturer specific7 Reset fault 15 Manufacturer specific

Status word

Bit Name Bit Name0 Ready to switch on 8 Reserved1 Switched on 9 Remote2 Operation enable 10 Target reached3 Fault 11 Internal limit active4 Disable voltage 12 Op. mode specific5 Quick stop 13 Op. mode specific6 Switch on disabled 14 Manufacturer specific7 Warning 15 Manufacturer specific

Introduction Communication. Serial port programming 7/43

Design decisions

I What data to send?I Data types, sizes, ranges, scaling and offsetI Trade-off communication efficiency vs. complexity of decoding

messagesI Reserve space for future additionsI Byte order for >8 bit data types

Introduction Communication. Serial port programming 8/43

Terminal Input/Output

I The fundamental conceptsI file descriptor - low level access

I open(), close() (more on these later)I read(), write()

I Stream (FILE*) - structured accessI fopen(), fdopen(), fclose()I fprintf(), fgetc(), fgets()

Terminal I/O programming (in C) Communication. Serial port programming 15/43

read()

ssize_t read(int fd, void *buf, size_t count);

I Reads up to count characters from a file descriptorI Returns

I on success: the number of characters readI zero on end-of-fileI a negative value on error

Terminal I/O programming (in C) : Raw I/O Communication. Serial port programming 16/43

Reading a single char with read()

#include <stdio.h>#include <unistd.h>int main() {

int run =1;

while(run){ssize_t res;char c;res = read(STDIN_FILENO , &c, 1);if(res > 0 ) {

printf("Read: %c\n", c);} else {

run = 0;}

}return 0;

}

Terminal I/O programming (in C) : Raw I/O Communication. Serial port programming 17/43

Reading a string with read()

#include <stdio.h>#include <unistd.h>

#define SIZE 10int main() {

int run =1;

while(run){char c[SIZE +1]; // +1: space for terminating nullssize_t res = read(STDIN_FILENO , c, SIZE);if(res > 0 ) {

c[res] = ’\0’; // add terminating nullprintf("Read: %s\n", c);

} else {run = 0;

}}return 0;

}

Terminal I/O programming (in C) : Raw I/O Communication. Serial port programming 18/43

Reading all input with read()

Check return value and increment position

#define SIZE 30

int main() {int run =1;char c[SIZE +1];size_t pos = 0;while(run && pos < SIZE){

ssize_t res = read(STDIN_FILENO , c+pos, SIZE -pos);if(res > 0 ) { // if more chars to read

pos += res; // increment pos} else {

run = 0;}

}c[pos] = ’\0’; //add terminating nullprintf("Buffer after read: %s\n", c);return 0;

}

Terminal I/O programming (in C) : Raw I/O Communication. Serial port programming 19/43

Raw access, writing with write()

#include <sys/types.h>#include <string.h>

#define BUFSIZE 20...char buf[BUFSIZE ];size_t nbytes;ssize_t bytes_written;int fd;...strncpy(buf , "This is a test\n", BUFSIZE );nbytes = strnlen(buf ,BUFSIZE );

bytes_written = write(fd, buf , nbytes );...

Terminal I/O programming (in C) : Raw I/O Communication. Serial port programming 20/43

Structured I/OOpening and closing streams

I FILE *fopen(const char *path, const char *mode);

I FILE *fdopen(int fd, const char *mode);

where mode is (r)ead, (w)rite or (a)ppend.For read/write use "r+", "w+", or "a+".#include <stdio.h>#include <errno.h>

int fd; ... initialised somehow

FILE *f = fdopen(fd, "r"); or FILE *f = fopen("file.txt", "r");if(!f){

perror("opening stream failed");// handle error or exit program

}

I int fclose(FILE *fp);

if(fclose(f)){perror("fclose failed");

}

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 21/43

Reading a single char from a stream: fgetc()

int fgetc(FILE *stream);

I Reads one character from a streamI Returns an unsigned char cast to intI or the constant EOF (End-of-file)

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 22/43

Example: fgetc()

#include <stdio.h>

int main(){

int run =1;

while(run){int res = fgetc(stdin);if(res != EOF ) {

char ch = res;printf("Read: %c\n", ch);

} else {run = 0;

}}return 0;

}

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 23/43

Read a C-string from a stream: fgets()

char * fgets(char * s, int size, FILE * stream);

I Reads up to size− 1 characters from a streamuntil a newline of EOF

I Adds a terminating null byteI If a newline is read, it is stored into the bufferI Returns s on success, or

NULL on error of if no characters were read

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 24/43

More structured I/O

Reading C strings:I char *fgets(char *s, int size, FILE *stream);

Example:char buf[BUFSIZE ]; // BUFSIZE -1 chars + terminating nullint fd; ...

FILE *f = fdopen(fd , "r");if(!f){ // handle error or return error code ...}char* res = fgets(buf , BUFSIZE , f);if(res) {

printf("input (%p): %s\n", res , buf);} else {

printf("fgets returned NULL\n");}

Formatted values:I int fprintf(FILE *stream, const char *format, ...);

I int fscanf(FILE *stream, const char *format, ...);

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 25/43

printf() format conversions

character argument type; convert tod, i signed decimal notationx unsigned hexadecimal notation (w/o leading 0x)u unsigned decimal notationc single character (converted to char)f double in decimal notation [-]mmm.dddds string (char *)I the conversion characters may be preceeded with arguments

for minimum width and/or precision. E.g.,I %4.2f : min 4 chars wide, 2 decimal positionsI %.8x : print (at least) 8 digits, padding with leading zeroes

I . . . and much more, consult a language reference

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 26/43

snprintf()

example

int snprintf(char *buf, size_t n, const char *format, ...);

I Similar functionality to StringBuilder in JavaI Writes (at most) n characters (incl. terminating null) to buf.I The return value is the number of chars in the resulting

formatted string.I If the return value is larger than n, the output was truncated.

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 27/43

snprintf()

example

#define BUFSZ 80

int main(){

char buf[BUFSZ ];int x = 17;float f = 4.2;

snprintf(buf ,BUFSZ , "adding %d and %f equals %f",x, f, x+f) ;

printf("buf: %s\n", buf);}

Terminal I/O programming (in C) : Structured I/O Communication. Serial port programming 28/43

Serial port setup

The serial port looks like a file to the program, but some setup isrequired:I Open the portI Set communication parameters

I Data rate (“baud rate”): bits per secondI Data bits: bits per character (normally 7 or 8)I Parity: (odd/even/none)I Stop bitsI Flow control

I Close the port

Serial port programming : Serial port configuration Communication. Serial port programming 29/43

Opening and closing the device

I int open(const char *path, int flags)

I returns a file descriptorI path is the device name (e.g., /dev/ttyS0)I flags is a bitwise-inclusive OR of flags, including exactly one of

I O_RDONLYI O_WRONLYI O_RDWR

I E.g., int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);

I Open for reading and writingI The port is not the “controlling terminal” for the programI Open non-blocking. Also on some systems: ignore the DCD

("carrier detect") line (On most systems, DCD is ignoredanyway, so blocking is fine if you want that.)

I int close(int fd)

I closes the file descriptor, freeing the underlying resourceI returns zero on success

Serial port programming : Serial port configuration Communication. Serial port programming 30/43

Opening the portError handling

const char *device = "/dev/ttyS0";int fd;

fd = open (device , O_RDWR | O_NOCTTY );if (fd < 0) {

perror (device );exit (1);

}

The standard function void perror(const char *s)

prints a system error message (include <stdio.h>)

Serial port programming : Serial port configuration Communication. Serial port programming 31/43

Port configuration

I Serial port configuration is fairly low-levelI read a struct with flags and parametersI change the structI write back the updated struct

s t r u c t t e rm i o s o p t i o n s ; // a l l o c a t e s t r u c tt c g e t a t t r ( fd , &op t i o n s ) ; // Get o p t i o n s f o r the po r t

c f s e t i s p e e d (&opt i on s , B2400 ) ; // Set the baud r a t e s to 2400c f s e t o s p e e d (&opt i on s , B2400 ) ;

o p t i o n s . c_c f l ag |= (CLOCAL | CREAD) ; // Enab le the r e c e i v e r and// i g n o r e modem c o n t r o l l i n e s

/∗ add o th e r c o n f i g u r a t i o n ( e . g . , nex t s l i d e ) he r eb e f o r e w r i t i n g back op t i o n s ∗/

t c s e t a t t r ( fd , TCSANOW, &op t i o n s ) ; // Write new op t i o n s

Serial port programming : Serial port configuration Communication. Serial port programming 32/43

Remarks

I CREAD: Enable receiverI CLOCAL: Ignore modem control lines (i.e., CD).

If CLOCAL is off, open() will block until the carrier detect signal isasserted (electrically), unless the flag O_NONBLOCK is set.

Note that “or” (as in flags |= a | b) means “and”(as in set (the bits set in) a and b in flags).

Serial port programming : Serial port configuration Communication. Serial port programming 33/43

Configuring the port

Control options:Bnnnnn : bit rateCS8 : character sizePARENB : enable parity bitCSTOPB : stop bits (2 stop bits if set)

CRTSCTS : output hardware flow control (only used if the cablehas all necessary lines. )

CREAD : enable receiverCLOCAL : local connection , no modem or job contol

Serial port programming : Serial port configuration Communication. Serial port programming 34/43

Configuring the port

struct termios options;tcgetattr(fd , &options );

cfsetispeed (&options , B1200);cfsetospeed (&options , B1200);

options.c_cflag |= CLOCAL | CREAD;

options.c_cflag &= ~CSIZE;options.c_cflag &= ~PARENB;options.c_cflag &= ~CSTOPB;options.c_cflag |= CS8; // 8N1

options.c_cflag &= ~CRTSCTS; // No CTS/RTS

tcsetattr(fd , TCSANOW , &options );

Serial port programming : Serial port configuration Communication. Serial port programming 35/43

Configuring the port

Some local options:ISIG : enable job control signals (INTR ,SUSP ,QUIT ,...)ICANON : Everything is stored into a buffer , and

can be edited until a carriage return or line feedECHO : Enable echoing of input characters

Serial port programming : Serial port configuration Communication. Serial port programming 36/43

Configuring the port

Some input and output options:ICRNL : map CR to NL (otherwise a CR input on

the other computer may not terminate input)

IXON : Enable software flow control (outgoing)IXOFF : Enable software flow control (incoming)

INPCK : Enable parity checkIGNPAR : Ignore parity errorsPARMRK : Mark parity errorsISTRIP : Strip parity bits

ONLCR : Map NL to CR NL in output.

Serial port programming : Serial port configuration Communication. Serial port programming 37/43

Interfaces to I/O operations

I raw access using file descriptorsI ssize_t read (int fd, void * buf, size_t nbyte );

I ssize_t write (int fd, const void * buf, size_t nbyte );

I structured access using streamsI FILE * fdopen ( int fd, const char * mode ) ;

I int fclose( FILE *stream );

I int fgetc ( FILE * stream );

I char * fgets ( char *s, int size, FILE * stream );

I int fprintf(FILE *stream, const char *format, ...);

Serial port programming : Serial port I/O Communication. Serial port programming 38/43

Self-study lab

I There is a self-study programming lab on the serial port (soon)on the web page

I Hints:I Start with read() and write()I Prefer the raw access functions when possileI If you need formatted output, use fprintf()I Formatted input is complicated, avoid if possible, use read()I or fgetc() for reading a single characterI Don’t use canonical mode unless reading linewiseI See the serial port lab memo for details

Serial port programming : Conclusion Communication. Serial port programming 39/43

CommunicationSummary

I during development and during operationI easier debuggingI less hard-coded parameters → less reprogrammingI required in the final lab project

I Design a simple protocolI Don’t be alarmed by the serial port programming

Summary Communication. Serial port programming 40/43

Questions?

Summary Communication. Serial port programming 41/43

Flow Control / Handshaking

I Flow controlI Avoiding buffer overflow

I Fast communicationI Small buffers

I Receiver can pause communication if it cannot keep up withthe data flow

I Hardware signalsI Signalling protocol

I Handshaking to establish a connectionI Agreement on data rates, packet type numbers, sequence

numbers, etc. (e.g., SYN-ACK)

Flow control/Handshaking Communication. Serial port programming 42/43

Flow Control / Handshaking (2)

I DTR/DSRI Data Terminal Ready / Data Set ReadyI Used for startup (typically: connection established over a

modem line → DSRI Can be jumpered for direct connections

I RTS/CTS: “Hardware flow control”I Request To Send: Terminal has data to sendI Clear To Send: Receiver ready to receive

I XON/XOFF: “Software flow control”I In band signalling, bad for binary data

Flow control/Handshaking Communication. Serial port programming 43/43

top related