Read Hex values Cport Library Delphi - delphi-xe2

I have a problem reading the values from a Comport, is there any solution how to read hex value from comport? I already tried:
var
Variable 1 : AnsiString;
begin
Comport1.Read(variable1,10);
edit1.text := variable1;
end
But comes an error:
[DCC Warning] SampProj.pas(86): W1057 Implicit string cast from
'AnsiString' to 'string'
[DCC Fatal Error] F2039 Could not create output file
'.\Win32\Debug\Project1.exe'

Related

Virtual Pascal GetDate returns Type mismatch?

I'm trying to port a DOS Pascal app to Windows using Virtual Pascal and working fairly well until this simple procedure:
PROCEDURE SysDate(VAR dt : datetype);
VAR
A,B,C,D : WORD;
BEGIN
GETDATE(A,B,C,D);
DT.YEAR := A;
DT.MONTH := B;
DT.DAY := C;
END;
caused this error:
Error 26: Type mismatch
GETDATE(A,B,C,D);
...........^
Any ideas what may be causing it? I searched the source for virtual pascal and the dos unit is the only thing defining GetDate() and defines all the variables as WORD as it should.

Incompatible types: 'string' and 'Boolean' [duplicate]

This question already has answers here:
Incompatible types: 'string' and 'Double'
(2 answers)
Closed last year.
I have to check for a sound card, so I don't need quality but only one answer yes or no. I used this code:
function IsSoundCardInstalled: Boolean;
Begin
Result := waveOutGetNumDevs > 0;
End;
procedure TForm1.FormCreate(Sender: TObject);
var
ids: TidIpWatch;
Speed: Double;
myStringList: TStringList;
begin
ids := TidIpWatch.Create;
Speed := GetCPUSpeed;
ids.Free;
myStringList:=TStringList.Create;
myStringList.Add('IP:' + (ids.LocalIP));
myStringList.Add('CPU: ' + (Tipo_cpu) + ' ' + Format('%f', [Speed]));
myStringList.Add((IsSoundCardInstalled));
myStringList.Add('etc.');
Memo1.Lines.Assign(myStringList);
myStringList.Free;
end;
But the error returns to me:
[DCC Error] Unit1.pas(138): E2010 Incompatible types: 'string' and 'Boolean'
On the line:
myStringList.Add((IsSoundCardInstalled));
IsSoundCardInstalled() returns a Boolean, but myStringList.Add() expects a string instead. You can't assign a Boolean as-is to a string, you need to use a conversion function (just like in your previous question), such as SysUtils.BoolToStr():
uses
..., SysUtils;
myStringList.Add(BoolToStr(IsSoundCardInstalled));
Or SysUtils.TBooleanHelper.ToString() in XE4+:
uses
..., SysUtils;
myStringList.Add(IsSoundCardInstalled.ToString);
On a side note: you are freeing the TIdIPWatch component before reading its LocalIP property, which is undefined behavior.
For that matter, you should not be using TIdIPWatch in this manner at all. It is meant for notifying you when the local IP changes, and for maintaining a history of local IP changes over time, but that is not how you have been using it lately. The TIdIPWatch.LocalIP property simply reads the global GStack.LocalAddress property, that is what you should be using instead:
uses
..., IdStack;
TIdStack.IncUsage;
try
myStringList.Add('IP:' + GStack.LocalAddress);
finally
TIdStack.DecUsage;
end;
However, a machine can have multiple local IPs, so you really should use GStack.GetLocalAddressList() instead:
uses
..., IdStack;
var
myLocalIPList: TIdStackLocalAddressList;
i: Integer;
begin
...
TIdStack.IncUsage;
try
myLocalIPList := TIdStackLocalAddressList.Create;
try
GStack.GetLocalAddressList(myLocalIPList);
for I := 0 to myLocalIPList.Count-1 do
myStringList.Add('IP:' + myLocalIPList[I].IPAddress);
finally
list.Free;
end;
finally
TIdStack.DecUsage;
end;
...
end;

Free Pascal warning "function result variable of a managed type does not seem to be initialized"

Free Pascal 3.2.0 issues a warning about a missing initialization for a function result of type TBytes:
function ToBytes(const AValue: RawByteString): TBytes;
begin
SetLength(Result, Length(AValue)); <--- Warning
if Length(AValue) > 0 then
Move(AValue[1], Result[0], Length(AValue));
end;
Warning: function result variable of a managed type does not seem to
be initialized
Declaration of TBytes:
TBytes = array of Byte;
How can I fix this warning?
Add "result:=nil;"
The reason this warning was added, is explained in https://bugs.freepascal.org/view.php?id=36973

Dynamic array access violation error

program LengthOfArray;
uses sysutils;
procedure Main();
var myArray: array of String;
begin
myArray[0] := 'hi';
myArray[1] := 'bye';
myArray[2] := 'hello';
WriteLn('array is: ', Length(myArray));
end;
begin
Main();
end.
When I run this, it gives me an error like this:
An unhandled exception occurred at $0040210C :
EAccessViolation : Access violation
$0040210C
$00401540
$004015ED
When compiling, it gave me a message Warning: Local variable "myArray" does not seem to be initialized... but I don't understand because I assigned 3 values to my array already. Isn't that initialized?

How to make function with multiple parameters with different data type in pascal?

I have made a function with 2 parameters with the same data type and I have no problem with that.
But I'm having trouble with different data type
Here's my code :
uses crt;
function inputscore(name : string, score:integer) : integer;
begin
writeln('My name is ',name,' and my score is ',score);
inputscore:=0;
end;
begin
clrscr;
inputscore('David',98);
readkey;
end.
But It returned this error message:
multipleparameterfunc.pas(2,34)Fatal syntax error, ")" expected but "," found
In Pascal you separate the arguments with a ;.
So your definition has to look like this:
function inputscore(name: string; score: integer) : integer;
When you call the function then you still use a , to separate the parameters:
inputscore('David', 98);

Resources