?? pc2plcview.cpp
字號(hào):
{
blnError=true;
break;
}
}
if (blnError)//若blnError被置1,說(shuō)明通信存在故障,則退出本次操作
{
myEdit.SetSel(1000000,1000000);
strDis="";
strDis+=" 通信錯(cuò)誤\15\12\15\12";
strDis.MakeUpper();
myEdit.ReplaceSel(strDis);
Beep(1000,50);
blnError=false;
return;
}
//數(shù)據(jù)已經(jīng)讀回,并且已經(jīng)校驗(yàn)正確
myWByte[0]=6; //回應(yīng)ACK
dataWLen=1;
myEdit.SetSel(1000000,1000000);
strDis="";
strDis+="<-- [ACK]\15\12";
strDis.MakeUpper();
myEdit.ReplaceSel(strDis);
intWrite=0;
if (!myfuncWrite())
{
myEdit.SetSel(1000000,1000000);
strDis="";
strDis+=" 通信錯(cuò)誤\15\12\15\12";
myEdit.ReplaceSel(strDis);
Beep(1000,50);
return;
}
myEdit.SetSel(1000000,1000000);
strDis="";
strDis+=" 接收完畢!\15\12\15\12";
myEdit.ReplaceSel(strDis); //顯示成功信息
Beep(1000,50);
}
int CPC2PLCView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEditView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CEdit& myEdit=this->GetEditCtrl( );
myEdit.SetReadOnly (true); //將顯示區(qū)域設(shè)為只讀
hWnd=GetSafeHwnd(); //獲取當(dāng)前窗口的句柄
blnOpened=false;
return 0;
}
//*****************************
//****讀線程函數(shù)****************
//*****************************
DWORD ThreadProcRead(LPVOID pParam)
{
CString myStr;
DWORD dwRes;
DWORD dwRead; //實(shí)際讀出的字節(jié)數(shù)
intRead=0;
Rol.hEvent=CreateEvent(NULL, //創(chuàng)建Rol的hEvent成員為無(wú)信號(hào)狀態(tài)
TRUE,
FALSE,
NULL);
if (Rol.hEvent == NULL)
{
AfxMessageBox("hEvent空");
intRead=1;
return -1;
}
if (ReadFile(hCom, //串口句柄
&myRByte, //存放讀取數(shù)據(jù)
dataRLen, //欲讀取的字節(jié)數(shù)
NULL,
&Rol)) //指向創(chuàng)建hCom時(shí)的Rol的指針
{
intRead=2;
//在這里加入處理已讀取數(shù)據(jù)的代碼,已讀取數(shù)據(jù)存放在myRByte數(shù)組中。
}
else
{
dwRes = WaitForSingleObject(Rol.hEvent,
2000); //設(shè)置2秒的超時(shí)
switch(dwRes)
{
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hCom,
&Rol,
&dwRead, //實(shí)際讀出的字節(jié)數(shù)
TRUE)) //TRUE表示直到操作完成該函數(shù)才返回
{
//操作失敗,可以使用GetLastError()函數(shù)獲取錯(cuò)誤信息。
intRead=1;
}
else
{
//操作成功完成,數(shù)據(jù)讀出,并存放到myRByte數(shù)組中。
//在這里加入處理數(shù)據(jù)的代碼
intRead=2;
}
break;
case WAIT_TIMEOUT:
//讀操作失敗,原因是讀超時(shí)。
intRead=1;
break;
default:
//這里可以加入默認(rèn)情況下的處理代碼。
intRead=1;
break;
}
}
CloseHandle(Rol.hEvent);
return 0;
}
//*****************************
//****寫(xiě)線程函數(shù)****************
//*****************************
DWORD ThreadProcWrite(LPVOID pParam)
{
CString myStr;
DWORD dwRes;
DWORD dwWrite;
intWrite=0;
Wol.Internal=0; //設(shè)置OVERLAPPED結(jié)構(gòu)Wol
Wol.InternalHigh=0;
Wol.Offset=0;
Wol.OffsetHigh=0;
Wol.hEvent=CreateEvent(NULL, //創(chuàng)建Wol的hEvent成員為無(wú)信號(hào)狀態(tài)
TRUE,
FALSE,
NULL);
if (Wol.hEvent == NULL)
{
AfxMessageBox("hEvent空");
intWrite=1;
return -1;
}
if (WriteFile(hCom, //串口句柄
&myWByte, //存放待發(fā)送數(shù)據(jù)
dataWLen, //欲發(fā)送的字節(jié)數(shù)
NULL,
&Wol)) //指向創(chuàng)建hCom時(shí)的Wol的指針
{
//在這里加入成功發(fā)送的處理代碼。
intWrite=2;
}
else
{
dwRes = WaitForSingleObject(Wol.hEvent,
500); //設(shè)置寫(xiě)超時(shí)500ms
switch(dwRes)
{
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hCom,
&Wol,
&dwWrite,
TRUE))
{
intWrite=1;
//操作失敗,可以使用GetLastError()函數(shù)獲取錯(cuò)誤信息。
}
else
{
//發(fā)送數(shù)據(jù)成功
intWrite=2;
//在這里加入發(fā)送成功的處理代碼
}
break;
case WAIT_TIMEOUT:
//發(fā)送失敗,原因是發(fā)送超時(shí)
intWrite=1;
break;
default:
//這里可以加入默認(rèn)情況下的處理代碼。
intWrite=1;
break;
}
}
CloseHandle(Wol.hEvent);
return 0;
}
void CPC2PLCView::OnDestroy()
{
CEditView::OnDestroy();
if(!(hCom==NULL))
{
CloseHandle(hCom); //關(guān)閉串口句柄
}
// TODO: Add your message handler code here
}
bool CPC2PLCView::myfuncWrite()
{
//寫(xiě)串口操作
DWORD dwThreadID;//
DWORD dwParam;
int i;
for(i=0;i<3;i++)
{
hThreadWrite=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE )ThreadProcWrite, //寫(xiě)線程函數(shù)名稱(chēng)
&dwParam,
0, //創(chuàng)建線程后,立即執(zhí)行該線程
&dwThreadID);
if (hThreadWrite==NULL)
{
AfxMessageBox("寫(xiě)線程創(chuàng)建失敗!");
intWrite=1;
return false;
}
while (intWrite==0)
{
Sleep(100); //等待寫(xiě)操作結(jié)果
}
if (intWrite==2)
{
return true;
}
}
return false;
}
bool CPC2PLCView::myfuncRead(BYTE ackChar)
{
//讀串口操作代碼
DWORD dwThreadID;
DWORD dwParam;
CString strDis;
int i;
for(i=0;i<3;i++)
{
hThreadRead=CreateThread(NULL,
0,
(LPTHREAD_START_ROUTINE )ThreadProcRead,//讀線程函數(shù)名稱(chēng)
&dwParam,
0, //創(chuàng)建線程后,立即執(zhí)行該線程
&dwThreadID);
if (hThreadRead==NULL)
{
AfxMessageBox("讀線程創(chuàng)建失敗!");
intRead=1;
return false;
}
while (intRead==0)
{
Sleep(100); //等待讀操作結(jié)果
}
if (intRead==2)
{
return true;
}
else
{
myWByte[0]=ackChar; //發(fā)送ENQ或NAK(ascii碼為5或21)
dataWLen=1;
CEdit& myEdit=this->GetEditCtrl( );
myEdit.SetSel(1000000,1000000);
strDis="";
switch(ackChar)
{
case 5:
{
strDis+="<-- [ENQ]\15\12";
break;
}
case 21:
{
strDis+="<-- [NAK]\15\12";
break;
}
}
myEdit.ReplaceSel(strDis);
if(!myfuncWrite())
{
return false;
}
}
}
return false;
}
void CPC2PLCView::OnUpdateReceive(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
//根據(jù)串口是否打開(kāi),決定是否允許“接收”命令
if (blnOpened)
{
pCmdUI->Enable(true);
}
else
{
pCmdUI->Enable(false);
}
}
void CPC2PLCView::OnOpencom()
{
// TODO: Add your command handler code here
CString strDis;
CEdit& myEdit=this->GetEditCtrl( );
hCom=CreateFile(myCom, //創(chuàng)建串口COM2
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL| FILE_FLAG_OVERLAPPED, //使用重疊方式
NULL );
if( hCom !=INVALID_HANDLE_VALUE)
{
SetupComm(hCom,1024,512);
DCB myDCB;
GetCommState( hCom, &myDCB );
myDCB.BaudRate=myBaudRate;
myDCB.fBinary=TRUE;
myDCB.fParity=myfParity;
myDCB.ByteSize=8;
myDCB.Parity=myParity;
myDCB.StopBits=ONESTOPBIT;
SetCommState(hCom,&myDCB);
//顯示串口成功打開(kāi)
myEdit.SetReadOnly (true);
myEdit.SetSel(1000000,1000000);
strDis=myCom;
strDis+="打開(kāi)成功!\15\12\15\12";
myEdit.ReplaceSel(strDis);
blnOpened=true;
}
else
{
//顯示串口打開(kāi)失敗
myEdit.SetReadOnly (true);
myEdit.SetSel(1000000,1000000);
strDis=myCom;
strDis+="打開(kāi)失敗!\15\12\15\12";
myEdit.ReplaceSel(strDis);
blnOpened=false;
}
}
void CPC2PLCView::OnUpdateSetup(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
//根據(jù)串口是否打開(kāi),決定是否允許“設(shè)置”命令
if (!blnOpened)
{
pCmdUI->Enable(true);
}
else
{
pCmdUI->Enable(false);
}
}
void CPC2PLCView::OnUpdateOpencom(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
//根據(jù)串口是否打開(kāi),決定是否允許“打開(kāi)”命令
if (!blnOpened)
{
pCmdUI->Enable(true);
}
else
{
pCmdUI->Enable(false);
}
}
void CPC2PLCView::OnUpdateClosecom(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
//根據(jù)串口是否打開(kāi),決定是否允許“關(guān)閉”命令
if (blnOpened)
{
pCmdUI->Enable(true);
}
else
{
pCmdUI->Enable(false);
}
}
void CPC2PLCView::OnClosecom()
{
// TODO: Add your command handler code here
if ((blnOpened) && (hCom!=NULL))
{
CloseHandle(hCom);
CString strDis; //顯示串口成功關(guān)閉
CEdit& myEdit=this->GetEditCtrl( );
myEdit.SetReadOnly (true);
myEdit.SetSel(1000000,1000000);
strDis=myCom;
strDis+=" 已關(guān)閉!\15\12\15\12";
myEdit.ReplaceSel(strDis);
blnOpened=false;
}
}
void CPC2PLCView::OnUpdateAppExit(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
//根據(jù)串口是否打開(kāi),決定是否允許“退出”命令
if (blnOpened)
{
pCmdUI->Enable(false);
}
else
{
pCmdUI->Enable(true);
}
}
void CPC2PLCView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CEditView::OnChar(nChar, nRepCnt, nFlags);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -