您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > 基于labwindows的串口通信
#includeansi_c.h#includeformatio.h#includeutility.h#includers232.h#includecvirte.h#includeuserint.h#include串口通信.h#definetext_length2048staticintconfig_handle;config_flag;portindex;port_open;RS232Error;comport;baudrate;parity;databits;stopbits;inputq;outputq;xmode;ctsmode;type;stringsize;bytes_sent;send_byte;send_mode;read_cnt;bytes_read;chardevicename[30];charsend_data[text_length];charread_data[text_length];chartbox_read_data[text_length];doubletimeout;staticvoid*callbackdata;staticintpanelHandle,graphpanel,config;voidComCallback(intCOMport,inteventMask,void*callbackdata);voidSetConfigParms();voidGetConfigParms();voidEnablePanelControls(int);voidSendAscii(void);voidSendByte(void);voidDisplayRS232Error(void);voidSetConfigParms(){SetCtrlVal(config_handle,CONFIG_COMPORT,comport);SetCtrlVal(config_handle,CONFIG_BAUDRATE,baudrate);SetCtrlVal(config_handle,CONFIG_PARITY,parity);SetCtrlVal(config_handle,CONFIG_DATABITS,databits);SetCtrlVal(config_handle,CONFIG_STOPBITS,stopbits);SetCtrlVal(config_handle,CONFIG_INPUTQ,inputq);SetCtrlVal(config_handle,CONFIG_OUTPUTQ,outputq);SetCtrlVal(config_handle,CONFIG_CTSMODE,ctsmode);SetCtrlVal(config_handle,CONFIG_XMODE,xmode);SetCtrlIndex(config_handle,CONFIG_COMPORT,portindex);}voidGetConfigParms(){GetCtrlVal(config_handle,CONFIG_COMPORT,&comport);GetCtrlVal(config_handle,CONFIG_BAUDRATE,&baudrate);GetCtrlVal(config_handle,CONFIG_PARITY,&parity);GetCtrlVal(config_handle,CONFIG_DATABITS,&databits);GetCtrlVal(config_handle,CONFIG_STOPBITS,&stopbits);GetCtrlVal(config_handle,CONFIG_INPUTQ,&inputq);GetCtrlVal(config_handle,CONFIG_OUTPUTQ,&outputq);GetCtrlVal(config_handle,CONFIG_CTSMODE,&ctsmode);GetCtrlVal(config_handle,CONFIG_XMODE,&xmode);GetCtrlVal(config_handle,CONFIG_TIMEOUT,&timeout);GetCtrlIndex(config_handle,CONFIG_COMPORT,&portindex);#ifdef_NI_unix_devicename[0]=0;#elseGetLabelFromIndex(config_handle,CONFIG_COMPORT,portindex,devicename);#endif}voidEnablePanelControls(intenable){SetCtrlAttribute(panelHandle,PANEL_SENDMODE,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_SENDDATA,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_SENDNUMBER,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_RECEIVENUMBER,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_RECEIVEDATA,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_STOPRECEIVE,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_CHECKDATA,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_CHECKGRAPH,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_FLUSHINPUT,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_FLUSHOUTPUT,ATTR_DIMMED,enable);SetCtrlAttribute(panelHandle,PANEL_DATA,ATTR_DIMMED,enable);}intmain(intargc,char*argv[]){if(InitCVIRTE(0,argv,0)==0)return-1;/*outofmemory*/if((panelHandle=LoadPanel(0,串口通信.uir,PANEL))0)return-1;DisplayPanel(panelHandle);RunUserInterface();DiscardPanel(panelHandle);return0;}intCVICALLBACKconfigcallback(intpanel,intcontrol,intevent,void*callbackData,inteventData1,inteventData2){switch(event){caseEVENT_COMMIT:config_handle=LoadPanel(panelHandle,串口通信.uir,CONFIG);InstallPopup(config_handle);if(config_flag)/*Configurationdoneatleastonce.*/SetConfigParms();elseconfig_flag=1;break;}return0;}intCVICALLBACKsendmodecallback(intpanel,intcontrol,intevent,void*callbackData,inteventData1,inteventData2){switch(event){caseEVENT_COMMIT:GetCtrlVal(panelHandle,PANEL_SENDMODE,&send_mode);if(send_mode){SetCtrlAttribute(panelHandle,PANEL_SENDDATAPLOT,ATTR_DIMMED,0);SetCtrlAttribute(panelHandle,PANEL_DATA,ATTR_DIMMED,1);}else{SetCtrlAttribute(panelHandle,PANEL_SENDDATAPLOT,ATTR_DIMMED,1);SetCtrlAttribute(panelHandle,PANEL_DATA,ATTR_DIMMED,0);}}return0;}intCVICALLBACKsendcallback(intpanel,intcontrol,intevent,void*callbackData,inteventData1,inteventData2){switch(event){caseEVENT_COMMIT:GetCtrlVal(panelHandle,PANEL_SENDMODE,&type);if(type)SendAscii();elseSendByte();RS232Error=ReturnRS232Err();if(RS232Error)DisplayRS232Error();SetCtrlVal(panelHandle,PANEL_SENDNUMBER,bytes_sent);break;}return0;}voidSendAscii(){GetCtrlVal(panelHandle,PANEL_SENDDATAPLOT,send_data);stringsize=StringLength(send_data);bytes_sent=ComWrt(comport,send_data,stringsize);}voidSendByte(){GetCtrlVal(panelHandle,PANEL_DATA,&send_byte);bytes_sent=ComWrtByte(comport,send_byte);}intCVICALLBACKreceivecallback(intpanel,intcontrol,intevent,void*callbackData,inteventData1,inteventData2){switch(event){caseEVENT_COMMIT:InstallComCallback(comport,LWRS_RECEIVE,2,0,ComCallback,callbackdata);caseEVENT_RIGHT_CLICK:break;}return0;}voidComCallback(intCOMport,inteventMask,void*callbackdata){intinputqueuelength;intbytesread;charbuffer[512];char*matchedchar=0;doublevalue;inti;inputqueuelength=GetInQLen(comport);bytesread=ComRd(comport,buffer,inputqueuelength);for(i=0;i=bytesread;i++){matchedchar=buffer+i;CopyString(tbox_read_data,0,matchedchar,0,inputqueuelength);SetCtrlVal(panelHandle,PANEL_RECEIVEDATAPLOT,tbox_read_data);break;}ArrayToFile(c:\\Users\\wanglei\\Desktop\\串口数据.txt,tbox_read_data,VAL_CHAR,inpu
本文标题:基于labwindows的串口通信
链接地址:https://www.777doc.com/doc-6242693 .html