Program Hangs


 
Thread Tools Search this Thread
Top Forums Programming Program Hangs
# 1  
Old 06-28-2015
Program Hangs

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:

Code:
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

Code:
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.
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[i] = buf[i];
    printf("%s", buf2);
    
    for (i = 0; i<8; i++)
        printf("%c\n", buf[i]);
        
printf("\n%s#3", buf);
    
    printf("buf2\n");

    
    for (i = 0; i<7; i++)
        printf("%c\n", buf2[i]);
  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
# 2  
Old 06-28-2015
You can't modify terminal characteristics until you have determined what your current terminal characteristics are; and getting the characteristics after you have initialized settings you want to put in place wipes out all of the initializations you performed... Try moving the line:
Code:
    tcgetattr(fp1, &options); //The tcgetattr function fills the termios structure

so it comes before the line:
Code:
    cfsetispeed(&options, B9600);

# 3  
Old 06-29-2015
Don -

Thank you.

Hangs and makes no progress.

A few more points to be added -

The program previously ran beautifully from the terminal. Now it eventually gives 0.00 as the result.

If I use minicom, I get the correct number.
# 4  
Old 06-29-2015
You do not have two programs. You have one program that requires compiling two C source files and linking them together into one executable object file.

What is minicom? If it works with minicom what are you using when it fails?

What operating system are you using?

What compiler are you using? What command line are you using to compile and link the various pieces of this program.

So, why is the inclusion of termios.h commented out when you are clearly getting warnings because you haven't included that header? And, why are you including asm/termios.h?

