NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Delphi COM Streaming

December 25, 2009
by Aceslick911
Aceslick911's Avatar

I made an awesome little app in delphi which sends commands to the LED ARRAY to display messages, effects, animations. The app works fine while its isolated but when I do anything in-between commands (say to another device) to the serial port, the whole thing crashes for some reason.

I think it has something to do with either the cable (PL212) or something im not doing right in delphi to send and recieve the messages. Does anyone have an experience regarding a similar issue?

This was supposed to be a kick-ass xmas lights display but it never got off the design table because the laptop kept crashing over and over again... Unrecoverable. Running windows XP.

-Angelo

December 25, 2009
by Aceslick911
Aceslick911's Avatar

*PL2303 cable.

commands used were...

Connect:

const

RxBufferSize = 256; TxBufferSize = 256; var DeviceName: array[0..80] of Char; DCB: TDCB; Config: string; CommTimeouts: TCommTimeouts; begin

StrPCopy(DeviceName, Edit4.text+':');

ComFile := CreateFile(DeviceName, GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

if ComFile = INVALID_HANDLE_VALUE then Result := False else Result := True;

if not SetupComm(ComFile, RxBufferSize, TxBufferSize) then Result := False;

if not GetCommState(ComFile, DCB) then Result := False;

if not result then begin Memo3.lines.add('Could not connect'); exit; end;

// define the baudrate, parity,... // hier die Baudrate, Parität usw. konfigurieren

Config := 'baud=115200 parity=n data=8 stop=1';

if not BuildCommDCB(@Config[1], DCB) then Result := False;

if not SetCommState(ComFile, DCB) then Result := False;

if not result then begin Memo3.lines.add('Could not connect'); exit; end;

with CommTimeouts do begin ReadIntervalTimeout := 0; ReadTotalTimeoutMultiplier := 0; ReadTotalTimeoutConstant := 10; WriteTotalTimeoutMultiplier := 0; WriteTotalTimeoutConstant := 500; end;

if not SetCommTimeouts(ComFile, CommTimeouts) then Result := False;


Send text command:

var BytesWritten: DWORD; i:integer; s:string; begin

//send characters 1 at a time for I := 1 to length(text) do begin s:=copy(text,i,1);

WriteFile(ComFile, s[1], Length(s), BytesWritten, nil);

end;


The application would freeze mostly during "send text" command but occasionally during the Open COM command.

December 25, 2009
by Aceslick911
Aceslick911's Avatar

*PL2303 cable.

commands used were...

Connect:


  const

    RxBufferSize = 256;
    TxBufferSize = 256;
  var
    DeviceName: array[0..80] of Char;
    DCB: TDCB;
    Config: string;
    CommTimeouts: TCommTimeouts;
  begin

    StrPCopy(DeviceName, Edit4.text+':');

    ComFile := CreateFile(DeviceName,
      GENERIC_READ or GENERIC_WRITE,
      0,
      nil,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL,
      0);

    if ComFile = INVALID_HANDLE_VALUE then
      Result := False
    else
      Result := True;

    if not SetupComm(ComFile, RxBufferSize, TxBufferSize) then
      Result := False;

    if not GetCommState(ComFile, DCB) then
      Result := False;

    if not result then
    begin
      Memo3.lines.add('Could not connect');
      exit;
    end;

    // define the baudrate, parity,...
    // hier die Baudrate, Parität usw. konfigurieren

    Config := 'baud=115200 parity=n data=8 stop=1';

    if not BuildCommDCB(@Config[1], DCB) then
      Result := False;

    if not SetCommState(ComFile, DCB) then
      Result := False;

    if not result then
    begin
      Memo3.lines.add('Could not connect');
      exit;
    end;

    with CommTimeouts do
    begin
      ReadIntervalTimeout         := 0;
      ReadTotalTimeoutMultiplier  := 0;
      ReadTotalTimeoutConstant    := 10;
      WriteTotalTimeoutMultiplier := 0;
      WriteTotalTimeoutConstant   := 500;
    end;

    if not SetCommTimeouts(ComFile, CommTimeouts) then
      Result := False;

Send text command:


  var
    BytesWritten: DWORD;
  i:integer;
  s:string;
  begin

  //send characters 1 at a time
  for I := 1 to length(text) do
  begin
    s:=copy(text,i,1);

    WriteFile(ComFile, s[1], Length(s), BytesWritten, nil);

  end;

The application would freeze mostly during "send text" command but occasionally during the Open COM command.

Post a Reply

Please log in to post a reply.

Did you know that our customers love us? Learn more...