E2010 Incompatible types: ‘Cardinal’ and ‘TSearchRec’

在编写一个非常简单的Delphi程序时,遇到一个奇怪的问题

[DCC Error] Project2.dpr(25): E2010 Incompatible types: ‘Cardinal’ and ‘TSearchRec’

var
  searchResult : TSearchRec;

begin
  // Try to find regular files matching Unit1.d* in the current dir
  if FindFirst('Unit1.d*', faAnyFile, searchResult) = 0 then
  begin
    repeat
      ShowMessage('File name = '+searchResult.Name);
      ShowMessage('File size = '+IntToStr(searchResult.Size));
    until FindNext(searchResult) <> 0;

    // Must free up resources used by these successful finds
    FindClose(searchResult);
  end;
end;

 

上述代码来自【参考1】

错误始终指向 FindClose(searchResult); 这一句,百思不得其解,后来搜索到了一个解释【参考2】,调换 uses 中的 windows和 sysutils 位置即解决……

参考:

1.http://www.delphibasics.co.uk/RTL.asp?Name=FindFirst FindFirst Function

2.http://www.delphipages.com/forum/showthread.php?t=145983

Step to UEFI (53) —– EFI_Graphics_Output_Protocol 屏幕拷贝的测试

前面实验了颜色填充,这里实验一下屏幕的拷贝。测试很简单,就是屏幕上一个区域的内容copy到另外的位置.

v2v

代码如下:

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include  <stdlib.h>
#include  <wchar.h>
#include  <time.h>
#include <Protocol/EfiShell.h>
#include <Library/ShellLib.h>

#include <Protocol/SimpleFileSystem.h>
#include <Protocol/BlockIo.h>
#include <Library/DevicePathLib.h>
#include <Library/HandleParsingLib.h>
#include <Library/SortLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>

#include <Protocol/LoadedImage.h>



extern EFI_BOOT_SERVICES         *gBS;
extern EFI_SYSTEM_TABLE			 *gST;
extern EFI_RUNTIME_SERVICES 	 *gRT;

extern EFI_SHELL_ENVIRONMENT2    *mEfiShellEnvironment2;
extern EFI_HANDLE				 gImageHandle;

static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;

//Copied from  C\MdePkg\Include\Protocol\UgaDraw.h
typedef struct {
  UINT8 Blue;
  UINT8 Green;
  UINT8 Red;
  UINT8 Reserved;
} EFI_UGA_PIXEL;


int
EFIAPI
main (                                         
  IN int Argc,
  IN char **Argv
  )
{
    EFI_STATUS    Status;
    UINTN i;
	
    Status = gBS->LocateProtocol(&GraphicsOutputProtocolGuid, NULL, (VOID **) &GraphicsOutput);
    if (EFI_ERROR(Status)) {
        GraphicsOutput = NULL;
		Print(L"Loading Graphics_Output_Protocol error!\n");
		return EFI_SUCCESS;
	}	

  for (i=0;i<100;i++) {
	GraphicsOutput->Blt(GraphicsOutput, NULL, EfiBltVideoToVideo,
                        rand() % (GraphicsOutput->Mode->Info->HorizontalResolution-100), 
						rand() % (GraphicsOutput->Mode->Info->VerticalResolution-100) , 
						rand() % (GraphicsOutput->Mode->Info->HorizontalResolution-100), 
						rand() % (GraphicsOutput->Mode->Info->VerticalResolution-100), 
						100, 100, 0); 
	gBS->Stall(50000);
  }				

  
  return EFI_SUCCESS;
  
}

 

运行结果

gfx3

工作视频:

http://www.tudou.com/programs/view/7gRxW6SqBKg/?resourceId=414535982_06_02_99

完整代码下载

GFXTest3

当然,这个实验有些让人看不清楚,修改上面的程序,直接在屏幕上画色块

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#include  <stdio.h>
#include  <stdlib.h>
#include  <wchar.h>
#include  <time.h>
#include <Protocol/EfiShell.h>
#include <Library/ShellLib.h>

