I have two programs, DriverScale.c and scale9.c. DriverScale.c calls scale 9.c which sends a W and a carraige return to the scale which SHOULD return the weight to DriverScale. However scale 9 hangs for at least 10 min, and then finally returns the weight.
Compilation:
ethan@meow:/var/www$ gcc -g -Wall -o DriverScale DriverScale.c scale9.c
scale9.c: In function �scale9':
scale9.c:53:2: warning: implicit declaration of function �cfsetispeed' [-Wimplicit-function-declaration]
cfsetispeed(&options, B9600);
^
scale9.c:54:2: warning: implicit declaration of function �cfsetospeed' [-Wimplicit-function-declaration]
cfsetospeed(&options, B9600);
^
scale9.c:65:2: warning: implicit declaration of function �tcflush' [-Wimplicit-function-declaration]
tcflush(fp1, TCOFLUSH);
^
scale9.c:76:2: warning: implicit declaration of function �tcgetattr' [-Wimplicit-function-declaration]
tcgetattr(fp1, &options); //The tcgetattr function fills the termios structure
^
scale9.c:84:2: warning: implicit declaration of function �tcsetattr' [-Wimplicit-function-declaration]
tcsetattr(fp1, TCSANOW, &options); /* apply the settings */
^
scale9.c:125:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
DriverScale.c
float scale9(void);
#include <stdio.h>
int main(void)
{
float wgt;
printf ("%s", "weight from scale9\n");
wgt = scale9();
printf("%f", wgt);
return 0;
}
scale9.c
Note: All comment lines are on one line in the code.
/*
*Scale9 Write data to Serial Port and Read it
*
*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stropts.h>
#include <asm/termios.h>
#include <unistd.h>
#include <string.h>
//#include <termios.h>
#define MODEMDEVICE "/dev/ttyS0"
//void main(void)
float scale9(void)
{
char buf[8] = " ", buf2[8] = " ";
FILE *fp1, *fp2;
float wgt;
int i;
struct termios options;
fp1 = fopen(MODEMDEVICE, "w+");
fp2 = fopen("/tmp/testfile.txt", "w+");
if(fp2 == NULL)
{
printf("testfile error. \n");
}
if(fp1 == NULL)
{
printf("initiation error. \n");
}
printf(" hellow scale");
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= ~PARENB; // Allow parity to be set
options.c_cflag &= ~CSTOPB; // 1 stop bit
options.c_cflag &= ~CSIZE; // Mask the character size bits
options.c_cflag |= CS7; // 7 bits
tcflush(fp1, TCOFLUSH);
printf(" hellow scale2");
tcgetattr(fp1, &options); //The tcgetattr function fills the termios structure
//you provide with the current serial port configuration.
//After we set the baud rates and enable local mode and serial data receipt,
//we select the new configuration using tcsetattr. The TCSANOW constant specifies
//that all changes should occur immediately without waiting for output data to finish
//sending or input data to finish receiving. There are other constants
//to wait for input and output to finish or to flush the input and output buffers.
tcsetattr(fp1, TCSANOW, &options); /* apply the settings */
printf("scale7");
fprintf(fp1, "%s","W\r\n"); //send the command to the scale
fread(buf, 1, 7,fp1); strip the first character, which is a carriage return
printf("buf\n");
printf("scale7#2");
for (i= 1; i < 7; i++)
buf2 = buf;
printf("%s", buf2);
for (i = 0; i<8; i++)
printf("%c\n", buf);
printf("\n%s#3", buf);
printf("buf2\n");
for (i = 0; i<7; i++)
printf("%c\n", buf2);
printf("buf2\n");
printf("scale7#4");
printf("%s", buf2);
wgt = atof(buf2);
printf("wgt\n");
printf("%f", wgt);
printf("\n%s", buf2);
fclose(fp1);
fwrite(buf, 1, 7,fp2 );
fwrite(buf2, 1, 7,fp2 );
fclose(fp2);
}
TIA