I was wondering if someone could help me I need to convert a very small C program to FreeBASIC and send the data to a pipe connection (that is set in the ini file of the application)
Code: Select all
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
/*#include <syslog.h> */
#include <string.h>
#ifdef _WIN32
#define O_NOCTTY 0
#else
#include <termios.h> POSIX terminal control definitions */
#endif
#include <ctype.h>
int main(void) {
/* This is used to set the comport etc from the ini file*/
/* Our process ID and Session ID */
pid_t pid, sid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
exit(EXIT_FAILURE);
}
/* If we got a good PID, then
we can exit the parent process. */
if (pid > 0) {
exit(EXIT_SUCCESS);
}
/* Change the file mode mask */
umask(0);
/* Open any logs here */
/* Create a new SID for the child process */
sid = setsid();
if (sid < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Change the current working directory */
if ((chdir("/")) < 0) {
/* Log the failure */
exit(EXIT_FAILURE);
}
/* Close out the standard file descriptors */
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(void)
{
int fd; /* File descriptor for the port */
fd = open("COM3", O_RDWR | O_NOCTTY );
if (fd == -1)
{ /* Could not open the port */
fprintf(stderr, "open_port: Unable to open /dev/ttyS0 - %s\n",
strerror(errno));
}
return (fd);
}
int mainfd=0;
int num, n; /* File descriptor */
char chout[16];
char serialBuffer[21];
char preBuffer[16];
char scannerInput[17];
char scaleInput[9];
char scaleBuffer[10] = "0000000000";
struct termios options;
char serialInput;
mainfd = open_port();
fcntl(mainfd, F_SETFL, FNDELAY); /* Configure port reading */
/* Get the current options for the port */
tcgetattr(mainfd, &options);
cfsetispeed(&options, B9600); /* Set the baud rates to 9600 */
cfsetospeed(&options, B9600);
/* Enable the receiver and set local mode */
options.c_cflag |= (CLOCAL | CREAD);
/* 7 bits, odd parity */
options.c_cflag |= PARENB;
options.c_cflag |= PARODD; /* odd parity */
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS7;
options.c_cflag |= CRTSCTS;
/* Enable data to be processed as raw input */
options.c_lflag &= ~(ICANON | ECHO | ISIG);
/* Set the new options for the port */
tcsetattr(mainfd, TCSANOW, &options);
FILE *fp_scanner;
FILE *fp_scale;
int in_buffer = 0;
int i;
n = 0;
num = 0;
write(mainfd, "S11\r", 5);
write(mainfd, "S14\r", 5);
while (1)
{
in_buffer = read(mainfd, &chout, 1); /* Read character from ABU */
if (in_buffer != -1) { /* if data is present in the serial port buffer */
if (chout[0] == 'S') {
num = 0;
}
serialBuffer[num] = chout[0];
num++;
;
if (chout[0] == '\n' && num > 2) {
serialBuffer[num] = '\0';
/**************** process scanned data ****************/
if (serialBuffer[1] == '0') {
for (i=0; i<17; i++) {
scannerInput[i] = serialBuffer[i+4];
}
fp_scanner = fopen("/ssddata/scanner", "w");
fprintf(fp_scanner, "%s\n", scannerInput);
fclose(fp_scanner);
}
/**************** process weight data ******************/
if (serialBuffer[1] == '1') {
if (serialBuffer[2] == '1') {
write(mainfd, "S14\r", 5);
}
else if (serialBuffer[2] == '4' && serialBuffer[3] == '3') {
write(mainfd, "S11\r", 5);
if (strcmp(scaleBuffer, serialBuffer) != 0) {
fp_scale = fopen("/ssddata/scale", "w");
fprintf(fp_scale, "%s\n", serialBuffer);
fclose(fp_scale);
}
}
else if (serialBuffer[2] == '4') {
write(mainfd, "S14\r", 5);
if (strcmp(scaleBuffer, serialBuffer) != 0) {
fp_scale = fopen("/ssddata/scale", "w");
fprintf(fp_scale, "%s\n", serialBuffer);
fclose(fp_scale);
}
}
for (i=0; i<10; i++) {
scaleBuffer[i] = serialBuffer[i];
}
} /* weight data processing ends */
} /* end of line data processing ends */
} /* non-empty buffer data processing ends */
else if (num > 20){
write(mainfd, "S14\r", 5);
num = 0;
}
in_buffer = -1;
usleep(10);
}
close(mainfd);
exit(EXIT_SUCCESS);
}
if someone know how to do this I would be most grateful as I am not sure. (i have the scanner working as part of my Main Loop application but this slows down and I do not want to have the scanner controlled by the main app anymore) I have done it via Pipe so i can create new "modules" for each scanner that I support (but the examples I have at the moment as almost all the same as the above code and I am not a C programmer)