unit device_thread; interface uses Classes, Windows, SysUtils, D2xxUnit, Dialogs, DeviceRD, IzmUnit, DateUtils; type TIzmResult = IzmUnit.TIzmResult; type device_thread_th = class(TThread) public destructor Destroy; override; private { Private declarations } protected result_m: params_record; procedure Execute; override; procedure Get_message; procedure Update; procedure Update_dev_cond_string_getdata; procedure Update_dev_cond_string_ready; procedure Update_dev_cond_string_decode; procedure Update_message_count; procedure Check_and_load_data; end; Var TTH: device_thread_th; num:integer; point,povtor:integer; izm_result: TIzmResult; message_: message_Array; implementation uses MainUnit, ConstUnit, DataUnit; { Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure device_thread.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; } { device_thread } destructor device_thread_th.Destroy; begin inherited; //здесь выполняй все действия, что должны выполниться при разрушении потока, // например пошли sendmessage "родительскому" окну //что поток закончил свою работу (при этом Synchronize не нужна) end; procedure device_thread_th.Execute; begin self.OnTerminate := nil; Get_message; end; procedure device_thread_th.Get_message; var i,j,k:integer; message_begin:boolean; begin for i:=1 to 24 do for j:=1 to 5 do begin izm_result[i,j].R:=0; izm_result[i,j].R_e:=0; izm_result[i,j].G0:=0; izm_result[i,j].G1:=0; izm_result[i,j].G2:=0; izm_result[i,j].G3:=0; izm_result[i,j].C:=0; izm_result[i,j].G0_e:=0; izm_result[i,j].G1_e:=0; izm_result[i,j].G2_e:=0; izm_result[i,j].G3_e:=0; for k := 0 to 2400 do begin izm_result[i,j].U_izm[k]:=0; izm_result[i,j].I_izm[k]:=0; end; end; j:=0; num:=0; povtor:=0; point:=1; message_begin:=false; while true do begin repeat Get_USB_Device_Status; //проверяем буфер устройства //sleep(100); until FT_Q_Bytes > 0; Read_USB_Device_Buffer(1); //читаем буфер устройства i:=FT_In_Buffer[0]; //читаем первое число if i=240 then begin message_begin:=true;//посылка пошла Synchronize(Update_dev_cond_string_getdata); end; if message_begin then begin message_[j]:=i; inc(j); end; if i=255 then begin //посылка закончилась j:=0; message_begin:=false; Synchronize(Update_dev_cond_string_decode); Synchronize(Update); Synchronize(Update_dev_cond_string_ready); inc(num); inc(povtor); Synchronize(Check_and_load_data); Synchronize(Update_message_count); for I := 0 to length(message_) -1 do message_[i]:=0; Purge_USB_Device_Out;//очищаем буфер end; end; end; procedure device_thread_th.Update; begin IzmForm.incoming_message(MainForm.device.message_decode(message_),povtor+1,point); end; procedure device_thread_th.Update_dev_cond_string_getdata; begin IzmForm.Izm_condition_label.Caption:=ConstUnit.izm_get_data; IZmForm.IzmPB.Position:=0; IZmForm.IzmTimer.Enabled:=true; end; procedure device_thread_th.Update_dev_cond_string_ready; begin IzmForm.Izm_condition_label.Caption:=ConstUnit.izm_device_ready; IZmForm.IzmTimer.Enabled:=false; end; procedure device_thread_th.Update_dev_cond_string_decode; begin IzmForm.Izm_condition_label.Caption:=ConstUnit.izm_mes_decode; end; procedure device_thread_th.Update_message_count; begin IzmForm.izm_mes_count.Caption:=inttostr(num); if ((num mod 5)=0) then begin povtor:=0; inc(point); end; if ((num mod 5)=0) then begin IzmForm.izm_next_point.Execute; end; IzmForm.izm_mes_tek_count.Caption:=inttostr(povtor); end; procedure device_thread_th.Check_and_load_data; var list:TStringList; i,j,k: Integer; str,y1,y2:string; tmp:array [1..6] of extended; s:TSystemTime; date:string; begin izm_result[point,povtor]:=MainForm.device.message_decode(message_); //если пришли к концу всего измерения: 24 точки по 5 повторов if (point=24) and (povtor=5) then if MessageBox(0, PChar(izm_izm_end), PChar(izm_caption_message), MB_ICONQUESTION or MB_OKCANCEL )=id_OK then begin IzmForm.Izm_stop_btn.Caption:='Закончить измерение'; Suspend; str:=''; //for I := 0 to 2400 do list:=TStringList.Create; list.Clear; list.Add('INSERT INTO `rd`.`'+table_znacheniya_name+'` ('); list.Add('`id` ,'); list.Add('`izm_num` ,'); list.Add('`param_num` ,'); list.Add('`point_num` ,'); list.Add('`povtor_num` ,'); list.Add('`znach` ,'); list.Add('`znach_UI`'); list.Add(')'); list.Add('VALUES'); for i:=1 to 24 do begin //запись полученных значений for j:=1 to 5 do begin tmp[1]:=izm_result[i,j].R; tmp[2]:=izm_result[i,j].G0; tmp[3]:=izm_result[i,j].G1; tmp[4]:=izm_result[i,j].G2; tmp[5]:=izm_result[i,j].G3; tmp[6]:=izm_result[i,j].C; y1:=IzmForm.get_string(izm_result[i,j].U_izm); y2:=IzmForm.get_string(izm_result[i,j].I_izm); for k:=1 to 6 do begin str:='(NULL , '''+ IzmForm.Izm_gen_num.Caption+''', '''+ inttostr(k+2)+''', '''+ inttostr(i)+''', '''+ inttostr(j)+''', '''; DecimalSeparator:='.'; str:=str+floattostrf(tmp[k],ffExponent, 6, 2)+''',' +'NULL'; if (i=24) and (j=5) and (k=6) and not(IzmForm.izm_write_ui.Checked) then str:=str+')' else str:=str+'),'; list.Add(str); end; if IzmForm.izm_write_ui.Checked then begin str:='(NULL , '''+ IzmForm.Izm_gen_num.Caption+''', '''+ inttostr(1)+''', '''+ inttostr(i)+''', '''+ inttostr(j)+''', '''+floattostr(0)+''',' +y1+'),'; list.Add(str); str:='(NULL , '''+ IzmForm.Izm_gen_num.Caption+''', '''+ inttostr(2)+''', '''+ inttostr(i)+''', '''+ inttostr(j)+''', '''+floattostr(0)+''',' +y2+')'; if not((i=24) and (j=50) and (k=6)) then str:=str+','; list.Add(str); end; end; end; list.SaveToFile('znachzapros.txt'); GetSystemTime(s); date:=inttostr(s.wYear)+'-'+inttostr(s.wMonth)+'-'+inttostr(s.wDay)+' '+ inttostr(s.wHour+4)+':'+inttostr(s.wMinute)+':'+inttostr(s.wSecond); //непосредственная запись в базу with SQL_DM.mysql_query4 do begin Connection:=SQL_DM.mysql_con; SQL.Clear; SQL.Add('INSERT INTO `rd`.`rd_izmerenia` ('); SQL.Add('`id` ,'); SQL.Add('`date` ,'); SQL.Add('`patient_num` ,'); SQL.Add('`opisanie`'); SQL.Add(')'); SQL.Add('VALUES ('); SQL.Add(' '''+IzmForm.Izm_gen_num.Caption+''' , '''+date+''', '''+IzmForm.Izm_pat_id.Caption+''', '); SQL.Add(' '''+IzmForm.izm_opisanie_edit.Text+''');'); Execute; Active:=false; SQL.Clear; SQL.AddStrings(list); try Execute; except // end; end; list.Destroy; //запись в файл токов и напряжений {list:=Tstringlist.Create; str:=''; for i:=1 to 24 do for j:=1 to 5 do begin ///формируем следующим образом: /// создаем табулированный файл для последующего экспорта в эксель /// в первой строчке номер точки, номер повтора /// потом столбец значений str:=str+'точка: '+inttostr(i)+'повтор: '+inttostr(j)+'U'+#9+ 'точка: '+inttostr(i)+'повтор: '+inttostr(j)+'I'+#9; end; list.Add(str); str:=''; for i:=1 to 24 do for j:=1 to 5 do for k:=0 to 2400 do begin begin str:=str+inttostr(izm_result[i,j].U_izm[k])+#9+inttostr(izm_result[i,j].I_izm[k])+#9; list.Add(str); end; list.SaveToFile(IzmForm.izm_opisanie_edit.Text+'номер пациента: '+IzmForm.Izm_pat_id.Caption+'точка №:'+inttostr(i)); end; end; showmessage('Токи и напряжения записаны'); list.Destroy;} end; // IzmForm.load_data_to_database(izm_result); end; end.