500分求地磅仪表数据的读取源码,如果感觉分少可以再加!!!! ( 积分: 0 )

  • 主题发起人 主题发起人 winsssff
  • 开始时间 开始时间
W

winsssff

Unregistered / Unconfirmed
GUEST, unregistred user!
有一台电子地磅显示仪表(XK3190-A9耀华的)的参数说明如下:

1、串行通讯接口为15针RS232串口
2、所有数据均为ASCII码,每组数据由10位组成,第一位为起始位,第10位为结束位
3、通讯方式:连续方式
每帧数据由12组数据组成,格式如下

第X字节 内容及注解
1 02(XON) 开始
2 +或- 符号位
3 称重数据 高位
4 称重数据
5 称重数据
6 称重数据
7 称重数据
8 称重数据 低位
9 小数点位数 从右到左,(0~4)
10 异或校验 高四位
11 异或校验 低四位
12 03(XOFF) 结束

现将其接到计算机Com口,通过程序如何才能准确的读出电子地磅的数据?
最好是使用 pcomm 的源码,如果是spcomm,请说明其属性设置
 
我觉得使用spcomm可以这样实现,

procedure TForm1.Comm1ReceiveData(Sender:TObject; Buffer:Pointer;
BufferLength:Word);
var t:pchar;
b:integer;
t1:string;
k:single;
p1:integer;
begin
//通讯返回字符串。|ST,GS,+0003.30 g
t:=Buffer;
t1:=t;
k:=0;

//新的电子称程序
//取小数点前4位,后3位
b:=Pos(chr(2),t1);
try
k:=strtofloat(Trim(copy(t1,b+1,7)));
except k:=0;
end;
k:=k/Power(10,strtoint(copy(t1,b+8,1)));//返回的重量值
end;
通讯参数根据你的称决定,另外没有校验的代码 ,你需要可以自己加上。
spcomm的属性,你可以使用默认值 。
 
通讯返回的都是Ascii码,十六进制下如果返回0应为
02 2B 30 30 30 30 30 30 30 31 42 03
还有comm的 receivedata事件触发有问题,是不是某个属性设置有问题
 
不错,我以前也写过其它类型的.不过那个是数据反过来.
 
我觉得应该是这样吧,你这样的16进制,字符串是不是就是"+0000000"这样?
另外,称是连续发出的,你可以定义公共变量追加在后面,
tstr:=tstr+"新接收的串",然后你再找开始标志。02,如果保证准确,同时再判断该串是不是有结束标志03,如果是,本串是完整的。可以进行数据转换。
 
只是我用 spcomm 接收时接收速度太慢,是不是需要修改哪个属性
用pcomm的 sio_read那我的buffersize应该设多大才好

delphigbg,能不能给出接收和处理的源码
 
接收速度慢和你使用控件无关,你可能需要设置设备的通讯参数。
 
我用串口测试工具测试显示很正常,但是用spcomm写的程序就速度太慢,是不是与xonlimit和xofflimit属性值有关,因为测试工具显示正常,但是用spcomm写的程序只有在满500字符后才会触发 receivedata显示
还有 (所有数据均为ASCII码,每组数据由10位组成,第一位为起始位,第10位为结束位),那我接收的数据是不是只有中间的8位数据位,因为我用串口测试工具接收显示的数据都很正确,但是用spcomm接收的数据有点看不明白,现在又没有仪表可以测试,真的很郁闷
 
那位高手能够给出接收处理的源码啊,分不够可以再加
 
你好!不知你搞定了没有,如果真的需要,跟我联系,
100%让你搞定。
qq:33586813
 
spcomm触发,除了满你所说的500字符,还有时间间隔吧,到时间也会触发,你检查看看时间超时属性。默认属性,不应该像你说的这样。
 
unit GBSetFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,inifiles, ExtCtrls, SPComm, Buttons, ComCtrls, ToolWin,
ImgList;

type
TGBSetForm = class(TForm)
Panel1: TPanel;
Label3: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Bevel1: TBevel;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
z: TRadioButton;
q: TRadioButton;
Panel2: TPanel;
Label11: TLabel;
B: TRadioButton;
A: TRadioButton;
ImageList1: TImageList;
Edit1: TComboBox;
LB: TPanel;
ToolBar1: TToolBar;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton2: TToolButton;
ToolButton1: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
Comm1: TComm;
Memo1: TMemo;
procedure WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt:String);
procedure ToolButton3Click(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure zClick(Sender: TObject);
procedure qClick(Sender: TObject);
procedure Edit3KeyPress(Sender: TObject; var Key: Char);
procedure Edit4KeyPress(Sender: TObject; var Key: Char);
procedure Edit5KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure ToolButton6Click(Sender: TObject);


private
{ Private declarations }
public
{ Public declarations }

end;

var
GBSetForm: TGBSetForm;
Filename:string;
Comm,CommName,BaudRate,Timeout,start,len,zt:String;

implementation
var
CommSetIni:Tinifile;
{$R *.dfm}

procedure TGBSetForm.WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt:String);
begin
CommSetIni.writestring(Comm,'CommName',CommName);
CommSetIni.writestring(Comm,'BaudRate',BaudRate);
CommSetIni.writestring(Comm,'Timeout',Timeout);
CommSetIni.writestring(Comm,'start',start);
CommSetIni.writestring(Comm,'len',len);
CommSetIni.writestring(Comm,'zt',zt);
end;


function Codown(Str: string): String;
Var I: Integer;
S:string;
begin
for I:=Length(Trim(Str)) downto 1 do
begin
S:=copy(Trim(Str), i , 1);
Result:=Result+S;
end;
end;

procedure TGBSetForm.ToolButton3Click(Sender: TObject);
begin
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
CommName:=Trim(Edit1.Text) ;
BaudRate:=Trim(Edit2.Text );
Timeout:= Trim(Edit3.Text );
start:=Trim(Edit4.Text );
Len:=Trim(Edit5.Text );
IF A.Checked then zt:='0' Else Zt:='1';
WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt);
Application.MessageBox('保存成功!!!','提示',64+MB_OK);
end;

procedure TGBSetForm.ToolButton1Click(Sender: TObject);
begin
comm1.StopComm ;
end;

procedure TGBSetForm.ToolButton2Click(Sender: TObject);
begin
comm1.CommName:=trim(edit1.Text);
comm1.BaudRate:=strtoint(trim(edit2.Text));
comm1.ReadIntervalTimeout:=strtoint('0'+trim(edit3.Text));
comm1.StopComm ;
Try
comm1.StartComm ;
ExCept
comm1.StopComm ;
end;
end;

procedure TGBSetForm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
type
IArr = ^Integer;
var
RevP : array [1..4096] of byte;
st,l,i : integer;
CommRevStr : ShortString;
Str : String;
begin
SetLength(CommRevStr,BufferLength);
Move(IArr(buffer)^,Revp,bufferLength);
For i:=1 to BufferLength do
begin
CommrevStr := Char(Revp);
end;

Memo1.Lines.Add(CommrevStr);
Memo1.Invalidate;

st:=strtoint('0'+trim(edit4.Text));
l:=strtoint('0'+trim(edit5.Text));

Str:=copy(CommrevStr,st,l);
IF length(Str)=l then
Begin
IF A.Checked then LB.Caption :=Str
Else LB.Caption:=CoDown(Str);
end;

end;

procedure TGBSetForm.FormCreate(Sender: TObject);
begin
FileName:=ExtractFilePath(paramstr(0))+'CommSet.DLL';
CommSetIni:=TInifile.Create(FileName);
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
Edit1.Text :=CommSetIni.ReadString (Comm,'CommName','COM1');
Edit2.Text :=CommSetIni.ReadString(Comm,'BaudRate','1200');
Edit3.Text :=CommSetIni.ReadString(Comm,'Timeout','50');
Edit4.Text :=CommSetIni.ReadString(Comm,'start','1');
Edit5.Text :=CommSetIni.ReadString(Comm,'len','6');
IF Trim(CommSetIni.ReadString(Comm,'zt','0'))='0' then A.Checked Else B.Checked ;

LB.Color :=clblack;

Application.Title := Trim('COM测试程序');

end;

procedure TGBSetForm.zClick(Sender: TObject);
begin
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
Edit1.Text :=CommSetIni.ReadString (Comm,'CommName','COM1');
Edit2.Text :=CommSetIni.ReadString(Comm,'BaudRate','1200');
Edit3.Text :=CommSetIni.ReadString(Comm,'Timeout','50');
Edit4.Text :=CommSetIni.ReadString(Comm,'start','1');
Edit5.Text :=CommSetIni.ReadString(Comm,'len','6');
IF Trim(CommSetIni.ReadString(Comm,'zt','0'))='0' then A.Checked:=True Else B.Checked:=True ;
end;

procedure TGBSetForm.qClick(Sender: TObject);
begin
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
Edit1.Text :=CommSetIni.ReadString (Comm,'CommName','COM1');
Edit2.Text :=CommSetIni.ReadString(Comm,'BaudRate','1200');
Edit3.Text :=CommSetIni.ReadString(Comm,'Timeout','50');
Edit4.Text :=CommSetIni.ReadString(Comm,'start','1');
Edit5.Text :=CommSetIni.ReadString(Comm,'len','6');
IF Trim(CommSetIni.ReadString(Comm,'zt','0'))='0' then A.Checked:=True Else B.Checked:=True ;
end;

procedure TGBSetForm.Edit3KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;

