hello a tous !
voila je fait un programme sous visual c++ qui doit prendre des infos via le port serie. j ai deux ports serie sur mon pc relie entre eux a l aide d un cable. Sous hyperterminal je peut afficher des caracteres sur l un comme sur l autre. bien jusque la.
le probleme etant que je veut verifier si mon programme c++ dialogue bien avec le com2 ou com1 .... je voudrait donc envoyer lors de l excecution de mon programme un mot du genre "Hello" ou "ENFIN OUI SA MARCHE"
mon programme est faut au niveau du management de la liason serie et j ai regarder plusieurs post la dessus mais j arrive pas a comprendre tous....
mon programme c ++ :
#include "automatique.h"
#include "simu_finlande.h"
#define DT (2.0)
#define PATH "C:\\simulator_profile\\profil_swarm_conductivity.txt"
double param_phys[10];
double etat_systeme[3];
double Temperature, Conductivity, Salinity;
double RHO;
double Table_profile[78][4];
FILE* profil_fd;
int lis_parametres_flotteur(char* config_file);
#ifdef WIN32 //allow Gettimeofday fonction to works.
#include <time.h>
#include <sys/timeb.h>
int gettimeofday (struct timeval *tp, void *tz)
{
struct _timeb timebuffer;
_ftime (&timebuffer);
tp->tv_sec = timebuffer.time;
tp->tv_usec = timebuffer.millitm * 1000;
return 0;
}
#endif
float noisify(){
float noise;
noise = (int)(11.0*rand()/(RAND_MAX + 1.0))-5.0;
if (noise == 0)
noise = 1;
// noise = 3.0e-2 * fabs(noise)/noise;
noise = 0.0;
return noise;
}
int main(int argc, char *argv[]) {
//int serial_fd;
FILE* log_fd;
struct timeval tv,tv1;
/* initialisation de la seed pour le générateur de nombres aléatoires */
printf("hello");
srand((unsigned)time(NULL));
log_fd = fopen("log", "w+");
// serial_fd = open_port("/dev/ttyS0");
printf("Port ouvert\n");
lis_parametres_flotteur("config_flotteur");
printf("Config lue\n");
/*reading of the profile which simulate the environment*/
profil_fd=fopen(PATH, "r");
if (profil_fd == NULL) {
printf("no profile in the folder\n");
}
read_profile();
/*initialisation*/
etat_systeme[0]=1.5; /*simulate the position of the sensor in the bottom of the float*/
etat_systeme[1]=0;
//initialize temperature, conductivity and salinity
read_sensors(etat_systeme[0]);
RHO = density_computing(etat_systeme[0], Temperature, Salinity);
printf("init %g %g %g %g %g\n\n", etat_systeme[0], Temperature, Conductivity, Salinity, RHO);
gettimeofday(&tv, NULL);
gettimeofday(&tv1, NULL);
while(1) {
int buf_size = 0;
int nb_read = 0;
float* volume;
unsigned char from_board[6];
unsigned char from_board2[4];
unsigned char to_board[32];
float z_be;
float z_le;
unsigned char* pz_be;
unsigned char* pz_le;
unsigned char* answer_ptr = from_board;
float z_noise;
pz_be = (unsigned char *)&z_be;
pz_le = (unsigned char *)&z_le;
/* while ((nb_read = read(serial_fd, answer_ptr, 6 - buf_size)) > 0){
buf_size += nb_read;
answer_ptr += nb_read;
printf("POUET %d\n", nb_read);
if(buf_size >= 6)
break;
}
if(buf_size != 6)
continue;*/
printf("from board %x%x%x%x%x%x\n", from_board[0], from_board[1], from_board[2], from_board[3],
from_board[4], from_board[5]);
from_board2[0] = from_board[1];
from_board2[1] = from_board[2];
from_board2[2] = from_board[3];
from_board2[3] = from_board[4];
volume=(float *)from_board2;
etat_systeme[2]=(double)*volume;
printf("VOLUME: %g\n", etat_systeme[2]);
merson(etat_systeme[2], 0.0, DT, Temperature);
z_noise = noisify();
z_le = (float)((etat_systeme[0] + z_noise)/(10.0));
// conversion en big endian
//for(buf_size=3; buf_size>=0; buf_size--)
// *(pz_be+buf_size)=*(pz_le+3-buf_size);
/*update value of temperature et conductivity*/
read_sensors(etat_systeme[0]);
//sprintf(to_board,"$AQCTD,12.000,0%2.3f,10.000*ff\n",fabs(z_le+1));
sprintf(to_board,"$AQCTD,%2.3f,0%2.3f,%2.3f*ff\n",Temperature, fabs(z_le+1), Conductivity);
//write(serial_fd, to_board, 32);
gettimeofday(&tv, NULL);
printf("delay between 2 interrupts : %g\n", (tv.tv_sec - tv1.tv_sec +
(tv.tv_usec-tv1.tv_usec)*1.0e-6));
printf("%s", to_board);
printf("%g %g %g %g %g %g %g %g\n\n", etat_systeme[0], z_le, etat_systeme[1], etat_systeme[2], Temperature,
Conductivity, Salinity, RHO);
fprintf(log_fd, "%g %g %g %g %d.%d %g %g %g %g %x%x%x%x%x%x\n", etat_systeme[0],
etat_systeme[1], etat_systeme[2], z_noise, tv.tv_sec, tv.tv_usec,
Temperature, Conductivity, Salinity, RHO,
from_board[0], from_board[1], from_board[2], from_board[3],from_board[4], from_board[5]);
fflush(log_fd);
tv1.tv_sec=tv.tv_sec;
tv1.tv_usec=tv.tv_usec;
}
exit(0);
}
int lis_parametres_flotteur(char* config_file){
int i = 0;
double valeur_lue;
FILE* config_fd;
config_fd = fopen(config_file, "r");
if (config_fd == NULL){
exit(1);
}
while(fscanf(config_fd, "%lf", &valeur_lue) == 1) {
param_phys[i]=valeur_lue;
if(i>10){
exit(1);
}
i++;
}
return EXIT_SUCCESS;
}
void read_profile(void)
{
int i;
double value;
for (i = 0; i<78; i++)
{
if (fscanf(profil_fd, "%lf\n", &value) == 1){
Table_profile[i][0] = value;
}
if (fscanf(profil_fd, "%lf\n", &value) == 1){
Table_profile[i][1] = value;
}
if (fscanf(profil_fd, "%lf\n", &value) == 1){
Table_profile[i][2] = value;
}
if (fscanf(profil_fd, "%lf\n", &value) == 1){
Table_profile[i][3] = value;
}
}
}
void read_sensors(double depth)
{
int i = 0;
//double delta;
while(Table_profile[i][0] < depth){
i++;
}
Temperature = Table_profile[i][1];
Conductivity = Table_profile[i][2];
Salinity = Table_profile[i][3];
}
bien sure me direz vous : // serial_fd = open_port("/dev/ttyS0"); ??????
le programme original tournait sous linux et c est a moi de le rendre compatible Windows... il me semble que windows ouvre les ports serie par default...
j essaye de chercher par moi meme mais je rame beaucoup la et j ai essaye beacup de trucs...
je suis perdus et au bord de la crise aider moiiiiii :)
merci a tous!