Com port reading strange..

I am trying to read from a com port, I know I am reading something, but its not showing up correct. I am reading this

剅佒൒着

here is code ..
In Private i have
hCommFile: THandle

Now when I read in serial it is done like so.
{***********************************************************************}
procedure TForm4.Button1Click(Sender: TObject);
var
   d: array[1..RxBufferSize] of Char;//Should this match the number in
   //"SetUpSerPort"?
   sTmpL: string;
   i: Integer;
   BytesRead,nNumBytesToRead:dword;
   SomeTxtFile : TextFile;
   filename : string;
   //You will find various versions of the ReadFile call
   //on the internet. Getting the types right appears to
   //be a problem... perhaps what is "right" varies from
   //one version of Windows or Delphi to another??

   //Note also that there are TWO, DIFFERENT, routines for
   //setting up hCommFile, the handle to the data stream,
   //one for creating a stream to be read from, one for
   //a stream to write to. And they can't, in a simple
   //world, both be open at once. And opening the handle
   //for reading a stream seems to flush any incoming data
   //which might be in the buffer... if it was even collected
   //to the buffer while the handle wasn't valid.

begin
if sPortState<>[r] then showmessage('Could not read')//no ; here
else begin //1
 nNumBytesToRead:=sizeof(d);
 if (ReadFile(hCommFile, d, nNumBytesToRead, BytesRead, nil))=false then
   {ORIGINAL FROM WHICH THIS IS ADAPTED HAD "Raise an exception" here,
   no begin..else structure }
   begin showmessage('Problem with ReadFile call');
   end//no ; here
  else begin //else2
    sTmpL:='';
    for i:=1 to BytesRead do sTmpL:= sTmpL + d[i];
    if sTmpL='' then sTmpL:='(Nothing was found coming in on the serial port '+
       'or in the buffer.)';
    //showmessage(sTmpL);
    filename := 'C:\Users\tngmorse\Desktop\assa.txt';
    AssignFile(SomeTxtFile, FileName);
    Append(SomeTxtFile) ;
   WriteLn(SomeTxtFile, sTmpL) ;
   CloseFile(SomeTxtFile) ;
    memo1.Lines.Add(sTmpL);
  end;//else2
  //leave open, in case another read is next. Write starts with a close. closehandle(hCommFile);
 end;//1
end; //buSeeIfTheresDataClick


and how i set up the serial port
{*****************************************************************************}
function tform4.SetUpSerPort:byte;
//Call AFTER establishing hCommFile. Returns 0 if setup goes okay.
var
   DCB: TDCB;
   Config : string;
   CommTimeouts : TCommTimeouts;
   bErrCode:byte;

begin
bErrCode:=0;//Will eventually be returned. If zero, no error seen
if not SetupComm(hCommFile, RxBufferSize, TxBufferSize) then
   bErrCode:=1;
   { Raise an exception --- these comments
       all scraps of source material... will try to use exceptions in
       due course!}

if bErrCode=0 then //no "begin" here...  (this is part of kludge avoiding exceptions)
if not GetCommState(hCommFile, DCB) then
   bErrCode:=2;
   { Raise an exception }

Config := 'baud=9600 parity=n data=8 stop=1' + chr(0);

if bErrCode=0 then //no "begin" here...  (this is part of avoiding exceptions)
if not BuildCommDCB(@Config[1], DCB) then
   bErrCode:=3;
   { Raise an exception }

if bErrCode=0 then //no "begin" here...  (this is part of avoiding exceptions)
if not SetCommState(hCommFile, DCB) then
   bErrCode:=4;
   { Raise an exception }

if bErrCode=0 then //no "begin" here...  (this is part of avoiding exceptions)
with CommTimeouts do
begin
   ReadIntervalTimeout := 0;
   ReadTotalTimeoutMultiplier := 0;
   ReadTotalTimeoutConstant := 200;//This determines(?) how long
      //you stay in an attempt to read from serial port. milliseconds
      //I hope these routines are reading from a buffer managed by
      //the OS independently of these routines.
   WriteTotalTimeoutMultiplier := 0;
   WriteTotalTimeoutConstant := 1000;
end;

if bErrCode=0 then //no "begin" here...  (this is part of avoiding exceptions)
if not SetCommTimeouts(hCommFile, CommTimeouts) then
   bErrCode:=5;
   { Raise an exception }

result:=bErrCode;
end;//function SetUpSerPort:byte;

Comments

Sign In or Register to comment.