Delphi CPort无法在windows 10中正常工作
已解决 只要更新Windows10,问题就会消失Delphi CPort无法在windows 10中正常工作,delphi,delphi-7,windows-10,tcomport,Delphi,Delphi 7,Windows 10,Tcomport,已解决 只要更新Windows10,问题就会消失 我有一个用Delphi7开发的旧程序,它使用TComPort进行串行通信 在windows 10上,t要枚举端口,它不会返回端口号 从windows xp到windows 8.1 u1,该程序运行良好 因此,Windows 10和tImport之间存在不兼容,设置兼容性或以管理员身份运行程序不会改变任何内容 我该怎么修理? 我必须使用另一个库进行串行通信 这是我做的一个测试 unit Unit1; interface uses
我有一个用Delphi7开发的旧程序,它使用TComPort进行串行通信 在windows 10上,t要枚举端口,它不会返回端口号 从windows xp到windows 8.1 u1,该程序运行良好 因此,Windows 10和tImport之间存在不兼容,设置兼容性或以管理员身份运行程序不会改变任何内容 我该怎么修理? 我必须使用另一个库进行串行通信 这是我做的一个测试
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CPort;
type
TForm1 = class(TForm)
ComPort1: TComPort;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var List:TStringList;
sPortName:String;
I:integer;
begin
List := TStringList.Create;
try
EnumComPorts( List );
For I := 0 to List.Count-1 do
begin
sPortName := Trim(List[I]);
Memo1.Lines.Add(sPortName);
end;
finally
List.Free;
end;
end;
end.
这就是结果
字符串“sPortName”的长度为3个字符。我以前遇到过导入返回端口名的问题。t导入在Windows 10中运行良好。我发现有必要对每个返回的端口名使用Trim(),因为在端口名的末尾可以返回0个字符。您没有提供任何有用的调试信息,但这可能会导致您的问题。解决办法很简单。我有:
List := TStringList.Create;
try
EnumComPorts( List );
For I := 0 to List.Count-1 do
begin
sPortName := Trim(List[I]);
<<< do something with sPortName >>>
end;
finally
List.Free;
end;
List:=TStringList.Create;
尝试
枚举组件(列表);
对于I:=0到List.Count-1 do
开始
sPortName:=饰件(列表[I]);
>
结束;
最后
列表。免费;
结束;
ComPort从注册表项HKEY\U LOCAL\U MACHINE“HARDWARE\DEVICEMAP\SERIALCOMM”获取已知串行端口。它存在于W10中吗?是否由于权限规则而可用?是的,此键具有以下值\Device\USBSER000 COM5\Device\USBSER001 COM6,但当我以两个没有编号的COM端口列表的形式启动程序时。像“COM”而不是“COM5”一样,尝试一些调试来调查哪里出错了。tImport的EnumComPorts有一个unicode问题。也许这有关系?请参阅。它不起作用,现在我更改了,带有示例EnumComPorts
的初始帖子将添加#0和随机数据,因为此函数中存在unicode错误<代码>修剪并不总是有效。正确的解决方案是修改CPort.pas
,请参阅。