#include <cviauto.h>
#include "excelreport.h"
#include <ansi_c.h>
#include "User Interface Application.h"
#include <userint.h>
#include <utility.h>
#include <formatio.h>
#include <rs232.h>
#include "UART.h"
#include "../UART_Configure.h"
extern void SartOrStopCommunicationThread(BYTE Operate);
extern int panelMainHandle; // 主句柄
static int panelHandle_UART = 0;
static UserDCB dcb;
int GetComConfigure(UserDCB* pDcb);
void InitUart(void)
{
GetComConfigure( &dcb);
char ComName1[10];
char ComName2[10] = "COM";
Fmt (ComName1, "%s<%i", dcb.ComPort); /* result: "16" */
strcat(ComName2, ComName1);
int UartError = OpenComConfig( dcb.ComPort, ComName2, dcb.BaudRate, dcb.Parity, dcb.ByteSize, dcb.StopBits , 256, 256);
if (UartError)
{
DisplayRS232Error (UartError);
}else
{
SetComTime ( dcb.ComPort, 5.0); // 超时
SetXMode (dcb.ComPort, 0); // 禁止软件握手
SetCTSMode (dcb.ComPort, LWRS_HWHANDSHAKE_OFF);
FlushInQ (dcb.ComPort);
FlushOutQ (dcb.ComPort);
}
}
int GetComConfigure(UserDCB* pDcb)
{
CAObjHandle ComExcelHandle =0; // Excel object
CAObjHandle NewExcelWorkbookobject = 0; // Workbook
CAObjHandle NewExcelWorkSheetobject = 0; // WorkSheet
HRESULT ret = 0;
char ProjectDir[300]; // 工程目录
double dataArray[5];
memset(dataArray, 0, sizeof(dataArray));
memset(ProjectDir, 0, sizeof(ProjectDir));
ExcelRpt_ApplicationNew (0, &ComExcelHandle);
if(ret < 0)
{
MessagePopup("提示:", "读取配置参数失败!");
return -1;
}
GetProjectDir(ProjectDir);
strcat(ProjectDir, "\\配置文件");
ret = ExcelRpt_WorkbookOpen ( ComExcelHandle, ProjectDir, &NewExcelWorkbookobject);
if(ret < 0)
{
MessagePopup("提示:", "配置文件不存在!");
return -1;
}
ExcelRpt_GetWorksheetFromName (NewExcelWorkbookobject, "COM端口配置", &NewExcelWorkSheetobject);
ExcelRpt_ReadData ( NewExcelWorkSheetobject, "C1:C5", ExRConst_dataDouble, dataArray);
dcb.ComPort = (int)dataArray[0];
dcb.BaudRate = (int)dataArray[1];
dcb.ByteSize = (int)dataArray[2];
dcb.StopBits = (int)dataArray[3];
dcb.Parity = (int)dataArray[4];
ExcelRpt_WorkbookClose (NewExcelWorkbookobject, 0);
CA_DiscardObjHandle(NewExcelWorkbookobject);
CA_DiscardObjHandle(ComExcelHandle);
return 1;
}
int SendUartData(PRS422M_FRAME pSendFrame, WORD Length)
{
if (pSendFrame)
{
static WORD SendCounter = 0;
pSendFrame->m_struct.m_FrameCounter = (SendCounter++)& 0xFFFF;
ComWrt( dcb.ComPort, (PBYTE)pSendFrame, Length );
// 显示发送的FrameCounter.
static unsigned int SendFrameCounter = 0;
char buf[10];
Fmt (buf, "%s<%i", (SendFrameCounter++)); /* result: "16" */
SetCtrlVal (panelMainHandle, PANEL_TX_FRAME_COUNTER, buf);
}
return TRUE;
}
int ReadUartData(PBYTE pReceiveFrame)
{
if( PNULL == pReceiveFrame)
{
return FALSE;
}
WORD Length = GetInQLen ( dcb.ComPort);
if( RS422x_QUEUE_LENGTH < Length) // 如果断点或者程序未工作
{
FlushInQ (dcb.ComPort);
return 0;
}
ComRd (dcb.ComPort, pReceiveFrame, Length);
return (Length);
}
WORD GetUartRxData(void)
{
BYTE RxDataFromUartCom[RS422x_QUEUE_LENGTH];
memset(RxDataFromUartCom, 0, sizeof(RxDataFromUartCom));
WORD iRecvLen = ReadUartData(RxDataFromUartCom);
int i;
for(i=0;i<iRecvLen;i++)
{
uartQInsert(&RS422Queue_Get, RxDataFromUartCom[i]);
}
//////////////////////////////////////////////////////////////////////////
WORD retVal = UART0SeekPak(&RS422Queue_Get, &RS422_Comd_Queue);
return retVal;
}
void CloseComInQuitApp(void)
{
if(dcb.ComPort)
{
CloseCom( dcb.ComPort);
}
}
void CVICALLBACK UartConfigure (int menuBar, int menuItem, void *callbackData, int panel)
{
panelHandle_UART = LoadPanel(0, "UART_Configure.uir", PANEL_UART);
if( panelHandle_UART < 0);
DisplayPanel(panelHandle_UART);
SartOrStopCommunicationThread(0); // 关闭线程。。。
SetCtrlAttribute (tabPanelHandle[TAB_SW_CTRL], SW_TAB_TIMER, ATTR_ENABLED, 0); // 禁止发送定时器
CloseComInQuitApp(); //关闭串口
}
int CVICALLBACK CloseUartConfigPanel (int panel, int event, void *callbackData,int eventData1, int eventData2)
{
switch (event)
{
case EVENT_GOT_FOCUS:
break;
case EVENT_LOST_FOCUS:
break;
case EVENT_CLOSE:
DiscardPanel(panelHandle_UART);
break;
}
return 0;
}
int CVICALLBACK CloseComPort (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
static unsigned int ComStatus = 1;
if(ComStatus % 2)
{
OpenComConfig( dcb.ComPort, dcb.CommName, dcb.BaudRate, dcb.Parity, dcb.ByteSize, dcb.StopBits , 512, 512);
SetCtrlVal ( panelHandle_UART, PANEL_UART_UART_STATUS_STRING, "OPEN"); // 状态
SetCtrlAttribute ( panelHandle_UART, PANEL_UART_COMMANDBUTTON, ATTR_LABEL_TEXT, "关闭串口"); // 状态
SartOrStopCommunicationThread(1); // 启动线程。。。
SetCtrlAttribute (tabPanelHandle[TAB_SW_CTRL], SW_TAB_TIMER, ATTR_ENABLED, 1); // 使能发送定时器
}else
{
CloseCom( dcb.ComPort);
SetCtrlVal ( panelHandle_UART, PANEL_UART_UART_STATUS_STRING, "CLOSE"); // 状态
SetCtrlAttribute ( panelHandle_UART, PANEL_UART_COMMANDBUTTON, ATTR_LABEL_TEXT, "打开串口"); // 状态
}
ComStatus++;
break;
}
return 0;
}
int CVICALLBACK ConfigUart (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
GetCtrlVal ( panelHandle_UART, PANEL_UART_COM_NUM_RING, &dcb.ComPort); // 串口号
GetCtrlVal ( panelHandle_UART, PANEL_UART_BAUD_RATE_RING, &dcb.BaudRate); // 波特率
GetCtrlVal ( panelHandle_UART, PANEL_UART_DATA_BITS_RING, &dcb.ByteSize); // 数据位
GetCtrlVal ( panelHandle_UART, PANEL_UART_PARITY_BITS_RING, &dcb.Parity); // 校验位
GetCtrlVal ( panelHandle_UART, PANEL_UART_STOP_BITS_RING, &dcb.StopBits); // 停止位
GetLabelFromIndex (panelHandle_UART, PANEL_UART_COM_NUM_RING, dcb.ComPort-1, dcb.CommName); // 串口名称
SetCtrlVal ( panelHandle_UART, PANEL_UART_COM_NUM_STRING, dcb.CommName); // 串口号
SetCtrlVal ( panelHandle_UART, PANEL_UART_BAUD_RATE_NUMERIC, dcb.BaudRate); // 波特率
SetCtrlVal ( panelHandle_UART, PANEL_UART_DATA_BITS_NUMERIC, dcb.ByteSize); // 数据位
SetCtrlVal ( panelHandle_UART, PANEL_UART_PARITY_BITS_NUMERIC, dcb.Parity); // 校验位
SetCtrlVal ( panelHandle_UART, PANEL_UART_STOP_BITS_NUMERIC, dcb.StopBits); // 停止位
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Display error information to the user. */
/*---------------------------------------------------------------------------*/
void DisplayRS232Error (int RS232Error)
{
char ErrorMessage[200];
switch (RS232Error)
{
default :
if (RS232Error < 0)
{
Fmt (ErrorMessage, "%s<RS232 error number %i", RS232Error);
MessagePopup ("RS232 Message", ErrorMessage);
}
break;
case 0 :
MessagePopup ("RS232 Message", "No errors.");
break;
case -2 :
Fmt (ErrorMessage, "%s", "Invalid port number (must be in the "
"range 1 to 8).");
MessagePopup ("RS232 Message", ErrorMessage);
break;
case -3 :
Fmt (ErrorMessage, "%s", "No port is open.\n"
"Check