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);
   d: array[1..RxBufferSize] of Char;//Should this match the number in
   sTmpL: string;
   i: Integer;
   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.

if sPortState<>[r] then showmessage('Could not read')//no ; here
else begin //1
 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
    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.)';
    filename := 'C:\Users\tngmorse\Desktop\assa.txt';
    AssignFile(SomeTxtFile, FileName);
    Append(SomeTxtFile) ;
   WriteLn(SomeTxtFile, sTmpL) ;
   CloseFile(SomeTxtFile) ;
  //leave open, in case another read is next. Write starts with a close. closehandle(hCommFile);
end; //buSeeIfTheresDataClick

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

bErrCode:=0;//Will eventually be returned. If zero, no error seen
if not SetupComm(hCommFile, RxBufferSize, TxBufferSize) then
   { 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
   { 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
   { Raise an exception }

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

if bErrCode=0 then //no "begin" here...  (this is part of avoiding exceptions)
with CommTimeouts do
   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;

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

end;//function SetUpSerPort:byte;


  • Is string perhaps something other than an AnsiString? What Delphi version are you using?
  • it should be AnsiString, but i think issue is i am reading it in as a string. Its XE2
  • XE2 is UTF/Unicode by default IIRC. Have you tried AnsiChar instead of Char?
  • After some changes , i used ansiChar instead and now its working! thanks
Sign In or Register to comment.