Board index » delphi » Invalid variant type conversion error

Invalid variant type conversion error

Hi, I am realy new to Delphi and I have a question.

I'm trying to make a program that will send some characters at COM2. This is
the code.

I get the error EVariantError with the message "Invalid variant type
conversion"

Please help me understand where is the mistake.

Thanks a lot.

Here's the code:

Quote
>var
>F:file of char;
>ch:char;
>hcom:thandle;

>begin
>assignfile(F,filelistbox1.filename);
>reset(F);
>hcom:=createfile('COM2',generic_read or

generic_write,0,0,open_existing,0,null);
Quote
>while not eof(F) do
>begin
> read(F,ch);
> write(hcom,ch);
>end;
>closehandle(hcom);
>closefile(F);
>end;

 

Re:Invalid variant type conversion error


Quote
"Dan" <aili...@home.ro> wrote in message

news:353a0782.0111282313.7181c2f5@posting.google.com...

Quote
> >var
> >F:file of char;
> >ch:char;
> >hcom:thandle;

> >begin
> >assignfile(F,filelistbox1.filename);
> >reset(F);
> >hcom:=createfile('COM2',generic_read or
> generic_write,0,0,open_existing,0,null);
> >while not eof(F) do
> >begin
> > read(F,ch);
> > write(hcom,ch);

You are using the wrong function to write to the com2 file! Define this:
var
    numWritten : integer = 0;

and use this function:

WriteFile(hcom, @ch, 1, @numWritten, nil);

Quote
> >end;
> >closehandle(hcom);
> >closefile(F);
> >end;

or use pascal file approach for the com port too but i'm not sure it will
work:

var
    fcom : file of char;

AssigFile(fcom, 'COM2');
Rewrite(fcom);
Write(fcom, ch);
Closefile(fcom);

Other Threads