RS232 / Serial Support

Linux specific questions.
Post Reply
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

RS232 / Serial Support

Post by Gablea »

Hi Everyone,

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);

}
Above is the Code that would get either the barcode number or weight from the attached Scanner / scale but as it is in C I have no idea what I need to change to get it to send data to the the pip called ScannerData (for the barcode number) or the ScaleData (for the Scale weight)

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)
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: RS232 / Serial Support

Post by TJF »

Your code snippet is not complete ( {} brackets mismatch ).
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: RS232 / Serial Support

Post by Gablea »

hi @TJF,

That snipe is how it was sent to me

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("/pos/is4c/rs232/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("/pos/is4c/rs232/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("/pos/is4c/rs232/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);

}
I have re pasted the snipe just in case I missed something.

I appreciate any help anyone can help.
adeyblue
Posts: 300
Joined: Nov 07, 2019 20:08

Re: RS232 / Serial Support

Post by adeyblue »

Gablea wrote:hi @TJF,

That snipe is how it was sent to me
Then I hope you didn't pay for it. It's looks like someone googled the various things you presumably asked for and then just pasted all the random bits they found into one file.
Gablea
Posts: 1104
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: RS232 / Serial Support

Post by Gablea »

@adeyblue

No I did not pay for it It was sent over to me free as it was a Demon that should have worked with the RS232 scanner scales that I have. (and when I complied it on linux and try to run it errors saying the exec is nor formatted correctly)

I have managed to get the first section works (the scanner side) but I still not sure how to get the weight from the scale
Post Reply