Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. function menuprinc
  2. bg = [0 0 0];
  3. figure('color', bg, 'name', 'Interface Homme-Machine', 'units', 'pixels', 'numbertitle', 'off', 'tag', 'figureMain', 'Position',[550 200 800 600]);
  4.  
  5.  
  6. %Menu principal
  7. uicontrol('style', 'text', 'units', 'normalized', 'position', [0.35 0.7 0.35 0.18], 'ForegroundColor', [1 1 1], 'backgroundColor', bg, 'string', 'Menu', 'fontSize', 52, 'tag', 'title');
  8. uicontrol('units', 'normalized', 'position', [0.7 0.5 0.2 0.125], 'string', 'Identification', 'BackgroundColor', [1 1 1], 'callback', @goToInterface, 'fontSize', 12, 'tag', 'btInterface');
  9. uicontrol('units', 'normalized', 'position', [0.11 0.5 0.2 0.125], 'string', 'Commande', 'BackgroundColor', [1 1 1], 'callback', @goToCommande, 'fontSize', 12, 'tag', 'btCommande');
  10. uicontrol('units', 'normalized', 'position', [0.37 0.3 0.26 0.125], 'string', 'Tester connection', 'BackgroundColor', [1 1 1], 'callback', @goToTester, 'fontSize', 12, 'tag', 'btTestConn');
  11.  
  12. uicontrol('style', 'text', 'units', 'normalized', 'position', [0.22 0.07 0.35 0.1], 'ForegroundColor', [1 1 1], 'backgroundColor', bg, 'string', 'N. Port USB', 'fontSize', 18, 'tag', 'textUSB');
  13. uicontrol('style', 'edit', 'units', 'normalized', 'position', [0.55 0.1 0.1 0.07], 'ForegroundColor', [1 1 1], 'backgroundColor', bg, 'string', '0', 'fontSize', 16, 'tag', 'editUSB');
  14.  
  15. %Interface
  16. uicontrol('units', 'normalized', 'position', [0.7 0.1 0.2 0.125], 'string', 'Menu', 'BackgroundColor', [1 1 1], 'callback', @goToMenu, 'fontSize', 12, 'tag', 'btMenu', 'visible', 'off');
  17. uicontrol('units', 'normalized', 'position', [0.7 0.5 0.2 0.125], 'string', 'Draw', 'BackgroundColor', [1 1 1], 'callback', @draw, 'fontSize', 12, 'tag', 'btMenu', 'visible', 'off');
  18. uicontrol('units', 'normalized', 'position', [0.7 0.3 0.2 0.125], 'string', 'receive', 'BackgroundColor', [1 1 1], 'callback', @receive, 'fontSize', 12, 'tag', 'btMenu', 'visible', 'off');
  19.  
  20.  
  21. axes('Position',[0.1 0.1 0.5 0.5], 'color', [1 1 1], 'tag', 'axesInterface', 'visible', 'off');
  22.  
  23. data = guihandles(gcf);
  24. guidata(gcf, data);
  25.  
  26.  
  27. function goToInterface(obj, event)
  28. data = guidata(gcbf);
  29.  
  30. %hide old data
  31. set(data.btInterface, 'visible', 'off');
  32. set(data.btCommande, 'visible', 'off');
  33. set(data.btTestConn, 'visible', 'off');
  34. set(data.textUSB, 'visible', 'off');
  35. set(data.editUSB, 'visible', 'off');
  36.  
  37. %show new data
  38. set(data.btMenu, 'visible', 'on');
  39. set(data.axesInterface, 'visible', 'on');
  40. set(data.title, 'string', 'Interface');
  41. guidata(gcbf, data);
  42.  
  43.  
  44.  
  45. function goToMenu(obj, event)
  46. data = guidata(gcbf);
  47.  
  48.  
  49. %hide old data
  50. set(data.btMenu, 'visible', 'off');
  51. set(data.axesInterface, 'visible', 'off');
  52. set(data.plotidentif, 'visible', 'off')
  53.  
  54. %show new data
  55. set(data.btInterface, 'visible', 'on');
  56. set(data.textUSB, 'visible', 'on');
  57. set(data.editUSB, 'visible', 'on');
  58. set(data.btCommande, 'visible', 'on');
  59. set(data.btTestConn, 'visible', 'on');
  60. set(data.title, 'string', 'Menu');
  61.  
  62.  
  63. function draw(obj, event)
  64. data = guidata(gcbf);
  65.  
  66. data.plotidentif = plot(rand(3));
  67. guidata(gcbf,data);
  68.  
  69.  
  70.  
  71. function goToTester(obj, event)
  72.  
  73. data = guidata(gcbf);
  74.  
  75. % Définition du port série
  76. comUSB = get(data.editUSB,'String');
  77. port = sprintf('COM%s', comUSB);
  78.  
  79. s = serial(port); % Création de l'objet « s » pour y placer COM1
  80.  
  81. set(s,'Terminator',''); % Changement du paramètre Terminator
  82. set(s,'InputBufferSize',8); %
  83. %set(s,'OutputBufferSize',10); %
  84. set(s,'Baudrate',19200); %
  85. set(s,'Timeout',2); %
  86.  
  87.  
  88. Error_flag=0;
  89.  
  90. % Pb de warning...
  91. warning off all
  92.  
  93. fclose(instrfind);
  94.  
  95. fopen(s);
  96.  
  97. % Envoi d'une trame STX LNG RACK CHECKSUM
  98.  
  99. fprintf(s,2);
  100. fprintf(s,1);
  101. fprintf(s,16);
  102. fprintf(s,1);
  103.  
  104. % Réception d'une trame
  105. [trame_recue,count,msg]=fscanf(s);
  106. if(strcmp(msg, 'A timeout occurred.'))
  107. Error_flag=1;
  108. msgbox(sprintf('Connection time out: are you sure COM%s is the correct Port?', comUSB), 'Time out', 'error');
  109. end
  110.  
  111. % Vérification de la trame d'acquittement STX LNG ACK CHECKSUM
  112. Trame_type='02018001';
  113. % Error_flag=0;
  114. for i=1:count
  115. if trame_recue(i)~=Trame_type(i)
  116. msgbox('erreur de trame', 'Trame mechante', 'error');
  117. Error_flag=1;
  118. end
  119. end
  120.  
  121.  
  122. % Message
  123. if Error_flag == 0
  124. %disp('dsPIC33 OK !');
  125. set(data.btTestConn, 'BackgroundColor', [0 1 0]);
  126.  
  127. data.portVerifie = port;
  128.  
  129. else
  130. %disp('Pb de comm avec le dsPIC33 !');
  131. set(data.btTestConn, 'BackgroundColor', [1 0 0])
  132. end
  133.  
  134. % Serial port closing
  135. fclose(s);
  136.  
  137. guidata(gcbf,data);
  138.  
  139. function receive(obj, event)
  140.  
  141. data = guidata(gcbf);
  142. s = serial(data.port); % Création de l'objet « s » pour y placer COM1
  143.  
  144. set(s,'Terminator',''); % Changement du paramètre Terminator
  145. set(s,'InputBufferSize',8);
  146. %set(s,'OutputBufferSize',10);
  147. set(s,'Baudrate',19200);
  148. set(s,'Timeout',2);
  149.  
  150. Error_flag=0;
  151.  
  152. % Pb de warning...
  153. warning off all
  154.  
  155. fclose(instrfind);
  156.  
  157. fopen(s);
  158. for i = 1:11
  159. % Envoi d'une trame STX LNG RACK CHECKSUM
  160. fprintf(s,2);
  161. fprintf(s,1);
  162. fprintf(s,16);
  163. fprintf(s,1);
  164. % Réception d'une trame
  165. [trame_recue,count,msg]=fscanf(s);
  166. if(strcmp(msg, 'A timeout occurred.'))
  167. Error_flag=1;
  168. msgbox(sprintf('Connection time out: are you sure COM%s is the correct Port?', comUSB), 'Time out', 'error');
  169. end
  170.  
  171. % Vérification de la trame d'acquittement STX LNG ACK CHECKSUM
  172. Trame_type='02018001';
  173. % Error_flag=0;
  174. for i=1:count
  175. if trame_recue(i)~=Trame_type(i)
  176. msgbox('erreur de trame', 'Trame mechante', 'error');
  177. Error_flag=1;
  178. end
  179. end
  180.  
  181. %%% recepetion donnees
  182. set(s,'InputBufferSize', 261);
  183.  
  184. % % % % % % Envoi d'une trame STX LNG CMD CHECKSUM
  185. fprintf(s,2);
  186. fprintf(s,1);
  187. fpritf(s,208);
  188. fprintf(s,1);
  189. % % % % %
  190. % % % % %
  191. if(strcmp(msg, 'A timeout occurred.'))
  192. Error_flag=1;
  193. msgbox('Connection time out.', 'Time out', 'error');
  194. end
  195. entree=[];
  196. % Réception d'une trame Réponse à la requête d’échange de données STX LNG DATA ... DATA CHECKSUM
  197. [A,count,msg]=fscanf(s);
  198. entree=[entree traiterTrame(A)];
  199. end
  200. % Envoi d'une trame STX LNG RACK CHECKSUM
  201. fprintf(s,2);
  202. fprintf(s,1);
  203. fprintf(s,16);
  204. fprintf(s,1);
  205. % Réception d'une trame
  206. [trame_recue,count,msg]=fscanf(s);
  207. if(strcmp(msg, 'A timeout occurred.'))
  208. Error_flag=1;
  209. msgbox(sprintf('Connection time out: are you sure COM%s is the correct Port?', comUSB), 'Time out', 'error');
  210. end
  211.  
  212. % Vérification de la trame d'acquittement STX LNG ACK CHECKSUM
  213. Trame_type='02018001';
  214. % Error_flag=0;
  215. for i=1:count
  216. if trame_recue(i)~=Trame_type(i)
  217. msgbox('erreur de trame', 'Trame mechante', 'error');
  218. Error_flag=1;
  219. end
  220. end
  221.  
  222. %%% recepetion donnees
  223. set(s,'InputBufferSize', 201);
  224.  
  225. % % % % % % Envoi d'une trame STX LNG CMD CHECKSUM
  226. fprintf(s,2);
  227. fprintf(s,1);
  228. fpritf(s,208);
  229. fprintf(s,1);
  230. % % % % %
  231. % % % % %
  232. if(strcmp(msg, 'A timeout occurred.'))
  233. Error_flag=1;
  234. msgbox('Connection time out.', 'Time out', 'error');
  235. end
  236. entree=[];
  237. % Réception d'une trame Réponse à la requête d’échange de données STX LNG DATA ... DATA CHECKSUM
  238. [A,count,msg]=fscanf(s);
  239. entree=[entree traiterTrame(A)];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement