PC机与变频器的串行通信
1#变频器在运行状态下改变它的“设定频率”为35.00hz
方法如下:
35.00去掉小数为3500d=0dach
a=1=01h (变频器地址为“01h”)
k=04h (运行参数设定为“04h”)
p1p0=0001h (运行时设定频率为“0001h”)
d3=00h (数据高字节为“00h”)
d2=00h (数据次高字节为“00h”)
d1=0dh (数据次字节为“0dh”)
d0=ach (数据低字节为“ach”)
s=c9h (和校验字节为“c9h”)
(s=0bh+04h+00h+01h+00h+00h+00h+0dh+ach=c9h)
主机先后依次发送字节如下的数据包ascii:
3ah,30h,42h,30h,34h,30h,30h,30h,31h,30h,30h,30h,30h,30h,44h,41h,43h,43h,39h,0dh,0ah
变频器回复主机相同数据。
部分通信代码如下:
打开通讯端口代码
char *comno;
dcb dcb;
string temp;
temp=“com”+inttostr(rdcom-》itemindex+1);
comno=temp.c_str() ;
hcomm=createfile(comno,generic_
read|generic_write,0,null,open_existing,0,0);
if(hcomm==invalid_handle_value)
{
statusbar1-》simpletext=“打开通信端口错误!”;
return;
}
else
statusbar1-》simpletext=“端口已打开!”;
sleep(100);
getcommstate(hcomm,&dcb);
dcb.baudrate=cbr_9600;
dcb.bytesize =8;
dcb.parity =noparity;
dcb.stopbits =onestopbit;
setcommstate(hcomm,&dcb);
if(!setcommstate(hcomm,&dcb))
{
statusbar1-》simpletext=“通信端口设置错误!”;
closehandle(hcomm);
return;
}
发送数据代码
int i=0;
unsigned char sends[21];
unsigned long lrc,bs;
sends[0]=3ah; //header
sends[1]=30h; //a
sends[2]=31h;
sends[3]=30h;//k
sends[4]=34h;
sends[5]=30h; //p1
sends[6]=30h;
sends[7]=30h; //p0
sends[8]=31h;
sends[9]=30h; //d3
sends[10]=30h;
sends[11]=30h;//d2
sends[12]=30h;
sends[13]=30h; //d1
sends[14]=44h;
sends[15]=41h; //d0
sends[16]=43h;
sends[17]=43h; //s
sends[18]=39h;
sends[19]=0dh; //delimiter
sends[20]=0ah;
for(i=0;i++;i《21)
{
if(hcomm==0)
return;
writefile(hcomm,sends,1,&lrc,null);
}
接收数据代码
int ln;
unsigned long lrc,bs;
char inbuff[1024];
dword nbytesread,dwevent,dwerror;
comstat cs;
if(hcomm==0)
{
mreceive-》text=“读取过程有问题,已跳出!”;
return;
}
if(hcomm==invalid_handle_value)
{
mreceive-》text=“读取过程有问题,已跳出!”;
return;
}
clearcommerror(hcomm,&dwerror,&cs);
if(cs.cbinque)
{
readfile(hcomm,inbuff,cs.cbinque,&nbytesread,null);
inbuff[cs.cbinque]=` `;
mreceive-》text=inbuff;
interceptrece(mreceive-》text);
}
else
mreceive-》text=“未读取到数据!”;
以上代码在winxp sp2操作系统, c++builder6 编程环境下调试编译通过。
结语
通过对带有rs-485通信接口的日普系列变频器的研究,设计了可行的变频器网络监控系统方案,在c++builder编程环境下,利用api函数,实现了变频器各项参数的在线监测与控制。提高了变频器控制的自动化水平。
评论