Using Delphi to control a hardware device connected to COM2

Just wrote a I/O unit for Windows 95 /NT. Here it is :)

(with TDCB in SetCommStatus you can control DTR etc.)
(Att: XonLim and XoffLim not higher then 600 or else NT doesn't work
properly ?)

unit My_IO;

interface

function OpenComm(InQueue, OutQueue, Baud: LongInt): Boolean;
function SetCommTiming: Boolean;
function SetCommBuffer(InQueue, OutQueue: LongInt): Boolean;
function SetCommStatus(Baud: Integer): Boolean;
function SendCommStr(S: String): Integer;
function ReadCommStr(var S: String): Integer;
procedure CloseComm;

var
  ComPort: Word;

implementation

uses Windows, SysUtils;

const
  CPort: array [1..4] of String =('COM1','COM2','COM3','COM4');

var
  Com: THandle = 0;

function OpenComm(InQueue, OutQueue, Baud : LongInt): Boolean;
begin
  if Com > 0 then CloseComm;
  Com := CreateFile(PChar(CPort[ComPort]), GENERIC_READ or GENERIC_WRITE,
0,nil , OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  Result := (Com > 0) and SetCommTiming and SetCommBuffer(InQueue,
OutQueue) and SetCommStatus(Baud) ;
end;

function SetCommTiming: Boolean;
var
  Timeouts: TCommTimeOuts;

begin
  with TimeOuts do
    begin
      ReadIntervalTimeout := 1;
      ReadTotalTimeoutMultiplier := 0;
      ReadTotalTimeoutConstant := 1;
      WriteTotalTimeoutMultiplier := 2;
      WriteTotalTimeoutConstant := 2;
    end;
  Result := SetCommTimeouts(Com,Timeouts);
end;

function SetCommBuffer(InQueue, OutQueue: LongInt): Boolean;
begin
  Result := SetupComm(Com, InQueue, OutQueue);
end;

function SetCommStatus(Baud: Integer): Boolean;
var
  DCB: TDCB;

begin
  with DCB do
    begin
      DCBlength:=SizeOf(Tdcb);
      BaudRate := Baud;
      Flags:=12305;
      wReserved:=0;
      XonLim:=600;
      XoffLim:=150;
      ByteSize:=8;
      Parity:=0;
      StopBits:=0;
      XonChar:=#17;
      XoffChar:=#19;
      ErrorChar:=#0;
      EofChar:=#0;
      EvtChar:=#0;
      wReserved1:=65;
    end;
  Result := SetCommState(Com, DCB);
end;

function SendCommStr(S: String): Integer;
var
  TempArray : array[1..255] of Byte;
  Count, TX_Count : Integer;

begin
  for Count := 1 to Length(S) do TempArray[Count] := Ord(S[Count]);
  WriteFile(Com, TempArray, Length(S), TX_Count, nil);
  Result := TX_Count;
end;

function ReadCommStr(var S: String) : Integer;
var
  TempArray : array[1..255] of Byte;
  Count, RX_Count : Integer;

begin
  S := '';
  ReadFile(Com, TempArray, 255, RX_Count, nil);
  for Count := 1 to RX_Count do S := S + Chr(TempArray[Count]);
  Result := RX_Count;
end;

procedure CloseComm;
begin
  CloseHandle(Com);
  Com := -1;
end;

end.

Also wrote a delay routine to send and wait for a while.
If your interrested, please let me know.

Lennart.

Jay Lee <yj...@aviion.zungwon.co.kr> schreef in artikel
<62mjmb$2m...@news1.dacom.co.kr>...

Quote
> Hi, I am a moderate level Delphi programmer and I have been given the
task
> to come up with an app that is supposed to control a h/w device connected
to
> COM2.  I need to send direct UART control commands, like CTS/RTS, DTR,
ie, I
> need to do bitwise manipulation of UART registers, and then send out data
> bits at precise intervals to this hardware.

> You know what?  I really don't have much clue as to how to do it.

> Any good help or pointers would be immensely appreciated.

> Jay