You declare that scale9() will return a floating point value, but there are no return statements in scale9() (so it can't possibly return a value).

There is clearly other code in what you have shown us that would generate compilation errors (not just warnings). (strip the first character, which is a carriage return is not a valid C statement.) Please show us your real code; not code that has been modified to spread comments across multiple lines.

The way you are setting options.c_cflag is clearly wrong, but I'm not sure how to correct it since I'm not sure what you're trying to do. What terminal characteristics does the scale connected to /dev/ttyS0 expect?

Please show us the output you get from the command:
Code:
od -bc < /tmp/testfile.txt

after you run your program.
# 5  
Old 06-29-2015
Don -

Thanks. Incisive answers that provide help are greatly appreciated.

So, why is the inclusion of termios.h commented out .. That is not where
Code:
termios.h

is. It is here ..
Code:
#include <asm/termios.h>

.

If it works with minicom what are you using when it fails? scale9

What operating system are you using? Debian testing.

There are no return statements in scale9() I noticed that and added one, no difference.

not code that has been modified to spread comments across multiple lines. As I said in my post.. Note: All comment lines are on one line in the code.

strip the first character, which is a carriage return is not a valid C statement.
It is a comment.

I do not get any errors in running the program, just warnings on compile.

What terminal characteristics does the scale connected to /dev/ttyS0 expect? 9600 7E1

Please show us the output you get from the command:

Code:
ethan@meow:/var/www$ od -bc < /tmp/testfile.txt
0000000

I hope this answers your questions.

TIA
# 6  
Old 06-29-2015
Quote:
Originally Posted by Meow613
Don -

Thanks. Incisive answers that provide help are greatly appreciated.

So, why is the inclusion of termios.h commented out .. That is not where
Code:
termios.h

is. It is here ..
Code:
#include <asm/termios.h>

.
No! The standard header is <termios.h>. I have no idea what is in <asm/termios.h>, but it clearly isn't what you need or you wouldn't be getting warnings about 5 functions being undeclared that should be declared in <termios.h>. Delete the line including <asm/termios.h> and uncomment the line including <termios.h>.
Quote:
If it works with minicom what are you using when it fails? scale9
OK. So, you're saying that when you run some other code that you haven't shown us, it works. And, when you run the code you have shown us it doesn't work.
Quote:
What operating system are you using? Debian testing.

There are no return statements in scale9() I noticed that and added one, no difference.
What return statement did you add and where did you add it?
Quote:
not code that has been modified to spread comments across multiple lines. As I said in my post.. Note: All comment lines are on one line in the code.

strip the first character, which is a carriage return is not a valid C statement.
It is a comment.
No! In the code you showed us:
Code:
    fread(buf, 1, 7,fp1); strip the first character, which is a carriage return

the text shown in red is not a comment.
Quote:
I do not get any errors in running the program, just warnings on compile.
Then you have now shown us the code you compiled! The code you have shown us will not compile successfully!
Quote:
What terminal characteristics does the scale connected to /dev/ttyS0 expect? 9600 7E1
That doesn't even come close to answering the question.
Quote:
Please show us the output you get from the command:

Code:
ethan@meow:/var/www$ od -bc < /tmp/testfile.txt
0000000

With the fopen() call you used, and the code:
Code:
   fwrite(buf, 1, 7,fp2 );
   fwrite(buf2, 1, 7,fp2 );

, you add 14 bytes to the end of that file every time you run your program. And, you're telling us that you have run your program several times and end up with absolutely no data written to your this file??? So, if your script is running for 10 minutes and then dies without printing anything, something is killing your program before it successfully reads anything from your scale.
Quote:
I hope this answers your questions.

TIA
Besides not writing anything into /tmp/testfile.txt, there are several printf() statements that do not seem to be running. So, the code you have shown us should not compile and if it did compile it should print data to standard output that you say is not appearing. But, since you don't include <newline> characters in most of your output, it may be stuck in a buffer instead of being made visible. And, since we don't know what characteristics you scale needs, we might not be seeing anything because the scale is hanging. And, the line could be dropping after 10 minutes if you don't have those characteristics set appropriately. And, you never check any of your I/O statements for error returns, so we have no indication if something is going wrong.
# 7  
Old 06-30-2015
Delete the line including <asm/termios.h> and uncomment the line including
Code:
<terminos.h>

Did that and received the following:

Code:
scale9.c: In function ‘scale9':
scale9.c:53:2: warning: passing argument 1 of ‘tcsetattr' makes integer from pointer without a cast [enabled by default]
  tcsetattr(fp1, TCSANOW, &options); /* apply the settings */
  ^
In file included from scale9.c:18:0:
/usr/include/termios.h:70:12: note: expected ‘int' but argument is of type ‘struct FILE *'
 extern int tcsetattr (int __fd, int __optional_actions,
            ^
scale9.c:67:2: warning: passing argument 1 of ‘tcflush' makes integer from pointer without a cast [enabled by default]
  tcflush(fp1, TCOFLUSH);
  ^
In file included from scale9.c:18:0:
/usr/include/termios.h:90:12: note: expected ‘int' but argument is of type ‘struct FILE *'
 extern int tcflush (int __fd, int __queue_selector) __THROW;
            ^
scale9.c:78:2: warning: passing argument 1 of ‘tcgetattr' makes integer from pointer without a cast [enabled by default]
  tcgetattr(fp1, &options); //The tcgetattr function fills the termios structure 
  ^
In file included from scale9.c:18:0:
/usr/include/termios.h:66:12: note: expected ‘int' but argument is of type ‘struct FILE *'
 extern int tcgetattr (int __fd, struct termios *__termios_p) __THROW;


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Ftp hangs

On Oracle Linux 5, 64 bit (derivative of RHEL) ... I have a shell script that runs every Sunday, that ftp's a bunch of files from server 'prod' to server 'test'. Script executes on 'test'. This has been running for YEARS with no problem. Normally the FTP step takes about 1.5 to 2 hours. (pulls... (8 Replies)
Discussion started by: edstevens
8 Replies

2. Shell Programming and Scripting

How to display a message if program hangs(takes too long)

I have a ksh script (script1) that calls another ksh script (script2). If script2.ksh hangs or takes too long to execute I want script1.ksh to kill the call to script2.ksh and instead just display "Script2 can't run right now". Could someone help me with coding this? (1 Reply)
Discussion started by: mrskittles99
1 Replies

3. UNIX for Advanced & Expert Users

SFTP. ls and get hangs.

Hello I have a sftp problem. If I do sftp between some nodes that we installed it works fine i can list directories and get files. If I connect with sftp from the other side of a firewall (wich has port 22 open) I can log in and cd to which ever direcory i want i can do pwd but when i do... (4 Replies)
Discussion started by: vettec3
4 Replies

4. Programming

popen hangs program during cmd execution

How can I get around this? when my program reaches the following popen job it halts the program until the ping/netstat/ipconfig/traceroute is completed then resume to the rest of the program... FILE *in; extern FILE *popen(); char buff; char newline; char nstat; char nping; ... (5 Replies)
Discussion started by: Jess83
5 Replies

5. AIX

sysback hangs

I've set sysback to run in the cron daily to backup my servers to a nim servers SAN storage. Every once and a while the backup process hangs and the backup doesn't complete. When I check the processes the sysback processes are still running hours after sysback would normally end. I don't see any... (1 Reply)
Discussion started by: daveisme
1 Replies

6. UNIX for Dummies Questions & Answers

Script to open program and send/execute command in program

Hi, i want to write a script that executes a program (exec?) . this program then requires a filename as input. how do i give it this input in the script so the program will be complete run and close by the script. e.g. exec prog.exe program then asks for filename "enter filename:"... (1 Reply)
Discussion started by: tuathan
1 Replies

7. AIX

telnet hangs

Hi All, When I tried to telnet to lparB from lparA, it hangs at "Escape character is..". I've tried; - killed -15 - re-started it - edit netsvc.conf, hosts=local,bind and hosts=local,bind4 - checked /etc/hosts for 127.0.0.1 loopback localhost - copy original inetd.conf ... (3 Replies)
Discussion started by: fara_aris
3 Replies

8. Programming

after executing execvp()... program hangs up

Hi , I m actually trying to implement pipes program,but after executing the execvp(),my program is getting hanged up :mad: Actaully i m getting the desired output expected from execvp()...but once results are displayed on the output screen ,program is getting hanged up values of... (3 Replies)
Discussion started by: Crab
3 Replies

9. UNIX for Advanced & Expert Users

sendmail -q hangs

Hi, I have a system where sendmail deamon not running. (And I have lot of jobs under crontab). But I have whole lot of sendmail processes started-up by cron process, eating up its CPU resources. (for a sample - see below) $ ps -ef|grep 248 root 248 1 0 Aug 12 ? 0:56... (5 Replies)
Discussion started by: chaandana
5 Replies

10. AIX

AIX Hangs

Hai I am new to AIX, currently in our shop we use AIX 4.3.3 , where we have hosted the websphere 3.5 applicaiton on it. The AIX server is getting hanged very frequently, when analyse we found the swap memory is increasing once it reaches the memory 100 the system is totaly in hung state, so... (5 Replies)
Discussion started by: vipin77
5 Replies
Login or Register to Ask a Question