Difference between revisions of "C programming notes"
From Wiki at Neela Nurseries
m (Add section regarding serial port programming.) |
m (→^ Serial Port Use in C: - add slightly modified serial example code from The Linux Documentation Project pages.) |
||
Line 9: | Line 9: | ||
* https://tldp.org/HOWTO/Serial-Programming-HOWTO/ | * https://tldp.org/HOWTO/Serial-Programming-HOWTO/ | ||
* https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html | * https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html | ||
+ | |||
+ | <pre> | ||
+ | // Example 3.2 code from https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html | ||
+ | |||
+ | #include <sys/types.h> | ||
+ | #include <sys/stat.h> | ||
+ | #include <fcntl.h> | ||
+ | #include <termios.h> | ||
+ | #include <stdio.h> | ||
+ | |||
+ | #include <stdlib.h> //echo "z" >> /dev/ttyS1 to provide exit() | ||
+ | |||
+ | // https://man7.org/linux/man-pages/man3/bzero.3.html | ||
+ | #include <strings.h> // to provide bzero() | ||
+ | |||
+ | // https://man7.org/linux/man-pages/man2/read.2.html | ||
+ | // https://linux.die.net/man/3/read | ||
+ | #include <unistd.h> // to provide read() | ||
+ | |||
+ | |||
+ | |||
+ | #define BAUDRATE B38400 | ||
+ | //#define MODEMDEVICE "/dev/ttyS1" | ||
+ | #define MODEMDEVICE "/dev/ttyUSB0" | ||
+ | #define _POSIX_SOURCE 1 /* POSIX compliant source */ | ||
+ | #define FALSE 0 | ||
+ | #define TRUE 1 | ||
+ | |||
+ | volatile int STOP=FALSE; | ||
+ | |||
+ | int main(void) | ||
+ | { | ||
+ | int fd, c, res; | ||
+ | struct termios oldtio,newtio; | ||
+ | char buf[255]; | ||
+ | |||
+ | printf("Starting,\nopening serial device '%s' . . .\n", MODEMDEVICE); | ||
+ | |||
+ | fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); | ||
+ | if (fd <0) {perror(MODEMDEVICE); exit(-1); } | ||
+ | |||
+ | tcgetattr(fd,&oldtio); /* save current port settings */ | ||
+ | |||
+ | bzero(&newtio, sizeof(newtio)); | ||
+ | // newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; | ||
+ | newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; | ||
+ | newtio.c_iflag = IGNPAR; | ||
+ | newtio.c_oflag = 0; | ||
+ | |||
+ | /* set input mode (non-canonical, no echo,...) */ | ||
+ | newtio.c_lflag = 0; | ||
+ | |||
+ | newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ | ||
+ | newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received */ | ||
+ | |||
+ | tcflush(fd, TCIFLUSH); | ||
+ | tcsetattr(fd,TCSANOW,&newtio); | ||
+ | |||
+ | |||
+ | printf("looping to read from serial port . . .\n"); | ||
+ | while (STOP==FALSE) { /* loop for input */ | ||
+ | res = read(fd,buf,255); /* returns after 5 chars have been input */ | ||
+ | buf[res]=0; /* so we can printf... */ | ||
+ | printf(":%s:%d\n", buf, res); | ||
+ | if (buf[0]=='z') STOP=TRUE; | ||
+ | } | ||
+ | tcsetattr(fd,TCSANOW,&oldtio); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </pre> | ||
<!-- comentario --> | <!-- comentario --> | ||
+ | |||
+ | == [[#top|^]] Co-routines == | ||
Interesting article by Simon Tatham on co-routines and a couple ways of implementing them in C language; accessible good style of writing: | Interesting article by Simon Tatham on co-routines and a couple ways of implementing them in C language; accessible good style of writing: |
Revision as of 20:11, 8 September 2022
Contents
^ Overview
^ Serial Port Use in C
- https://tldp.org/HOWTO/Serial-Programming-HOWTO/
- https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html
// Example 3.2 code from https://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> #include <stdlib.h> //echo "z" >> /dev/ttyS1 to provide exit() // https://man7.org/linux/man-pages/man3/bzero.3.html #include <strings.h> // to provide bzero() // https://man7.org/linux/man-pages/man2/read.2.html // https://linux.die.net/man/3/read #include <unistd.h> // to provide read() #define BAUDRATE B38400 //#define MODEMDEVICE "/dev/ttyS1" #define MODEMDEVICE "/dev/ttyUSB0" #define _POSIX_SOURCE 1 /* POSIX compliant source */ #define FALSE 0 #define TRUE 1 volatile int STOP=FALSE; int main(void) { int fd, c, res; struct termios oldtio,newtio; char buf[255]; printf("Starting,\nopening serial device '%s' . . .\n", MODEMDEVICE); fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); if (fd <0) {perror(MODEMDEVICE); exit(-1); } tcgetattr(fd,&oldtio); /* save current port settings */ bzero(&newtio, sizeof(newtio)); // newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD; newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD; newtio.c_iflag = IGNPAR; newtio.c_oflag = 0; /* set input mode (non-canonical, no echo,...) */ newtio.c_lflag = 0; newtio.c_cc[VTIME] = 0; /* inter-character timer unused */ newtio.c_cc[VMIN] = 5; /* blocking read until 5 chars received */ tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); printf("looping to read from serial port . . .\n"); while (STOP==FALSE) { /* loop for input */ res = read(fd,buf,255); /* returns after 5 chars have been input */ buf[res]=0; /* so we can printf... */ printf(":%s:%d\n", buf, res); if (buf[0]=='z') STOP=TRUE; } tcsetattr(fd,TCSANOW,&oldtio); return 0; }
^ Co-routines
Interesting article by Simon Tatham on co-routines and a couple ways of implementing them in C language; accessible good style of writing:
Quote from the article:
- <! -- not intended use for ul tag -->
"Any coding standard which insists on syntactic clarity at the expense of algorithmic clarity should be rewritten." - Simon Tatham
Floating point convert and notes on-line, Javascript converter:
^ C Pointers and dereference syntax
This section the beginning of references, links and personal notes on some of the most difficult and important C language constructs, often used in schedulers and RTOS implementations:
Arrays of strings and arrays of pointers to strings:
^ Simple C examples
Program 1 - use of C comma operator, a binary operator:
#include <stdio.h> #include <stdlib.h> // Compile with: // // $ gcc -Wall main.c // // As of 2022-06-09 Builds with 'gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0' int main(int argc, char *argv[]) { int z = 3; int y[(z++, (z + 1))]; y[0] = 1; if ( z == y[0] ) { } #define SIZE_OF_160_BYTES (160) char lbuf[SIZE_OF_160_BYTES] = { 0 }; snprintf(lbuf, SIZE_OF_160_BYTES, "2022-06-09 main.c test of C comma operator\narray y[] has size of %lu\n", sizeof(y)); printf("%s", lbuf); return 0; }
^ References