//*********************************************************
// Labwindows/cvi8.5 RS232串口通讯测试程序
// szlihongtao
// 2010-06-20
//*********************************************************
#include <rs232.h>
#include <ansi_c.h>
#include <cvirte.h>
#include <userint.h>
#include <formatio.h>
#include "sample5.h"
//*********************************************************
static int panelHandle;
int comselect;
int status;
char info[20],readBuf[20];
//*********************************************************
// 串口设置初始化
// 返回1表示设置成功
// 返回0表示出错
//*********************************************************
unsigned int init_comm(void)
{
unsigned int btlselect,jhselect,longselect,stopselect;
unsigned int const arr_baud[]={9600,1200,2400,4800,9600,19200};
GetCtrlVal(PANEL,PANEL_COMSELECT,&comselect);//选择串行口
GetCtrlVal(PANEL,PANEL_BTLSELECT,&btlselect);//选择波特率
btlselect=arr_baud[btlselect];
GetCtrlVal(PANEL,PANEL_JYSELECT,&jhselect);//选择奇偶校验模式
GetCtrlVal(PANEL,PANEL_LONGSELECT,&longselect);//选择数据位长度
GetCtrlVal(PANEL,PANEL_STOPSELECT,&stopselect);//选择停止位
//设置和打开串行口
status=OpenComConfig(comselect,"",btlselect,jhselect,longselect,stopselect,32767,32767);
if(status!=0)
{
MessagePopup("Error","config failed!");
return 0;
}
SetCTSMode(comselect,LWRS_HWHANDSHAKE_OFF);
FlushInQ(comselect); // 清空输入和输出缓冲区
FlushOutQ(comselect);
SetComTime (comselect, 0.3); // 设置串口超时为0.3s
return 1;
}
//*********************************************************
// 主程序
//*********************************************************
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "sample5.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
return 0;
}
//*********************************************************
// 发送数据
//*********************************************************
int CVICALLBACK Send (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
if (event==EVENT_COMMIT)
{
status=init_comm(); // 串口设置初始化
if(status==0)
return 0;
GetCtrlVal(PANEL,PANEL_SENDTEXT,info);//获取发送信息文本
status=ComWrt(comselect,info,strlen(info));//发送
if(status!=strlen(info))
MessagePopup("Error","Send data failed!");
}
return 0;
}
//*********************************************************
// 接收/显示数据
//*********************************************************
int CVICALLBACK Recieve (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
//接收
status=ComRd(comselect,readBuf,strlen(info));
if(status==0)
{
MessagePopup("Error","Read data failed!");
break;
}
//将接收信息写入文本框
SetCtrlVal(PANEL,PANEL_RECIVETEXT,readBuf);
SetActiveCtrl(PANEL,PANEL_RECIVETEXT);
break;
}
}
return 0;
}
//*********************************************************
// 退出
//*********************************************************
int CVICALLBACK Quit (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
//*********************************************************
// 清除发送缓冲显示区
//*********************************************************
int CVICALLBACK Clearsend (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
//清空发送文本框
ResetTextBox(PANEL,PANEL_SENDTEXT,"\0");
break;
}
}
return 0;
}
//*********************************************************
// 清除接收缓冲显示区
//*********************************************************
int CVICALLBACK Clearrecive (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
{
//清空接收文本框
ResetTextBox(PANEL,PANEL_RECIVETEXT,"\0");
break;
}
}
return 0;
}
//*********************************************************
//*********************************************************
//*********************************************************
用户425825 2012-9-6 18:54
用户1585902 2012-7-16 19:20