Project

General

Profile

grblrw » grblrw.c

Frédéric Blanc, 2024-01-17 11:42

 
/* grblrw.c

Copyright (c) 2023- BLANC Frédéric (LAAS-CNRS)

memrw is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

memrw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with memrw. If not, see <http://www.gnu.org/licenses/>.
*/

// gcc -Wall -Wextra grblrw.c -o grblrw

// C library headers

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <unistd.h> // write(), read(), close()
#include <termios.h> // Contains POSIX terminal control definitions

#define SERIAL_SPEED B115200

int verbose = 0;

int read_grbl(const char *port, const char *filename)
{

int f_save = 0;

FILE *paramFile = NULL;

FILE *serial = fopen(port, "a");

if (serial == NULL)
{
perror("Erreur lors de l'ouverture du port série en lecture");
exit(EXIT_FAILURE);
}
struct termios tty;
int fd = fileno(serial);
if (fd == -1)
{
perror("fileno())");
exit(EXIT_FAILURE);
}
if (tcgetattr(fd, &tty) != 0)
{
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
return 1;
}
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)

tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes

tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
// tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)

tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
cfsetispeed(&tty, SERIAL_SPEED);
cfsetospeed(&tty, SERIAL_SPEED);

if (filename != NULL)
{
paramFile = fopen(filename, "w");
if (paramFile == NULL)
{
perror("Error open file parameter GRBL");
return EXIT_FAILURE;
}
f_save = 1; // flag save file
}
fprintf(serial, "$$\r\n");
printf("$$\r\n");
sleep(1);
int c;
while (1)
{
c = fgetc(serial);
if (f_save == 1)
{
}
printf("%c", c);
}

fclose(serial);
fclose(paramFile);
return 0;
}

int write_grbl(const char *port, const char *filename)
{
char buffer[1024];
int f_save = 0;

FILE *paramFile = NULL;

FILE *serial = fopen(port, "a");

if (serial == NULL)
{
perror("Erreur lors de l'ouverture du port série en lecture");
exit(EXIT_FAILURE);
}
struct termios tty;
int fd = fileno(serial);
if (fd == -1)
{
perror("fileno())");
exit(EXIT_FAILURE);
}
if (tcgetattr(fd, &tty) != 0)
{
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
return 1;
}
tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
tty.c_cflag |= CS8; // 8 bits per byte (most common)
tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)

tty.c_lflag &= ~ICANON;
tty.c_lflag &= ~ECHO; // Disable echo
tty.c_lflag &= ~ECHOE; // Disable erasure
tty.c_lflag &= ~ECHONL; // Disable new-line echo
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes

tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)
// tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)

tty.c_cc[VTIME] = 10; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
tty.c_cc[VMIN] = 0;
cfsetispeed(&tty, SERIAL_SPEED);
cfsetospeed(&tty, SERIAL_SPEED);

if (filename != NULL)
{
paramFile = fopen(filename, "r");
if (paramFile == NULL)
{
perror("Error open file parameter GRBL");
return EXIT_FAILURE;
}
f_save = 1; // flag save file
}

while (fgets(buffer, sizeof(buffer), paramFile) != NULL)
{

fprintf(serial, "%s", buffer);
sleep(1);
}

fclose(serial);
fclose(paramFile);
return 0;
}

int main(int argc, char *argv[])
{
char port[1024];
char filename[1024];
int opt;

while ((opt = getopt(argc, argv, "vp:f:rw")) != -1)
{
switch (opt)
{
case 'v':
printf("verbose\r\n");
verbose = 1;
break;
case 'p':
if (verbose == 1)
{
printf("optarg port:%s\r\n", optarg);
}

strcpy(port, optarg);
break;
case 'f':
if (verbose == 1)
{
printf("optarg filename:%s\r\n", optarg);
}

strcpy(filename, optarg);
break;
case 'r':
if (verbose == 1)
{
printf("optarg port:%s filename:%s\r\n", port, filename);
}

read_grbl(port, filename);
break;
case 'w':
if (verbose == 1)
{
printf("optarg port:%s filename:%s\r\n", port, filename);
}

write_grbl(port, filename);
break;

case '?':
fprintf(stderr, "Usage: %s -r -p serial_port [-f output_file]\r\n", argv[0]);
break;
}
}

return EXIT_SUCCESS;
}
    (1-1/1)