File: select1.c

package info (click to toggle)
icomlib 1.0.1-6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,716 kB
  • ctags: 1,363
  • sloc: cpp: 4,848; makefile: 533; sh: 181; ansic: 145
file content (143 lines) | stat: -rw-r--r-- 4,477 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h> /* for the timeval struct */
#include <unistd.h>   /* for the select() stuff */
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>

/* baudrate settings are defined in <asm/termbits.h>, which is
included by <termios.h> */
#define BAUDRATE B9600
/* change this definition for the correct port */
#define PCRDEV "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE; 

main(int argc, char *argv[])
{
        int fd,a,b,c, res;
        struct termios oldtio,newtio;
	char buf[256];
	char buf2[256];
	char buf3[256];

	/* select stuff for a timeout */
	int ok; /* to see if it's ok to read/write */
	fd_set *mainSet;
	fd_set *readSet;
	fd_set *writeSet;
	struct timeval *mainTimeOut;

	mainSet = malloc(sizeof(fd_set));
	readSet = malloc(sizeof(fd_set));
	writeSet = malloc(sizeof(fd_set));
	bzero(mainSet, sizeof(fd_set));
	bzero(readSet, sizeof(fd_set));
	bzero(writeSet, sizeof(fd_set));

	mainTimeOut = malloc(sizeof(struct timeval));
	bzero(mainTimeOut, sizeof(struct timeval));
	/* end select stuff for a timeout */

        /* Open modem device for reading and writing and not as controlling tty
        because we don't want to get killed if linenoise sends CTRL-C.  */
        fd = open(PCRDEV, O_RDWR | O_NOCTTY ); 
        if (fd <0) {perror(PCRDEV); exit(-1); }

        tcgetattr(fd,&oldtio); /* save current serial port settings */
	bzero(&newtio, sizeof(newtio)); 

        /* 
         BAUDRATE: Set bps rate. use cfsetispeed and cfsetospeed.
         CS8     : 8n1 (8bit,no parity,1 stopbit)
         CLOCAL  : local connection, no modem contol
         CREAD   : enable receiving characters
        */
	cfsetispeed( &newtio, BAUDRATE );
	cfsetospeed( &newtio, BAUDRATE );
        newtio.c_cflag |=  (CLOCAL | CREAD);
	newtio.c_cflag &= ~HUPCL ;
	newtio.c_cflag &= ~PARENB ;
	newtio.c_cflag &= ~CSTOPB ;
	newtio.c_cflag &= ~CSIZE;
	newtio.c_cflag |= CS8;
       
        /* ICANON  : enable canonical input disable all echo functionality, 
		  and don't send signals to calling program */
        newtio.c_lflag |= ICANON;
	newtio.c_lflag &= ~(ECHO | ECHOCTL);
 
        /* Raw output.  */
        newtio.c_oflag &= ~OPOST;
       
        /*
         IGNPAR  : ignore bytes with parity errors
         ICRNL   : map CR to NL (otherwise a CR input on the other computer
                   will not terminate input)
          otherwise make device raw (no other input processing)
 	newtio.c_iflag = IGNPAR ;
        */
       
        /* initialize control characters */
        /* newtio.c_cc[VTIME]    = 5; *//* inter-character timer unused */
        /* newtio.c_cc[VMIN]     = 0; *//* blocking read until 1 character arrives */

        /* now clean the modem line and activate the settings for the port */
        /* tcflush(fd, TCIFLUSH); */
        tcsetattr(fd,TCSANOW,&newtio);

	/* here goes the select stuff */
	FD_ZERO(mainSet); 
	FD_ZERO(readSet);
	FD_ZERO(writeSet);
	FD_SET(fd, mainSet);
	FD_SET(fd, readSet);
	FD_SET(fd, writeSet);
	mainTimeOut->tv_sec=5;
	mainTimeOut->tv_usec=0;

	
	
	if (argc > 1) {
		a = 0;
		b = 0;
		strcpy(buf, "G300\x0d\x0a");
		if (select(fd+1, NULL, writeSet, NULL, mainTimeOut)) {
			write( fd, buf, strlen(buf)); 
			fprintf(stderr, "\tA\tWriting ok to %d\n", fd);
    		if (select(fd+1, readSet, NULL, NULL, mainTimeOut)) {
	    		a = read(fd, buf, 0xff);
		    	fprintf(stderr, "\tA\tReading ok to %d\n", fd);
		    	/* second loop for data */
        		strcpy(buf, "G4\?\x0d\x0a");
		        fprintf(stderr, "REQ: %s\n", buf);
        		if (select(fd+1, NULL, writeSet, NULL, mainTimeOut)) {
		        	write( fd, buf, strlen(buf));
        			fprintf(stderr, "\tB\tWriting ok to %d\n", fd);
            		if (select(fd+1, readSet, NULL, NULL, mainTimeOut)) {
                		b = read(fd, buf2, 0xff);
		    	        fprintf(stderr, "\tB\tReading ok to %d\n", fd);
		    	    } else {
		    	        fprintf(stderr, "\tB\tReading timeout %d\n", fd);
		    	    }
		        } else {
        			fprintf(stderr, "\tB\tWriting timeout %d\n", fd);		    	
        		}
    		} else {
	    		fprintf(stderr, "\tA\tReading timeout %d\n", fd);
		    }
		} else {
			fprintf(stderr, "\tA\tWriting timeout %d\n", fd);
		}
		buf[a]=0;buf2[b]=0;
		fprintf(stderr, "\t \tRED: %i,%s %i,%s\n", a,buf,b,buf2);
	}

   /* restore the old port settings */
   tcsetattr(fd,TCSANOW,&oldtio);
}