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]=` 主站蜘蛛池模板: 宜兴市| 颍上县| 淮滨县| 富源县| 兴城市| 陇川县| 蒙山县| 新乐市| 孝昌县| 治多县| 兴城市| 邹平县| 大新县| 遂溪县| 乌兰县| 沿河| 新和县| 凉山| 江油市| 遂溪县| 当涂县| 嵊州市| 醴陵市| 德化县| 阿尔山市| 五华县| 吕梁市| 托里县| 灵璧县| 三亚市| 板桥市| 林口县| 隆子县| 辽宁省| 南漳县| 资兴市| 西吉县| 和平区| 齐齐哈尔市| 淮滨县| 云阳县|