J'ai seulement ca :
Je ne vois pas comment faire ensuite...Code:
1
2
3
4 buf=[mot_commande + mot_commande2 + Pos_mem_faible + Pos_mem_fort + Registre_faible + Registre_fort] [CRC16hi,CRC16lo]=CRC16(buf) fwrite(s,[buf + CRC16lo + CRC16hi])
Version imprimable
J'ai seulement ca :
Je ne vois pas comment faire ensuite...Code:
1
2
3
4 buf=[mot_commande + mot_commande2 + Pos_mem_faible + Pos_mem_fort + Registre_faible + Registre_fort] [CRC16hi,CRC16lo]=CRC16(buf) fwrite(s,[buf + CRC16lo + CRC16hi])
Quelque chose comme ceci ?
Code:
1
2
3
4
5 buf = mot_commande + mot_commande2 + Pos_mem_faible + Pos_mem_fort + Registre_faible + Registre_fort crc = CRC16().calculate(buf) ser.write(buf+crc)
Mais encore une fois, tout dépend du type des variables dans le code…
Oui quelque chose comme cela.
Mes variables en python, si je ne me suis pas trompé , sont :En matlab c'était comme cela :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13 code_ascii_W=87 code_ascii_R=82 pos_mem_faible=100 pos_mem_fort=00 nb_registre_faible=1 nb_registre_fort=00 poids_valeur1_1=1 poids_valeur1_2=00 poids_valeur1_3=00 poids_valeur1_4=00
Code:
1
2
3
4
5
6 code_ascii_W=hex2dec('57'); code_ascii_R=hex2dec('52'); pos_mem_faible=hex2dec('64'); pos_mem_fort=hex2dec('00'); nb_registre_faible=hex2dec('01'); nb_registre_fort=hex2dec('00');
Et je ne comprends pas pourquoi ça dépend des variables ?
Cela ne dépend pas des variables mais de leurs types
Et cela va changer quoi dans la ligne de code ? Je crois que leurs types sont des chaînes de caractères.
Essaie comme ceci :
Code:
1
2
3
4
5
6
7 buf = '{}{}{}{}{}{}'.format(mot_commande, mot_commande2, pos_mem_faible, pos_mem_fort, nb_registre_faible, nb_registre_fort) crc = CRC16().calculate(buf) buf = buf + crc ser.write(buf.encode())
Et si ça ne fonctionne pas, montre nous le code Python que tu as écris.
Bon j'ai encore des soucis dans mon code... Voilà ce que j'ai écrit :
Je sais pas ou est mon erreur ..Code:
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 def initialisationtest(s) : from PyCRC.CRC16 import CRC16 import array result=0 mot_commande=82 mot_commande2=68 Pos_mem_faible=100 Pos_mem_fort=00 Registre_faible=1 Registre_fort=00 buf = '{}{}{}{}{}{}'.format(mot_commande, mot_commande2, Pos_mem_faible, Pos_mem_fort, Registre_faible, Registre_fort) crc = CRC16().calculate(buf) buf = buf + crc s.write(buf.encode()) for i in range[1,11]: a = array.array("L") # L is the typecode for uint32 a.fromfile(s, 1) if i==6: test=a.fromfile(s, 1) if test==1: result=1
Merci d'avance !
Si ca peut vous aider à comprendre je met le programme matlab de base aussi :
Code:
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 % TASK: Check the correct initialization. % INPUT % s: serial port associated to the hand. % OUTPUT % result: 1 if ok, 0 if not function [result] = initialisationtest(s) result=0; % Creation of the command string mot_commande=hex2dec('52'); %<--- R mot_commande2=hex2dec('44'); %<--- D Pos_mem_faible=hex2dec('64'); Pos_mem_fort=hex2dec('00'); Registre_faible=hex2dec('01');%<--- poids faible du nombre de registre Registre_fort=hex2dec('00');%<--- poids fort du nombre de registre % CRC16 computation buf=[mot_commande,mot_commande2,Pos_mem_faible,Pos_mem_fort,Registre_faible,Registre_fort]; [crc16hi,crc16lo]=CRC16(buf); % Write the command on the hand fwrite(s,[buf,crc16lo,crc16hi]); % Read the responce: the useful data are contained at sixth position of the % command string. If the read data is 1, it means that the hand is % correctly initialised for i=1:11 fread(s,1); if i==6 test=fread(s,1); end end if test==1 result=1; end end
Je sais d'où viens le problème, c'est le CRC16 qui ne fonctionne pas. En effet le programme matlab utilise un autre code :
Il faut donc le traduire en python et l'incrémenter dans le code mais je suis incapable de le faire, je ne connais pas assez bien python pour ca... Pourriez-vous le traduire si ce n'est pas trop long ?Code:
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 % TASK: compute the CRC16 in order to check the good communication % INPUT % buf: the string of commands containing [code_command_1, code_command_2, % least_significant_byte_memory_position, most_significant_byte_memory_position, % least_significant_byte_register_number,most_significant_byte_register_number, % 01,00,00,00, least_significant_data, most_significant_data, 00, 00] function [crc16hi, crc16lo] = CRC16(buf) crc16hi = uint8(255); crc16lo = uint8(255); len = length(buf); % Length of the table to be tested for byte_index = 1:len crc16lo = bitxor(crc16lo,uint8(buf(byte_index)));%<---CRC16 low xor la % valeur du tableur variant de 1 à len on ne le fait qu'avec le low car % les mots que l'on compart avec CRC16 ne fonts que 8 bits for bit_index = 1:8 test1 = bitand(crc16hi,1);%<---prend la valeur 0 ou 1 en fonction de si le dernier bit de CRC16 soit 1 ou 0 test2 = bitand(crc16lo,1);%<---Et bit à bit entre CRC16lo et 1 crc16lo = uint8(fix(double(crc16lo)/2));%<---retrecissement du mot de 1bit crc16hi = uint8(fix(double(crc16hi)/2));%<---retrecissement du mot de 1bit if test1 %<--- comme on a coupé en deux il y à une correction à faire entre les deux mot si CRC16 high finissait par un 1 crc16lo = bitor(crc16lo,128);%<---si crc16hi avait un 1 au LSB il est réinjecter sur crc16lo end if test2 %<--- condition de modification du mot (parité) crc16lo = bitxor(crc16lo,uint8(1));%<---principe du crc16 : X^16+X^14+X (partie 01 en hexa) crc16hi = bitxor(crc16hi,uint8(160));%<---principe de ce CRC : X^16+X^14+X (partie A0 en hexa) end end end return
Merci d'avance !!