Advertisement
Guest User

swiss javitva G altal

a guest
Feb 29th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.50 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Button1: TButton;
  12.     procedure Button1Click(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.     { Public declarations }
  17.   end;
  18.  
  19. var
  20.   Form1: TForm1;
  21.   comfile:THandle;
  22.  
  23. implementation
  24.  
  25. {$R *.dfm}
  26.  
  27. function OpenCOMPort: Boolean;
  28. var
  29.   DeviceName: array[0..80] of Char;
  30.   ComFile: THandle;
  31. begin
  32.    { First step is to open the communications device for read/write.
  33.      This is achieved using the Win32 'CreateFile' function.
  34.      If it fails, the function returns false.
  35.  
  36.      Wir versuchen, COM1 zu öffnen.
  37.      Sollte dies fehlschlagen, gibt die Funktion false zurück.
  38.    }
  39.   StrPCopy(DeviceName, 'COM1:');
  40.  
  41.   ComFile := CreateFile(DeviceName,
  42.     GENERIC_READ or GENERIC_WRITE,
  43.     0,
  44.     nil,
  45.     OPEN_EXISTING,
  46.     FILE_ATTRIBUTE_NORMAL,
  47.     0);
  48.  
  49.   if ComFile = INVALID_HANDLE_VALUE then
  50.     Result := False
  51.   else
  52.     Result := True;
  53. end;
  54.  
  55.  
  56. function SetupCOMPort: Boolean;
  57. const
  58.   RxBufferSize = 256;
  59.   TxBufferSize = 256;
  60. var
  61.   DCB: TDCB;
  62.   Config: string;
  63.   CommTimeouts: TCommTimeouts;
  64. begin
  65.    { We assume that the setup to configure the setup works fine.
  66.      Otherwise the function returns false.
  67.  
  68.      wir gehen davon aus das das Einstellen des COM Ports funktioniert.
  69.      sollte dies fehlschlagen wird der Rückgabewert auf "FALSE" gesetzt.
  70.    }
  71.  
  72.   Result := True;
  73.  
  74.   if not SetupComm(ComFile, RxBufferSize, TxBufferSize) then
  75.     Result := False;
  76.  
  77.   if not GetCommState(ComFile, DCB) then
  78.     Result := False;
  79.  
  80.   // define the baudrate, parity,...
  81.   // hier die Baudrate, Parität usw. konfigurieren
  82.  
  83.   Config := 'baud=9600 parity=n data=8 stop=1';
  84.  
  85.   if not BuildCommDCB(@Config[1], DCB) then
  86.     Result := False;
  87.  
  88.   if not SetCommState(ComFile, DCB) then
  89.     Result := False;
  90.  
  91.   with CommTimeouts do
  92.   begin
  93.     ReadIntervalTimeout         := 0;
  94.     ReadTotalTimeoutMultiplier  := 0;
  95.     ReadTotalTimeoutConstant    := 1000;
  96.     WriteTotalTimeoutMultiplier := 0;
  97.     WriteTotalTimeoutConstant   := 1000;
  98.   end;
  99.  
  100.   if not SetCommTimeouts(ComFile, CommTimeouts) then
  101.     Result := False;
  102. end;
  103.  
  104.  
  105. {
  106.   The following is an example of using the 'WriteFile' function
  107.   to write data to the serial port.
  108.  
  109.   Folgendes Beispiel verwendet die 'WriteFile' Funktion, um Daten
  110.   auf den seriellen Port zu schreiben.
  111. }
  112.  
  113.  
  114. procedure SendText(s: string);
  115. var
  116.   BytesWritten: DWORD;
  117. begin
  118.    {
  119.      Add a word-wrap (#13 + #10) to the string
  120.  
  121.      An den übergebenen String einen Zeilenumbruch (#13 + #10) hängen
  122.    }
  123.   s := s + #13 + #10;
  124.   WriteFile(ComFile, s[1], Length(s), BytesWritten, nil);
  125. end;
  126.  
  127.  
  128. {
  129.   The following is an example of using the 'ReadFile' function to read
  130.   data from the serial port.
  131.  
  132.   Folgendes Beispiel verwendet die 'ReadFile' Funktion, um Daten
  133.   vom seriellen Port zu lesen.
  134. }
  135.  
  136.  
  137. function ReadText: string;
  138. var
  139.   d: array[1..80] of Char;
  140.   s: string;
  141.   BytesRead, i: cardinal;
  142. begin
  143.   Result := '';
  144.   if not ReadFile(ComFile, d, SizeOf(d), BytesRead, nil) then
  145.   begin
  146.     { Raise an exception }
  147.   end;
  148.   s := '';
  149.   for i := 1 to BytesRead do s := s + d[I];
  150.   Result := s;
  151. end;
  152.  
  153.  
  154. procedure CloseCOMPort;
  155. begin
  156.   // finally close the COM Port!
  157.   // nicht vergessen den COM Port wieder zu schliessen!
  158.   CloseHandle(ComFile);
  159. end;
  160.  
  161. procedure TForm1.Button1Click(Sender: TObject);
  162. begin
  163.   showmessage('hi');
  164. end;
  165.  
  166. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement