Commit 78a024f7ade45ffd271d72cb1d8081bfbfd29365
1 parent
4ed129b3
Ajustements et clarifications
Showing
3 changed files
with
43 additions
and
29 deletions
Show diff stats
page_web/index.html
... | ... | @@ -0,0 +1,18 @@ |
1 | +/* | |
2 | + * Public definitions for serial library | |
3 | + */ | |
4 | + | |
5 | +//// | |
6 | +// Constants | |
7 | +//// | |
8 | + | |
9 | +#define SERIAL_READ 0 | |
10 | +#define SERIAL_WRITE 1 | |
11 | +#define SERIAL_BOTH 2 | |
12 | + | |
13 | +//// | |
14 | +// Public prototypes | |
15 | +//// | |
16 | +int serialOpen(char *device,int mode); | |
17 | +void serialConfig(int fd,int speed); | |
18 | +void serialClose(int fd); | ... | ... |
serveur/websocket_server.c
... | ... | @@ -38,56 +38,53 @@ static int callback_my( |
38 | 38 | struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, |
39 | 39 | void *user,void *in,size_t len) |
40 | 40 | { |
41 | -printf("boucle\n"); | |
42 | -static char *message=NULL; | |
41 | +static char *mesage=NULL; | |
43 | 42 | static int msize=0; |
44 | 43 | switch(reason) |
45 | 44 | { |
46 | - printf("%s\n",reason); | |
47 | 45 | case LWS_CALLBACK_ESTABLISHED: |
48 | 46 | printf("connection established\n"); |
49 | - message=NULL; | |
47 | + mesage=NULL; | |
50 | 48 | |
51 | 49 | // Declenchement d'un prochain envoi au navigateur |
52 | 50 | libwebsocket_callback_on_writable(this,wsi); |
53 | 51 | break; |
52 | + | |
54 | 53 | case LWS_CALLBACK_RECEIVE: |
55 | 54 | |
56 | - // Ici sont traites les messages envoyes par le navigateur | |
57 | - printf("received data: %s\n",(char *)in); | |
58 | - message=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING); | |
59 | - if(message==NULL) | |
60 | - { | |
55 | + // Ici sont traites les mesages envoyes par le navigateur | |
56 | + char* recei=(char *)in; | |
57 | + int rec = *recei - 48; //réception de l'instruction, convertie en entier | |
58 | + printf("received data: %d\n",rec); | |
59 | + mesage=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING); | |
60 | + if(mesage==NULL) | |
61 | + { | |
61 | 62 | perror("callback_my.malloc"); |
62 | 63 | exit(EXIT_FAILURE); |
63 | - } | |
64 | - | |
65 | - int a=0; | |
66 | - char value[3]; | |
67 | - sprintf (value, "%d", serial_read(&a)) ; | |
68 | - printf("value = %s\n",value); | |
69 | - //char* value = "0"; | |
70 | - // memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,in,len); | |
71 | - memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,value,1 + strlen(value)); | |
64 | + } | |
65 | + char value[3]; | |
66 | + sprintf (value, "%d", (10*serial_read(&rec))+rec); //réception de la valeur demandée, sous forme de str | |
67 | + printf("value = %s\n",value); | |
68 | + memcpy(mesage+LWS_SEND_BUFFER_PRE_PADDING,value,1 + strlen(value)); | |
72 | 69 | |
73 | 70 | // Declenchement d'un prochain envoi au navigateur |
74 | - //msize=len; | |
75 | 71 | msize=strlen(value); |
76 | 72 | libwebsocket_callback_on_writable(this,wsi); |
77 | 73 | break; |
74 | + | |
78 | 75 | case LWS_CALLBACK_SERVER_WRITEABLE: |
79 | - | |
80 | - // Ici sont envoyes les messages au navigateur | |
81 | - if(message!=NULL){ | |
82 | - char *out=message+LWS_SEND_BUFFER_PRE_PADDING; | |
76 | + // Ici sont envoyes les mesages au navigateur | |
77 | + if(mesage!=NULL) | |
78 | + { | |
79 | + char *out=mesage+LWS_SEND_BUFFER_PRE_PADDING; | |
83 | 80 | printf("%s\n",out); |
84 | 81 | printf("%d\n",msize); |
85 | 82 | libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT); |
86 | - //libwebsocket_write(wsi,(unsigned char *)out,msize,LWS_WRITE_TEXT); | |
87 | - free(message); | |
88 | - message=NULL; | |
89 | - } | |
83 | + free(mesage); | |
84 | + mesage=NULL; | |
85 | + } | |
90 | 86 | break; |
87 | + | |
91 | 88 | default: |
92 | 89 | break; |
93 | 90 | } |
... | ... | @@ -160,7 +157,7 @@ int n=0; |
160 | 157 | int value=0; |
161 | 158 | int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); |
162 | 159 | serialConfig(sd,B9600); |
163 | -if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } | |
160 | +if(write(sd,&c,sizeof(char))!=1){ perror("main.write"); exit(-1); } //Envoi de l'instruction | |
164 | 161 | |
165 | 162 | //Read the number of decimals |
166 | 163 | if(read(sd,&n,sizeof(char))!=1){ perror("main.read"); exit(-1); } | ... | ... |