#include <Protocol/SimpleFileSystem.h>
#include <Protocol/BlockIo.h>
#include <Library/DevicePathLib.h>
#include <Library/HandleParsingLib.h>
#include <Library/SortLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/BaseMemoryLib.h>

#include <Protocol/LoadedImage.h>



extern EFI_BOOT_SERVICES         *gBS;
extern EFI_SYSTEM_TABLE			 *gST;
extern EFI_RUNTIME_SERVICES 	 *gRT;

extern EFI_SHELL_ENVIRONMENT2    *mEfiShellEnvironment2;
extern EFI_HANDLE				 gImageHandle;

static EFI_GUID GraphicsOutputProtocolGuid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
static EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput = NULL;

//Copied from  C\MdePkg\Include\Protocol\UgaDraw.h
typedef struct {
  UINT8 Blue;
  UINT8 Green;
  UINT8 Red;
  UINT8 Reserved;
} EFI_UGA_PIXEL;


int
EFIAPI
main (                                         
  IN int Argc,
  IN char **Argv
  )
{
    EFI_STATUS    Status;
    UINTN i;
	EFI_GRAPHICS_OUTPUT_BLT_PIXEL FillColor;
	
    Status = gBS->LocateProtocol(&GraphicsOutputProtocolGuid, NULL, (VOID **) &GraphicsOutput);
    if (EFI_ERROR(Status)) {
        GraphicsOutput = NULL;
		Print(L"Loading Graphics_Output_Protocol error!\n");
		return EFI_SUCCESS;
	}	

  for (i=0;i<100;i++) {
    FillColor.Blue=rand() % 256;
	FillColor.Red=rand() % 256;
	FillColor.Green=rand() % 256;
	
	GraphicsOutput->Blt(GraphicsOutput, &FillColor, EfiBltVideoFill,
                0, 
				0 , 
				rand() % (GraphicsOutput->Mode->Info->HorizontalResolution-100), 
				rand() % (GraphicsOutput->Mode->Info->VerticalResolution-100), 
				100, 100, 0); 
	gBS->Stall(50000);
  }				

  
  return EFI_SUCCESS;
  
}

 

运行结果

gfxtest5

工作视频:

http://www.tudou.com/programs/view/VSbFaN1XBZc/?resourceId=414535982_06_02_99

完整代码下载

GFXTest5

参考:

1. UEFI SPEC 2.4 P498

2. 本文参考 https://github.com/chengs 的代码

If you are using Delphi TComPort VCL ……

If you are using Delphi TComPort VCL for some Arduino programs. Please set FlowControl -> ControlDTR to dtrEnable. Otherwise, you will get nothing from Serial Port.

comport

It costed me one afternoon for this issue. I worked with a Arduino Pro Micro (Leonardo). The Serial Monitor of IDE worked well. Putty worked well and ‘Serial Port Utility’ worked well. Arduino Uno was tried. It works well……. Only my Delphi program couldn’t get anything from the Serial Port. At last, I noticed the example program of TComPort worked well. But if I deleted this VCL and added again, it would fail. At last I found this Properties.

I don’t know why. All the documents said Leonardo didn’t use DTR pin.

If you have any suggestion, please let me know. Thanks a lot.

蓝牙控制小灯泡亮度的实验

这里实现用 Windows x86 平板电脑控制小灯泡亮度。

硬件方面在我们最初实验设备【参考1】的基础上增加一个蓝牙模块(这里建议使用蓝牙的朋友选用 HC06系列的,和HC05的不同,这个系列只有Slave的功能,但是个人感觉HC06更容易搜索连接上,我用HC05的时候每次都需要重新搜索配对设备,但是HC06上不用),用来和Windows平板进行通讯。

image001

