00001
00010 #include "h1.h"
00011
00012
00013 void signal_1( int);
00014 void rs232_clear_exit( int);
00015
00016
00017 int shm_id1, shm_id2;
00018 unsigned char *SHM_config, *SHM_data;
00019 char RS232_DEVICE[20] = "/dev/ttyUSB0";
00020
00026 int rs232(void)
00027 {
00028
00029 printf("rs232 started!\n");
00030
00031
00032 signal(SIGUSR1,signal_1);
00033 signal(SIGINT, rs232_clear_exit);
00034 signal(SIGTERM, rs232_clear_exit);
00035
00036
00037 memset(&SHM_data, 0, SHM_SIZE );
00038 memset(&SHM_config, 0, SHM_SIZE );
00039
00040
00041 int running, rs_connect, first_time, continuos, send1value, new_order;
00042 char reading_mode[128];
00043 int p=getppid();
00044 int n, i, fd;
00045 unsigned char buf;
00046 int n_sensors;
00047 int n_ultrasounds, n_infrared;
00048 int units;
00049
00050
00051 strcpy(reading_mode,"null");
00052 rs_connect=0;
00053 running=0;
00054 new_order=0;
00055
00056
00057 shm_id1 = GetSharedMem("gtk.c");
00058 if(shm_id1 == -1) return -1;
00059
00060 SHM_config = shmat(shm_id1, (void *) 0, 0);
00061 if(SHM_config == (unsigned char *) (-1)) {perror("shmat");exit(1);}
00062
00063
00064
00065 shm_id2 = GetSharedMem("rs232.c");
00066 if(shm_id2 == -1) return -1;
00067
00068 SHM_data = shmat(shm_id2, (void *) 0, 0);
00069 if(SHM_data == (unsigned char *) (-1)) {perror("shmat");exit(1);}
00070
00071
00072
00073
00074 do
00075 {
00076
00077
00078 if (sig_1>0)
00079 {
00080 if (SHM_config[0] == '1') {rs_connect=1; printf("connect!\n");}
00081
00082 else if (SHM_config[0]== '3') {running=1; strcpy(reading_mode,"continuos"); printf("continuos\n");}
00083 else if (SHM_config[0]== '4') {running=1; strcpy(reading_mode,"single_reading"); printf("single_reading\n");}
00084 else if (SHM_config[0]== '5') {running=0; strcpy(reading_mode,"stop"); printf("Stop\n");}
00085 else if (SHM_config[0]== '6') {running=1; strcpy(reading_mode,"get_reading"); printf("get_reading\n");}
00086 new_order=1;
00087 sig_1=0;
00088 }
00089
00090
00091 if (rs_connect==1)
00092 {
00093
00094 printf("trying to connect using : %s\n", RS232_DEVICE);
00095 fd=open(RS232_DEVICE, O_RDWR);
00096 if(fd==-1) {printf("Opening error!\n"); sprintf(SHM_config, "x\0"); rs_connect=0; kill(getppid(), SIGUSR2); continue;}
00097
00098 struct termios options;
00099 printf("Setting Parameters...\n");
00100 tcgetattr(fd, &options);
00101 cfsetispeed(&options, B9600);
00102 cfsetospeed(&options, B9600);
00103
00104
00105 options.c_cflag |= (CLOCAL | CREAD);
00106 options.c_cflag &= ~PARENB;
00107 options.c_cflag &= ~CSTOPB;
00108 options.c_cflag &= ~CSIZE;
00109 options.c_cflag |= CS8;
00110 options.c_cflag &= ~CRTSCTS;
00111
00112
00113 options.c_lflag &= ~(ICANON | ECHO | ISIG);
00114 options.c_cc[VMIN]=0;
00115 options.c_cc[VTIME]=10;
00116
00117 tcsetattr(fd, TCSANOW, &options);
00118
00119
00120 rs_connect=0;
00121 printf("Connected!\n");
00122
00123
00124 sprintf(SHM_config, "z\0");
00125 kill(getppid(), SIGUSR2);
00126 }
00127
00128
00129
00130 if (new_order==1)
00131 {
00132 if (strcmp(reading_mode, "continuos")==0) {write(fd, "r", 1); }
00133 else if (strcmp(reading_mode, "single_reading")==0) {write(fd, "1", 1); }
00134 else if (strcmp(reading_mode, "get_reading")==0) {write(fd, "1", 1); }
00135 else if (strcmp(reading_mode, "stop")==0) {write(fd, "s", 1); }
00136 new_order=0;
00137 }
00138
00139
00140 if (running==1)
00141 {
00142 n=read(fd, &buf, 1);
00143
00144 if (n<0) { continue;}
00145 if (n>0)
00146 {
00147 if (buf==0xff)
00148 {
00149
00150 read(fd, &buf, 1);
00151 n_ultrasounds=buf & 7;
00152 n_infrared=(buf>>3) & 7 ;
00153 units=(buf>>6);
00154 n_sensors=n_ultrasounds + n_infrared;
00155
00156 SHM_data[0]=n_sensors;
00157 SHM_data[1]=n_ultrasounds;
00158 SHM_data[2]=n_infrared;
00159 SHM_data[3]=units;
00160
00161 for (i=4; i<(n_sensors+4); i++)
00162 {
00163 read(fd, &buf, 1);
00164 if (buf==0xff) continue;
00165 SHM_data[i] = buf;
00166 }
00167
00168
00169
00170
00171
00172
00173
00174
00175 kill(getppid(), SIGUSR2);
00176
00177 if (strcmp(reading_mode, "single_reading")==0){running=0; }
00178 if (strcmp(reading_mode, "get_reading")==0) {running=0; }
00179 }
00180 else
00181 {printf("Wainting for start byte!\n");}
00182 }
00183 }
00184 } while(getppid()==p);
00185
00186 close (fd);
00187
00188
00189 if(shmdt(SHM_config) == -1){perror("shmdt"); exit(1);}
00190
00191 if(shm_id1 > 0) shmctl(shm_id1, IPC_RMID, NULL);
00192
00193
00194 if(shmdt(SHM_data) == -1){perror("shmdt"); exit(1);}
00195
00196 if(shm_id2 > 0) shmctl(shm_id2, IPC_RMID, NULL);
00197
00198 return 0;
00199 }
00200
00201
00207 void signal_1(int x)
00208 {
00209
00210 if (SHM_config[0]=='7') {strcpy(RS232_DEVICE,"/dev/ttyUSB0"); return;}
00211 else if (SHM_config[0]=='8') {strcpy(RS232_DEVICE,"/dev/ttyUSB1"); ;return;}
00212 else if (SHM_config[0]=='9') {strcpy(RS232_DEVICE,"/dev/ttyUSB2");return;}
00213
00214
00215 else { sig_1++;}
00216 }
00217
00218
00224 void rs232_clear_exit(int xx)
00225 {
00226
00227 if(shmdt(SHM_config) == -1){perror("shmdt"); exit(1);}
00228
00229 if(shm_id1 > 0) shmctl(shm_id1, IPC_RMID, NULL);
00230
00231
00232 if(shmdt(SHM_data) == -1){perror("shmdt"); exit(1);}
00233
00234 if(shm_id2 > 0) shmctl(shm_id2, IPC_RMID, NULL);;
00235
00236 exit(0);
00237 }