代码拉取完成,页面将自动刷新
unit frmMainP;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, CPort;
type
TfrmMain = class(TForm)
grp1: TGroupBox;
btnTest: TButton;
lblWeight: TLabel;
btnClose: TButton;
btnStop: TButton;
comPort: TComPort;
lblMsg: TLabel;
chkSign: TCheckBox;
btnSet: TButton;
chkAll: TCheckBox;
procedure btnTestClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure btnStopClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure comPortRxChar(Sender: TObject; Count: Integer);
procedure btnCloseClick(Sender: TObject);
procedure comPortAfterClose(Sender: TObject);
procedure comPortAfterOpen(Sender: TObject);
procedure btnSetClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.btnTestClick(Sender: TObject);
begin
if not comPort.Connected then
begin
comPort.Open;
lblMsg.Caption := '打开!';
end;
end;
procedure TfrmMain.FormShow(Sender: TObject);
begin
if FileExists('./ComSetup.ini') then
comPort.LoadSettings(stIniFile, './ComSetup.ini')
else
ShowMessage('请选设置参数');
lblWeight.Caption := '';
lblMsg.Caption := '准备好了!';
btnStop.Enabled := False;
end;
procedure TfrmMain.btnStopClick(Sender: TObject);
begin
if comPort.Connected then
begin
comPort.Close;
lblMsg.Caption := '关闭!';
lblWeight.Caption := '';
end;
end;
procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if comPort.Connected then comPort.Close;
end;
procedure TfrmMain.comPortRxChar(Sender: TObject; Count: Integer);
Function getNum(cStr:string):string;
var
tmpStr:string;
begin
result := '';
//删除STX(0x02)及(0x2c),两种方式。length(cStr) =>17 查托利多手册共18位,有效17位
//变成这样: [-,0xxx1401xxx000CR] -> [0xxx1401xxx000CR] <- CR表示回车
if pos(#2,cStr)>0 then //pos(#2)=1 1即 copy(cStr,3,14);
tmpStr:=copy(cStr,pos(#2,cStr)+length(#2)+1,length(cStr)-pos(#2,cStr)-length(#2)-1);
//tmpStr:=copy(cStr,pos(#2,cStr)+2,length(cStr)-3);
if pos(#$D,tmpStr)>0 then // pos(#$D) => 15, 删除回车,其中:#$D => ASCII #13
tmpStr:=copy(tmpStr,1,pos(#$D,tmpStr)-1); //这时[0xxx1401xxx000](无CR)
// pos(' ') => 2
if pos(' ',tmpStr)>0 then
//pos处加1删除一位状态码,
tmpStr:=copy(tmpStr,pos(' ',tmpStr)+1,length(tmpStr)-length(' ')-1); //[xx1401xxx000]
if pos('000',tmpStr)>0 then
tmpStr:=copy(tmpStr,1,pos('000',tmpStr)-1); //[xx1401xxx]
tmpStr:=trim(tmpStr);
if (tmpStr<>'') then
begin
// 仅保留两位小数
tmpStr:=copy(tmpStr,1,length(tmpStr)-2)+'.'+copy(tmpStr,length(tmpStr)-1,2);
result:=tmpstr;
end;
Result := tmpStr;
end;
var
cStr:String;
begin
sleep(50);
comPort.ReadStr(cStr, Count);
if chkSign.Checked then //选择了信号, 输出原始字符
begin
lblWeight.Font.Size := 20;
lblWeight.Font.Name := 'Tahoma';
lblWeight.Font.Color := clRed;
//查看所有字符,空格替换成零
if chkAll.Checked then cStr := StringReplace(cStr, ' ', '0', [rfReplaceAll]);
lblWeight.Caption := cStr; //输出原始字符
lblMsg.Caption := IntToStr(Length(cStr)); //显示字符长度
end
else
begin
lblWeight.Caption := getNum(cStr);
lblWeight.Font.Size := 50;
lblWeight.Font.Name := 'PMingLiU';
lblWeight.Font.Color := clRed;
end;
cStr:='';
end;
procedure TfrmMain.btnCloseClick(Sender: TObject);
begin
close;
end;
procedure TfrmMain.comPortAfterClose(Sender: TObject);
begin
btnTest.Enabled := True;
btnStop.Enabled := False;
end;
procedure TfrmMain.comPortAfterOpen(Sender: TObject);
begin
btnTest.Enabled := False;
btnStop.Enabled := True;
end;
procedure TfrmMain.btnSetClick(Sender: TObject);
begin
comPort.ShowSetupDialog;
comPort.StoreSettings(stIniFile, './ComSetup.ini');
end;
end.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。