procedure TGBSetForm.Edit4KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;

procedure TGBSetForm.Edit5KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;

procedure TGBSetForm.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9',#8]) then
key:=#0;
end;

procedure TGBSetForm.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','C','O','M','c','o','m',#8]) then
key:=#0;
end;

procedure TGBSetForm.ToolButton6Click(Sender: TObject);
begin
Close;
end;

end.
 
unit GBSetFrm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,inifiles, ExtCtrls, SPComm, Buttons, ComCtrls, ToolWin,
ImgList;

type
TGBSetForm = class(TForm)
Panel1: TPanel;
Label3: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Bevel1: TBevel;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
z: TRadioButton;
q: TRadioButton;
Panel2: TPanel;
Label11: TLabel;
B: TRadioButton;
A: TRadioButton;
ImageList1: TImageList;
Edit1: TComboBox;
LB: TPanel;
ToolBar1: TToolBar;
ToolButton3: TToolButton;
ToolButton4: TToolButton;
ToolButton2: TToolButton;
ToolButton1: TToolButton;
ToolButton5: TToolButton;
ToolButton6: TToolButton;
Comm1: TComm;
Memo1: TMemo;
procedure WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt:String);
procedure ToolButton3Click(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure zClick(Sender: TObject);
procedure qClick(Sender: TObject);
procedure Edit3KeyPress(Sender: TObject; var Key: Char);
procedure Edit4KeyPress(Sender: TObject; var Key: Char);
procedure Edit5KeyPress(Sender: TObject; var Key: Char);
procedure Edit2KeyPress(Sender: TObject; var Key: Char);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
procedure ToolButton6Click(Sender: TObject);


private
{ Private declarations }
public
{ Public declarations }

end;

var
GBSetForm: TGBSetForm;
Filename:string;
Comm,CommName,BaudRate,Timeout,start,len,zt:String;

implementation
var
CommSetIni:Tinifile;
{$R *.dfm}

procedure TGBSetForm.WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt:String);
begin
CommSetIni.writestring(Comm,'CommName',CommName);
CommSetIni.writestring(Comm,'BaudRate',BaudRate);
CommSetIni.writestring(Comm,'Timeout',Timeout);
CommSetIni.writestring(Comm,'start',start);
CommSetIni.writestring(Comm,'len',len);
CommSetIni.writestring(Comm,'zt',zt);
end;


function Codown(Str: string): String;
Var I: Integer;
S:string;
begin
for I:=Length(Trim(Str)) downto 1 do
begin
S:=copy(Trim(Str), i , 1);
Result:=Result+S;
end;
end;

procedure TGBSetForm.ToolButton3Click(Sender: TObject);
begin
IF Z.Checked then Comm:='Comm1' Else Comm:='Comm2';
CommName:=Trim(Edit1.Text) ;
BaudRate:=Trim(Edit2.Text );
Timeout:= Trim(Edit3.Text );
start:=Trim(Edit4.Text );
Len:=Trim(Edit5.Text );
IF A.Checked then zt:='0' Else Zt:='1';
WriteIni(Comm,CommName,BaudRate,Timeout,start,len,zt);
Application.MessageBox('保存成功!!!','提示',64+MB_OK);
end;

procedure TGBSetForm.ToolButton1Click(Sender: TObject);
begin
comm1.StopComm ;
end;

procedure TGBSetForm.ToolButton2Click(Sender: TObject);
begin
comm1.CommName:=trim(edit1.Text);
comm1.BaudRate:=strtoint(trim(edit2.Text));
comm1.ReadIntervalTimeout:=strtoint('0'+trim(edit3.Text));
comm1.StopComm ;
Try
comm1.StartComm ;
ExCept
comm1.StopComm ;
end;
end;

procedure TGBSetForm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
type
IArr = ^Integer;
var
RevP : array [1..4096] of byte;
st,l,i : integer;
CommRevStr : ShortString;
Str : String;
begin
SetLength(CommRevStr,BufferLength);
Move(IArr(buffer)^,Revp,bufferLength);
For i:=1 to BufferLength do
begin
CommrevStr := Char(Revp);
end;

Memo1.Lines.Add(CommrevStr);
Memo1.Invalidate;

st:=strtoint('0'+trim(edit4.Text));
l:=strtoint('0'+trim(edit5.Text));

Str:=copy(CommrevStr,st,l);
IF length(Str)=l then
Begin
IF A.Checked then LB.Caption :=Str
Else LB.Caption:=CoDown(Str);
end;

end;
 
我这几天也用spcomm写了一个托利多电子秤8142pro的数据采集程序,显示都很正常,但就是接收速度太慢,老是要延时几十秒,还没有搞清楚这是怎么回事,郁闷中。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
S
回复
0
查看
928
SUNSTONE的Delphi笔记
S
后退
顶部