Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2003.05.15;
Скачать: CL | DM;

Вниз

Работа с Com портом   Найти похожие ветки 

 
Sergey_   (2003-03-19 15:41) [0]

Уважаемые All, кто подскажет где крутить :
пытаюсь послать данные в СОМ порт функцией
WriteFile(hFile: THandle; const Buffer; nNumberOfBytesToWrite: DWORD;
если я посылаю один раз то получается
если в цикле то при второй записи получаю сообщение
"Код ошибки: 997 (0x03E5) - Протекает наложенное событие ввода/вывода."
как победить ?


 
ki11er   (2003-03-19 15:55) [1]

Не работать с overlapped или работать с ним правильно ;-)


 
Sergey_   (2003-03-19 16:03) [2]

а правильно энто как есть пример ?


 
ki11er   (2003-03-19 16:07) [3]

Посмотри на www.torry.ru, там таких компонентов вагон и маленькая тележка ;-)


 
REA ©   (2003-03-19 16:09) [4]

Если не удается самому, удобно использовать готовые компоненты.
Мне попадплся неплохой компонент CPort http://www2.arnes.si/~sopecrni
,но наверно есть и лучше.


 
Sergey_   (2003-03-19 16:21) [5]

2(REA) именно эту компоненту я и использую
она и выдаёт ошибку на моё действие
For i := 1 to 100 do
Cport.WriteStr(St);
первый раз пишет нормально а на второй Код ошибки: 997 (0x03E5)


 
ki11er   (2003-03-19 16:33) [6]

2 Sergey_]

20.06.2000 - Forum added
I have created a forum to discuss ComPort Library problems and issues.

Ты туда писал?
Кстати, St какую длинну имеет?


 
Sergey_   (2003-03-19 16:41) [7]

2 ki11er
нет не писал
ST - 11 символов
в чём проблема я вроде понял - не понял как её решать
такая конструкция работает
For i := 1 to 100 do begin
ShowMessage(st);
Cport.WriteStr(St);
end;



 
ki11er   (2003-03-19 16:53) [8]

Странно, в ихнем хелпе написано (только что скачал):

Call WriteStr function to write Str variable to output buffer. The function does not return until whole string is written or timeout elapses.


Скорее всего ошибка в ихних компонентах. Либо ты установил очень уж маленький таймаут ;-)


 
Sergey_   (2003-03-19 18:05) [9]

хм а какой надо ?


 
Переяслов Григорий ©   (2003-03-19 18:06) [10]

Лови так, если нет мыла. Это компонент. 3 года пишу с его использованием, проблем нет.

unit OComm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TBaudRate = (BR____110, BR____300, BR____600, BR___1200,
BR___2400, BR___4800, BR___9600, BR__14400, BR__19200,
BR__38400, BR__56000, BR__57600, BR_115200, BR_128000,
BR_256000);
TStopBits = (SB_ONESTOPBIT, SB_ONE5STOPBITS, SB_TWOSTOPBITS);
TComNo = (Com1, Com2, Com3, Com4);

TComm = class(TComponent)
private
FActive: Boolean;
FBaudRate: TBaudRate;
FStopBits: TStopBits;
FComNo: TComNo;
FOnActivate: TNotifyEvent;
FOnDeActivate: TNotifyEvent;
FBufferLen: LongInt;
procedure SetActive(Value: Boolean);
procedure SetBaudRate(Value: TBaudRate);
procedure SetStopBits(Value: TStopBits);
procedure SetComNo(Value: TComNo);
procedure SetBufferLen(Value: LongInt);

protected
function GetBaudRate: LongInt;
function GetStopBits: LongInt;
procedure DoActive; dynamic;

public
ComPort: Integer;
Connect: Boolean;
Wait: Boolean;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;

procedure Write(const Buffer: array of Byte; Count: Integer);
function Read(var Buffer: array of Byte; Count: Integer): Boolean;
procedure WriteByte(B: Byte);
function ReadByte: Byte;
procedure Purge;

published
property Active: Boolean read FActive write SetActive;
property BaudRate: TBaudRate read FBaudRate write SetBaudRate;
property StopBits: TStopBits read FStopBits write SetStopBits;
property ComNo: TComNo read FComNo write SetComNo;
property BufferLen: LongInt read FBufferLen write SetBufferLen;

property OnActivate: TNotifyEvent
read FOnActivate write FOnActivate;
property OnDeActivate: TNotifyEvent
read FOnDeActivate write FOnDeActivate;
end;


Var
Comm : TComm;


implementation
const
TimeOut = 100;

var
DCB: TDCB;
TOUT : TCOMMTIMEOUTS;
Answer: Boolean;

