您当前的位置:五五电子网电子知识plc技术PLC入门VC环境下PLC与上位机的实时通信 . 正文
VC环境下PLC与上位机的实时通信 .

VC环境下PLC与上位机的实时通信 .

点击数:7487 次   录入时间:03-04 11:58:51   整理:http://www.55dianzi.com   PLC入门
// 首先初始化通信端口 
bool cplccommdlg::oninitdialog()
{……
m_mycom.setcommport(1);  file://选择com1
m_mycom.setinbuffersize(1024); file://设置输入缓冲区的大小
m_mycom.setoutbuffersize(512);  file://设置输出缓冲区的大小
m_mycom.setinputmode(1);//设置数据获取方式
m_mycom.setsettings("9600,e,7,1");//设置通信参数
m_mycom.setinputlen(0);      file://设置读取方式
if(!m_mycom.getportopen() );   
m_mycom.setportopen (true);   file://打开串口
stationno=0x30;
if(settimer(1,m_usertime,null)==0)
afxmessagebox("error setting,please check it!")

file://消息处理函数ontimer
void cplccommdlg::ontimer(uint nidevent) 

 byte receivedata[1024]; 
byte senddata[]={`0x05`,`0`,`i`,`f`,`f`,`w`,`r`,`a`,`y`,`0`,`0`,`0`,`0`,
`0`,`1`,`sumh`,`suml`};
if(stationno<0x31)
stationno++;
else 
stationno=0x30;
senddata[3]= stationno;
sumcheck(senddata,17)
 cbytearray array;
array.removeall();
array.setsize(17);
for(int k=0;k<17;k++)

array.setat(k,senddata[k]);

m_mycom.setoutput(colevariant(array));//发送命令
 ……//数据接收处理及加入数据库操作

file://和校验函数sumcheck
void cplccommdlg::sumcheck(byte temp[],int n)

int i;
byte sum=0x00,sumh=0xf0,suml=0x0f;
for(i=1;i<=n-3;i++)
sum+=temp[i];
suml= suml∑
sumh= sumh∑
sumh= sumh>>4;
temp[n-1]= suml;
temp[n-2]= sumh;

 
5  结论
      串行通信是目前计算机与其他设备之间重要的通信手段之一,本程序作为小型监控系统的重要组成部分,实现了上位机对多台plc的实时监控,传送数据准确,程序运行稳定。整个控制系统方便可靠,同时又节省了大笔投资,具有相当大的实用性。

上一页  [1] [2] 


本文关键字:通信  PLC入门plc技术 - PLC入门

《VC环境下PLC与上位机的实时通信 .》相关文章>>>