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: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203:
| unit Unit7;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, uCOMPort;
type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; Edit1: TEdit; Label1: TLabel; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); private public end;
var Form1: TForm1; PortTimeout : _COMMTIMEOUTS; PortHandle : Integer; PortDCB : TDCB; PortNr : Integer; PortState : Cardinal; WriteOverlapped,ReadOverlapped,StatusOs: TOverlapped; ComFile: THandle; DCB : TDCB;
implementation
{$R *.dfm}
procedure RTS (State:Integer); begin if State=0 then EscapeCommFunction(PortHandle,CLRRTS) else EscapeCommFunction(PortHandle,SETRTS); end;
function SendString(Str: String): boolean; stdcall; var written : cardinal; tmpStr : string; i: LongBool; begin if (PortHandle <> 0) then begin tmpStr := string(Str); Result := not WriteFile(PortHandle,tmpStr[1],Length(tmpStr),written,@WriteOverlapped); end; end;
function ReadText: string; var d: array[1..80] of Char; s: string; BytesRead:cardinal; i: Integer; begin Result := ''; if not ReadFile(ComFile, d, SizeOf(d), BytesRead, nil) then begin ShowMessage('Unmöglich etwas zu senden!'); end; s := ''; for i := 1 to BytesRead do s := s + d[I]; Result := s; end;
procedure DTR (State : integer); begin if State = 0 then EscapeCommFunction(PortHandle,CLRDTR) else EscapeCommFunction(PortHandle,SETDTR); end;
procedure InitOverlapped(var Overlapped : TOverlapped); begin Overlapped.Offset := 0; Overlapped.OffsetHigh := 0; Overlapped.Internal := 0; Overlapped.InternalHigh := 0; Overlapped.hEvent := CreateEvent(nil,True,False,''); end;
function OpenCOM(Port: byte): boolean; stdcall; begin PortHandle := CreateFile(PChar('\\.\COM'+IntToStr(Port)),GENERIC_READ or GENERIC_WRITE,0, nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,LongInt(0)); if PortHandle > 0 then begin Result := true; InitOverlapped(WriteOverlapped); InitOverlapped(ReadOverlapped); InitOverlapped(StatusOs); end else Result := false; end;
procedure CloseCOM; stdcall; begin PurgeComm(PortHandle, PURGE_RXABORT or PURGE_RXCLEAR or PURGE_TXABORT or PURGE_TXCLEAR); SetCommMask(PortHandle,0); CloseHandle(PortHandle); PortHandle := 0; end;
function SetBaudRate(baud: cardinal): boolean; stdcall; begin GetCommState(PortHandle,PortDCB); PortDCB.BaudRate := baud; Result := SetCommState(PortHandle,PortDCB); end;
procedure GetDCB; begin GetCommState(PortHandle,DCB); end;
procedure SetParity (Parity : byte); begin if (PortHandle > 0) and (Parity in [0..4]) then begin GetDCB; DCB.Parity := Parity; windows.SetCommState(PortHandle,DCB) end; end;
procedure SetStopBits(bits : byte); begin if (bits > 0) and (bits <= 2) then begin GetDCB; DCB.StopBits := bits; SetCommState (PortHandle,DCB); end; end;
procedure SetByteSize(bytesize : byte); begin if bytesize in [1..8] then begin GetDCB; DCB.ByteSize := bytesize; SetCommState (PortHandle,DCB); end; end;
procedure TForm1.Button1Click(Sender: TObject); begin SENDSTRING(chr(003)+chr(000)+chr(255)+chr(252)); end;
procedure TForm1.Button2Click(Sender: TObject); begin SENDSTRING(chr(003)+chr(000)+chr(000)+chr(003)); end;
procedure TForm1.Button3Click(Sender: TObject); begin OpenCOM(1); SetBaudRate(19200); SetParity(0); SetStopBits(0); SetByteSize(8); end;
procedure TForm1.Button4Click(Sender: TObject); begin CloseCOM; end;
procedure TForm1.Button5Click(Sender: TObject); var s,p:String; var b:byte; begin s:=Edit1.Text; SENDSTRING(chr(StrtoInt(s[1]+s[2]+s[3]))+chr(StrtoInt(s[4]+s[5]+s[6]))+chr(StrtoInt(s[7]+s[8]+s[9]))+chr(StrtoInt(s[10]+s[11]+s[12]))); Readtext; end;
end. |