PC機與變頻器的串行通信
1#變頻器在運行狀態(tài)下改變它的“設定頻率”為35.00hz
方法如下:
35.00去掉小數(shù)為3500d=0dach
a=1=01h (變頻器地址為“01h”)
k=04h (運行參數(shù)設定為“04h”)
p1p0=0001h (運行時設定頻率為“0001h”)
d3=00h (數(shù)據(jù)高字節(jié)為“00h”)
d2=00h (數(shù)據(jù)次高字節(jié)為“00h”)
d1=0dh (數(shù)據(jù)次字節(jié)為“0dh”)
d0=ach (數(shù)據(jù)低字節(jié)為“ach”)
s=c9h (和校驗字節(jié)為“c9h”)
?。╯=0bh+04h+00h+01h+00h+00h+00h+0dh+ach=c9h)
主機先后依次發(fā)送字節(jié)如下的數(shù)據(jù)包ascii:
3ah,30h,42h,30h,34h,30h,30h,30h,31h,30h,30h,30h,30h,30h,44h,41h,43h,43h,39h,0dh,0ah
變頻器回復主機相同數(shù)據(jù)。
部分通信代碼如下:
打開通訊端口代碼
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;
}
發(fā)送數(shù)據(jù)代碼
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);
}
接收數(shù)據(jù)代碼
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]=`看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();