constructor TComm.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Wait := True;
FActive := False;
FBaudRate := BR___9600;
FStopBits := SB_ONESTOPBIT;
FComNo := Com2;
FBufferLen := 300;
Connect := False;
ComPort := INVALID_HANDLE_VALUE;
end;

destructor TComm.Destroy;
begin
Active := False;
inherited Destroy;
end;

procedure TComm.Write(const Buffer: array of Byte; Count: Integer);
var
CountWrite: DWord;
begin
CountWrite := 0;
if ComPort <> INVALID_HANDLE_VALUE then
WriteFile(ComPort,Buffer,Count,CountWrite,Nil);
end;

function TComm.Read(var Buffer: array of Byte; Count: Integer): Boolean;
var
CountRead: DWord;
begin
CountRead := 0;
if ComPort <> INVALID_HANDLE_VALUE then
Result := ReadFile(ComPort,Buffer,Count, CountRead, Nil) and (CountRead > 0);
Wait := CountRead = 0;
if Wait then
begin
Buffer[0] := 0;
Result := False;
end;
end;

procedure TComm.WriteByte(B: Byte);
var
A: array[1..1] of Byte;
begin
A[1] := B;
Write(A, 1);
end;

function TComm.ReadByte: Byte;
var
A: array[1..1] of Byte;
begin
Read(A, 1);
Result := A[1];
end;


 
Переяслов Григорий ©   (2003-03-19 18:06) [11]

function TComm.GetBaudRate: LongInt;
begin
case BaudRate of
BR____110: Result := CBR_110;
BR____300: Result := CBR_300;
BR____600: Result := CBR_600;
BR___1200: Result := CBR_1200;
BR___2400: Result := CBR_2400;
BR___4800: Result := CBR_4800;
BR___9600: Result := CBR_9600;
BR__14400: Result := CBR_14400;
BR__19200: Result := CBR_19200;
BR__38400: Result := CBR_38400;
BR__56000: Result := CBR_56000;
BR__57600: Result := CBR_57600;
BR_115200: Result := CBR_115200;
BR_128000: Result := CBR_128000;
BR_256000: Result := CBR_256000;
else
Result := CBR_9600;
end;
end;

function TComm.GetStopBits: LongInt;
begin
case StopBits of
SB_ONESTOPBIT: Result := ONESTOPBIT;
SB_ONE5STOPBITS: Result := ONE5STOPBITS;
SB_TWOSTOPBITS: Result := TWOSTOPBITS;
else
Result := ONESTOPBIT;
end;
end;

procedure TComm.DoActive;
begin
end;

procedure TComm.SetActive(Value: Boolean);
begin
if Value<>FActive then
begin
if Value then
begin
Connect := True;
case ComNo of
Com1: ComPort := CreateFile("Com1",
GENERIC_WRITE Or GENERIC_READ,0,Nil,OPEN_EXISTING,0,0);
Com2: ComPort := CreateFile("Com2",
GENERIC_WRITE Or GENERIC_READ,0,Nil,OPEN_EXISTING,0,0);
Com3: ComPort := CreateFile("Com3",
GENERIC_WRITE Or GENERIC_READ,0,Nil,OPEN_EXISTING,0,0);
Com4: ComPort := CreateFile("Com4",
GENERIC_WRITE Or GENERIC_READ,0,Nil,OPEN_EXISTING,0,0);
end;
if ComPort <> INVALID_HANDLE_VALUE then
begin
DCB.BaudRate := GetBaudRate;
DCB.ByteSize := 8;
DCB.Parity := NOPARITY;
DCB.StopBits := GetStopBits;
// DCB.fRTSControl := RTS_CONTROL_HANDSHAKE;


 
ki11er   (2003-03-19 18:58) [12]


> хм а какой надо ?

Чего не знаю, того не знаю (не я ж эти компоненты писал). Могу только предположить, что он должен быть не меньше, чем время ухода твоих 11 символов на заданной тобой скорости.



Страницы: 1 вся ветка

Текущий архив: 2003.05.15;
Скачать: CL | DM;

Наверх




Память: 0.5 MB
Время: 0.015 c
1-46706
Ruslan
2003-05-03 08:05
2003.05.15
Про появление формы


7-47023
Snap
2003-03-17 18:52
2003.05.15
Можно ли отследить что мышка была нажата или передвинута программно, а не юзером?


3-46561
pathfinder
2003-04-22 18:15
2003.05.15
IBQuery или IBSQL?


1-46684
BLAST
2003-05-03 19:24
2003.05.15
Вопрос по FindComponent


14-46980
_PG_gaws
2003-04-21 13:18
2003.05.15
Помогите !!!!!!!!!!!(Как перехватить нажатие кнопки Maximize)