| Что такое порт?, правила для работы с портами | Previous Home Next |
| var f: THandle; |
| ct: TCommTimeouts; |
| dcb: TDCB; |
| f := Windows.CreateFile(PChar('COM1'), GENERIC_READ or |
| GENERIC_WRITE, |
| FILE_SHARE_READ or FILE_SHARE_WRITE, |
| nil, OPEN_EXISTING, |
| FILE_ATTRIBUTE_NORMAL, 0); |
| if (f < 0) or not Windows.SetupComm(f, 2048, 2048)or not |
| Windows.GetCommState(f, dcb) then exit; //init error dcb.BaudRate := скоpость; |
| dcb.StopBits := стоп-биты; |
| dcb.Parity := ?етность; |
| dcb.ByteSize := 8; |
| if not Windows.SetCommState(f, dcb) or |
| not Windows.GetCommTimeouts(f, ct) then exit; //error |
| ct.ReadTotalTimeoutConstant := 50; |
| ct.ReadIntervalTimeout := 50; |
| ct.ReadTotalTimeoutMultiplier := 1; |
| ct.WriteTotalTimeoutMultiplier := 0; |
| ct.WriteTotalTimeoutConstant := 10; |
| if not Windows.SetCommTimeouts(f, ct) |
| or not Windows.SetCommMask(f, EV_RING + EV_RXCHAR + EV_RXFLAG + EV_TXEMPTY) |
| then exit; //error |
| result := true; |
| p := @Buf; |
| result := 0; |
| while size > 0 do |
| begin |
| if not WriteFile(f, p^, 1, i, nil) then exit; |
| inc(result, i); inc(integer(p)); dec(size); |
| Application.ProcessMessages; |
| end; |
| Begin |
| BaudRate := 9600; |
| ByteSize := 8; |
| Parity := NOPARITY; |
| StopBits := ONESTOPBIT; // одино?ный стоп-бит |
| Flags := EV_RXCHAR + EV_EVENT2; |
| End; |