代码方面,Arduino使用的程序非常简单,将串口收到的char当作PWM值直接输出。程序使用了2个串口,一个是通常的USB,同PC进行通讯,主要是为了方便Debug;真正工作的是另外一个进行蓝牙通讯的串口。

int  n=255;
void setup()
{
    Serial.begin(9600);
    Serial1.begin(9600);    
    pinMode(6,OUTPUT);      //该端口需要选择有#号标识的数字口
}

void loop()
{
  char  c;
    while (Serial.available() > 0)  
    {
        c=Serial.read();
        analogWrite(6,c);
        Serial.println(c);
    }
    while (Serial1.available() > 0)  
    {
        c=Serial1.read();
        analogWrite(6,c);
        Serial.println(c);
    }    
}

 

上位机使用的是Delphi 2010,使用控件很简单即可完成编程。

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, iComponent, iVCLComponent, iCustomComponent, iPositionComponent,
  iScaleComponent, iKnob, iSevenSegmentDisplay, iSevenSegmentBinary,
  iSevenSegmentInteger, StdCtrls, CPortCtl, CPort, Buttons;

type
  TForm2 = class(TForm)
    iKnob1: TiKnob;
    iSevenSegmentInteger1: TiSevenSegmentInteger;
    ComPort1: TComPort;
    Button1: TButton;
    Button2: TButton;
    procedure iKnob1PositionChange(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  ComPort1.ShowSetupDialog;
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  if ComPort1.Connected then
    begin
      ComPort1.Close;
      Button2.Caption:='Connect';
    end
  else
    begin
      ComPort1.Open;
      ComPort1.WriteStr(chr(0));
      Button2.Caption:='Disconnect';
    end
end;

procedure TForm2.FormActivate(Sender: TObject);
begin
  iKnob1.Width:=Form2.Width;
end;

procedure TForm2.iKnob1PositionChange(Sender: TObject);
var
  c:byte;
begin
  c:=trunc(iKnob1.Position);
  iSevenSegmentInteger1.Value:=c;
  if Comport1.Connected then
    begin
      ComPort1.Write(&c,1);
    end;
end;

end.

 

界面
image002

上位机完整代码下载

knobtest

工作的视频:
http://www.tudou.com/programs/view/yE4EHhUFcvU/?resourceId=414535982_06_02_99

最后说点其他的:除了Apple和各式各样的 Anrdoid平板电脑,x86的Windows平板也在崛起。

相比之下,使用Windows平板编程有如下优点:
1. 编程简单。工具方面Delphi VB VC 都是非常成熟的工具,能在普通PC上运行的程序,即可顺利移植到Windows平板上(甚至可以说‘移植’这个词不合适,因为不用任何改动直接放上去即可);
2. 发布简单。从时效性上来说,不需要发布到什么市场,也不需要什么审核,各种方法让对方拿到即可运行;
3. 周边设备多多,比如:各种摇杆方向盘,价格也比Apple专用的低很多;
4. 程序运行非常稳定,除非程序有错误,否则根本不会出现那种莫名其妙的“闪退”;

此外,从我的实践的角度来说,Windows 平板目前还有如下的缺点:
1. Windows本质上是给有鼠标的机器运行的,而不是触摸类的设备。这两者在精度上差别很大,传统的Window的各种控件,默认的调用者也都是鼠标,如果直接用触摸操作起来很困难,也容易误操作。因此,我用普通台式机做平板程序的感受是:你一定要把你的用户当成视力有困难的人,能调大的菜单或者按钮一定要做到最大…….
2. 目前比较缺少Windows x86平板方面的中文资料,在使用板载的各种传感器时,缺少资料

参考:
1. http://www.lab-z.com/mos%E6%8E%A7%E5%88%B6%E5%B0%8F%E7%81%AF%E6%B3%A1%E7%9A%84%E5%AE%9E%E9%AA%8C/ MOS控制小灯泡的实验

解决 Warning C4819 的工具

某些情况下,BIOS编译过程中会收到 Warning C4819 的错误信息。

cwarning

Warning C4819:The file contains a character that can ot be represented in the current code page(936). save the file in unicode format to prevent data loss.

中文意思是:该文件包含不能在当前代码页中表示的字符,请将文件保存为Unicode格式,以防止数据丢失。【参考1】

简单的说产生的原因是源代码中有和你当前系统中 codepage 中不兼容无法表示的编码。这只是一个Warning 信息,只是因为 BIOS编译过程打开了 Warning as error 所以会导致编译停止。这样的字符通常处于注释中,程序员情不自禁的用了本地字符导致的。

了解了原因,有下面的解决方法:

1.换用英文版的OS,一劳永逸的方法;
2.在出现问题的文件关闭warning功能;
3.找到出现Warning的文件用NotePad打开,再按照ANSI格式保存一下;

只是上面都比较麻烦,特别是某些时候涉及到有这样问题的程序很多,你无法知道需要重复上面的动作多少次。

于是编写了这个工具用来解决这个问题,具体的原理是:逐个打开源文件,然后转换编码为当前系统的CodePage,比较转换前后,如果结果相同表明没有无法识别的编码字符;如果不同,首先改名原文件做备份,再将转换编码后的文件保存下来。

特别注意:

1.请确保使用这个工具之前你的源文件有备份
2.请确保源文件去掉只读属性

下载:

CCPv1.0

参考:

1.http://www.cnblogs.com/rainbowzc/archive/2009/07/02/1515427.html 不再经受”Warning C4819″的摧残(转)

本文首发于 BIOSren 论坛 http://biosren.com/viewthread.php?tid=7582&rpid=57253&fav=yes&ordertype=0&page=1#pid57253

在 TPanel 上做图

Delphi 的TPanel 是没有 Canavs属性的,因此无法直接在上面绘制图形。用下面的方法可以绕过限制,实现绘图的功能。

示例代码:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm2 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
var
  PanelCanvas:TControlCanvas;
begin
  PanelCanvas:=TControlCanvas.Create;
  PanelCanvas.Control:=panel1;
  with PanelCanvas do
  begin
      pen.Color:=clGreen;
      pen.Width:=5;
      MoveTo(0,0);
      LineTo(Panel1.Width,Panel1.Height);

      MoveTo(Panel1.Width-1,0);
      LineTo(0,Panel1.Height);
  end;

  PanelCanvas.Free;
end;

end.

 

按下Button后,会在 Panel 对角线绘制直线。

dp

代码和可执行文件下载

DrawOnPanel

Delphi 中的 TMediaPlayer 全屏

一个Delphi中使用TMediaPlayer全屏播放的例子,做法挺简单的。需要在TForm上放置2个按钮,一个用来播放,一个用来关闭TForm。然后再放置一个 TMediaPlayer 和 TPanel 控件即可。

所有的属性都是在运行期设置的。代码如下:

unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, MPlayer, StdCtrls;

type
  TForm2 = class(TForm)
    MediaPlayer1: TMediaPlayer;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Button1Click(Sender: TObject);
begin
  if MediaPlayer1.DeviceID<>0 then
    begin
      if (MediaPlayer1.Mode=mpplaying) then MediaPlayer1.Stop;
    end;

  MediaPlayer1.FileName:='md.wmv';
  MediaPlayer1.Open;
  MediaPlayer1.Play;
  MediaPlayer1.DisplayRect:=Rect(0,0,Screen.Width,Screen.Height);
end;

procedure TForm2.Button2Click(Sender: TObject);
begin
  Form2.Close;
end;

procedure TForm2.FormActivate(Sender: TObject);
begin
  //设置Form没有边框
  Form2.BorderStyle:=bsNone;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  //Form最大化
  WindowState:=wsMaximized;
  //Panel也是最大化
  Panel1.Align:=alClient;
  //设置播放显示在Panel1上
  MediaPlayer1.Display:=Panel1;
end;

end.

 

默认情况下 XP 和 Windows7 至少要支持WMV ,下面是一个测试的视频

md

源代码和可执行文件下载

FullScreen

Delphi发送键盘消息的例子

这是一个Delphi实现对其他窗口发送按键的例子。

首先是 keyTest,这是用来测试接收按键的程序,接收键盘发出的方向键,并将其显示在窗口上,只显示上下左右四个方向键。

keytest

其次是 SensKey ,它会首先查找窗口,然后发送按键给找到的窗口。程序是Console模式的,每隔1s发送一个方向按键。

测试时,先运行 Keytest,然后运行 SendKey,就可以看到 KeyTest 的窗口被挪到最前面,然后依次收到Sendkey 程序发出来的按键。

下载: KeyT

参考:

1.晓风缠月的博客
http://blog.sina.com.cn/s/blog_63cefe150100ogp9.html delphi虚拟键值
2.东方千树
http://hi.baidu.com/fangqianshu/item/97dc6fa46c4002e915329b21 Delphi中的OnKeyDown事件等等
OnKeyPress 只能抓到数值或字母按键及 Esc键、空白键,但不含功能键(F1-F12)
OnKeyDown 能抓到所有的键(除 Tab 键)但不能分辨「对称键」的不同
OnShortCut 能抓到所有的键(含 Tab 键)且能分辨「对称键」的不同

Delphi 写的替换 // 注释小工具

Delphi 的注释方式有两种:一种是传统的 Pascal 的 {} ,另外一种是 // 的单行注释。

下面的代码能将程序中的 //abcdefg 替换为 {//abcdefg} 这样的形式

program Project6;

{$APPTYPE CONSOLE}

//必须声明 Classes 否则 try..except 无法正常捕捉到异常
uses
  SysUtils,Classes;

var
  fInput,fOutput:TextFile;
  Line:String;
begin
  writeln('   Delphi comments "//" to Pascal comments "{}"');
  writeln('             Usage "DC2PC filoname"');
  writeln('            Powered by www.lab-z.com');

  //如果没有输入文件名
  if paramCount=0 then
    begin
      writeln('Please input file name!');
      exit;
    end;

  //打开输入文件
  AssignFile(fInput,ParamStr(1));
  try
    Reset(fInput);
  Except
    writeln('Opening file '+ParamStr(1)+' error!');
    exit;
  end;

  //替换后的结果放在 输入文件名.pas 文件中
  AssignFile(fOutput,ParamStr(1)+'.pas');
  rewrite(fOutput);

  //在输入文件中查找 // 的注释
  while NOT eof(fInput) do
     begin
       readln(fInput,Line);
       if pos('//',Line)<>0 then
         begin
           insert('{',Line,pos('//',Line));
           Line:=Line+'}';
         end;
       //writeln(Line);
       writeln(fOutput,Line);
     end;

  CloseFile(fInput);
  CloseFile(fOutput);
end.

 

上述代码在实际使用中还有一点小问题,比如 writeln(“abc //efg”); 这样的代码也会被替换,不过正常的代码应该不会有很多处这样的用法,出现问题手工修改一下就好了。

Delphi 在进行 shl 运算需要特别注意的地方

前几天在调试 2048 AI 程序的时候发现 Delphi 在处理 shl 超过32位时有着疑似bug的问题。

搜索一下得到如下的解答:

http://stackoverflow.com/questions/8127693/how-can-i-get-a-result-larger-than-232-from-shl

解决方法就是在做 shl 的时候做一次强制类型转换。

例如: n = Int64(2) shl 33

写一个简单的程序验证之,

program Project6;

{$APPTYPE CONSOLE}

uses
  SysUtils;

begin
  writeln(Format('%X',[1 shl 32]));
  writeln(Format('%X',[Int64(1) shl 32]));
  readln;
end.

 

delphishl

我试验了 Delphi 7/Delphi 10/Delphi 2010都一样,但是据说 XE2 之后修正了这个潜在的问题。