[ Pobierz całość w formacie PDF ]
.DFM}275 const// -- wartości znaczników sterujących portu szeregowego --dcb_fBinary = $0001;dcb_fParity = $0002;cbInQueue = 32; // rozmiary buforów danychcbOutQueue = 32;query_1 : PChar = '*IDN?'+#13+#10;query_2 : PChar = 'CDAT?'+#13+#10; // przykładowe zapytaniavarBuffer_O : ARRAY[0.cbOutQueue] of Char; // bufor wyjściowyBuffer_I : ARRAY[0.cbInQueue] of Char; // bufor wejściowyNumber_Bytes_Read : DWORD;hCommDev : THANDLE;lpFileName : PChar;fdwEvtMask : DWORD;Stat : TCOMSTAT;Errors : DWORD;dcb : TDCB;intVar : Integer; // licznik pomiarówintVarSleep : Integer; // licznik póznieniabResult : BOOL; // "niema" zmienna logicznahThread_SR : THANDLE;ThreadID_SR: Cardinal;Code, V : Integer;//--------------------------------------------------------------------procedure TForm1.CloseCommClick(Sender: TObject);variCheckProcess: Integer;beginiCheckProcess := MessageDlg('Zakończenie pomiaru i zamknięcie'+' aplikacji?', mtConfirmation, [mbYes, mbNo], 0);case iCheckProcess ofidYes:beginSuspendThread(hThread_SR);CloseHandle(hCommDev);Application.Terminate();end;idNo: Exit;end;end;//--------------------------------------------------------------------procedure TForm1.FormCreate(Sender: TObject);beginTrackBar1.Position := 1000;TrackBar1.Max := 2000;TrackBar1.Min := 1;TrackBar1.Frequency := 100;OpenComm.Enabled := TRUE;intVar := 0;intVarSleep := 1000;bResult := TRUE;Form1.BorderIcons:=[biSystemMenu, biMinimize];Form1.Series1.LinePen.Color := clBlue;Form1.Series1.LinePen.Style := psSolid;276 Picture3D.Checked := TRUE;end;//--------------------------------------------------------------------function Write_Comm(hCommDev: THANDLE;nNumberOfBytesToWrite: DWORD): Integer;varNumberOfBytesWritten : DWORD;beginWriteFile(hCommDev, Buffer_O, nNumberOfBytesToWrite,NumberOfBytesWritten, NIL);if (WaitCommEvent(hCommDev, fdwEvtMask, NIL) = TRUE) thenWrite_Comm := 1elseWrite_Comm := 0;end;//--------------------------------------------------------------------function Read_Comm(hCommDev: THANDLE;Buf_Size: DWORD): Integer;varnNumberOfBytesToRead: DWORD;beginClearCommError(hCommDev, Errors, @Stat);if (Stat.cbInQue > 0) thenbeginif (Stat.cbInQue > Buf_Size) thennNumberOfBytesToRead := Buf_SizeelsenNumberOfBytesToRead := Stat.cbInQue;ReadFile(hCommDev, Buffer_I, nNumberOfBytesToRead,Number_Bytes_Read, NIL);Read_Comm := 1;endelsebeginNumber_Bytes_Read := 0;Read_Comm := 0;end;end;//--------------------------------------------------------------------function RS_Send: Integer;beginRepeatFlushFileBuffers(hCommDev);Until (Write_Comm(hCommDev, StrLen(Buffer_O)) 0);Result := 0;end;//--------------------------------------------------------------------procedure TForm1.OpenCommClick(Sender: TObject);var i : Integer;beginif (CheckBox1.Checked = TRUE) thenlpFileName:='COM2';hCommDev:= CreateFile(lpFileName, GENERIC_READ or GENERIC_WRITE, 0,NIL, OPEN_EXISTING, 0, 0);if (hCommDev INVALID_HANDLE_VALUE) thenBEGINSetupComm(hCommDev, cbInQueue, cbOutQueue);dcb.DCBlength := sizeof(dcb);277 GetCommState(hCommDev, dcb);if (CheckBox2.Checked = TRUE) thendcb.BaudRate:=CBR_1200;//-przykładowe ustawienia znaczników sterujących DCB-dcb.Flags := dcb_fParity;dcb.Parity := ODDPARITY;dcb.StopBits :=ONESTOPBIT;dcb.ByteSize :=7;SetCommState(hCommDev, dcb);GetCommMask(hCommDev, fdwEvtMask);SetCommMask(hCommDev, EV_TXEMPTY);StrCopy(Buffer_O, query_1);RS_Send; // zapytanie o identyfikację urządzeniaSleep(1000);if (Read_Comm(hCommDev, SizeOf(Buffer_I)) > 0) thenbegin// -- wyświetlanie numeru przyrząduApplication.MessageBox(PChar(AnsiString(Buffer_I)),'Identyfikacja urządzenia przyłączonego do wybranego'+' portu :' ,MB_OK);OpenComm.Enabled := FALSE;endelseApplication.MessageBox('Urządzenie nie odpowiada ','Uwaga !' ,MB_OK);for i:=0 to cbInQueue dobeginBuffer_O[i] := char(0);Buffer_I[i] := char(0);end;Sleep(1000);ENDelsecase hCommDev ofIE_BADID:beginlpFileName := '';Application.MessageBox('Niewłaściwa nazwa portu lub'+' jest on aktywny', 'Uwaga !',MB_OK);end;end;end;//--------------------------------------------------------------------function RS_Send_Receive(P : Pointer): Integer;var j : Integer;beginwhile(bResult = TRUE) doBEGINwhile(Write_Comm(hCommDev, StrLen(Buffer_O)) = 0) do278 FlushFileBuffers(hCommDev);Form1.Memo1.Lines.Add('');Sleep(intVarSleep);//-------odczyt danych z portu--------if (Read_Comm(hCommDev, SizeOf(Buffer_I)) > 0) thenbeginForm1.Memo2.Lines.Add(AnsiString(Buffer_I));Inc(intVar); // zliczanie kolejnych pomiarówForm1.Memo1.Lines.Add(AnsiString(IntToStr(intVar)));val(Buffer_I, V, Code);Form1.Series1.AddXY(intVar, Round(V), '',clTeeColor);endelsebeginForm1.Memo2.Lines.Add('x0');Beep();Form1.Memo2.Lines.Add('');for j := 0 to cbInQueue doBuffer_I[j] := char(0);end;END; // koniec whileResult:=0;end;//--------------------------------------------------------------------procedure TForm1.StartClick(Sender: TObject);beginif (hCommDev > 0) thenbeginStrCopy(Buffer_O, query_2);hThread_SR := BeginThread (NIL, 0, @RS_Send_Receive, NIL, 0,ThreadID_SR);endelseApplication.MessageBox('Niewłaściwa nazwa portu lub jest on'+' aktywny ', 'Uwaga !',MB_OK);end;//----------wstrzymanie pomiaru --------------------------------------procedure TForm1.SuspendClick(Sender: TObject);beginSuspendThread(hThread_SR);end;//----------wznowienie pomiaru ---------------------------------------procedure TForm1.ResumeClick(Sender: TObject);beginResumeThread(hThread_SR);end;//-----kopiowanie okna edycji Memo2 do schowka------------------------procedure TForm1.SpeedButton1Click(Sender: TObject);beginForm1.Memo2.SelectAll;Form1.Memo2.CopyToClipboard;end;//-----kopiowanie okna edycji Memo1 do schowka------------------------procedure TForm1.SpeedButton2Click(Sender: TObject);begin279 Form1.Memo1.SelectAll;Form1.Memo1.CopyToClipboard;end;//--------------------------------------------------------------------procedure TForm1.TrackBar1Change(Sender: TObject);beginintVarSleep := TrackBar1.Position; // sterowanie póznieniemEdit1.Text := IntToStr(TrackBar1.Position);end;//---------wykres 3-wymiarowy--------------------------------------procedure TForm1.Picture3DClick(Sender: TObject);beginChart1 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • personata.xlx.pl