I want to setup my MFC-based dialogue "IDD_LOADCELL" with serial communication so that when that dialogue is pop up, the data from PIC (external device) which is in hex file format is display on the edit control box on the dialogue. The variable name of the edit control box is "m_loadP "and "m_loadT1". type of variable for both are string. I want to get the code for the COMPort setting and reading the data from pic and finally display it on the edit control box created on the dialogue. Below if my code for ComPort setting which I dont know whether correct or not. I'm still dont know how to combine read data funcion o this bunch of code. please help me...
// Loadcell.cpp : implementation file
//
#include "stdafx.h"
#include "3Dbalance.h"
#include "Loadcell.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CLoadcell
CLoadcell::CLoadcell()
{
}
CLoadcell::~CLoadcell()
{
}
BEGIN_MESSAGE_MAP(CLoadcell, CWnd)
//{{AFX_MSG_MAP(CSerialCom)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLoadCell message handlers
BOOL CLoadcell::OpenPort(CString portname)
{
portname= "//./" +portname;
hComm = CreateFile(portname,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
if(hComm==INVALID_HANDLE_VALUE){
//MessageBox("Cannot open Communication Port.Please\nQuit the program and Re-start your PC.","Com Port Error",MB_OK+MB_ICONERROR);
return false;}
else
return true;
}
BOOL CLoadcell::ConfigurePort(DWORD BaudRate, BYTE ByteSize, DWORD fParity, BYTE Parity, BYTE StopBits)
{
if((m_bPortReady = GetCommState(hComm, &m_dcb))==0){
MessageBox(_T("GetCommState Error","Error",MB_OK+MB_ICONERROR));
CloseHandle(hComm);
return false;}
m_dcb.BaudRate =BaudRate;
m_dcb.ByteSize = ByteSize;
m_dcb.Parity =Parity ;
m_dcb.StopBits =StopBits;
m_dcb.fBinary=TRUE;
m_dcb.fDsrSensitivity=false;
m_dcb.fParity=fParity;
m_dcb.fOutX=false;
m_dcb.fInX=false;
m_dcb.fNull=false;
m_dcb.fAbortOnError=TRUE;
m_dcb.fOutxCtsFlow=FALSE;
m_dcb.fOutxDsrFlow=false;
m_dcb.fDtrControl=DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity=false;
m_dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow=false;
m_dcb.fOutxCtsFlow=false;
m_bPortReady = SetCommState(hComm, &m_dcb);
if(m_bPortReady ==0){
MessageBox(_T("SetCommState Error","Error",MB_OK+MB_ICONERROR));
CloseHandle(hComm);
return false;}
return true;
}
BOOL CLoadcell::SetCommunicationTimeouts(DWORD ReadIntervalTimeout, DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant, DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant)
{
if((m_bPortReady = GetCommTimeouts (hComm, &m_CommTimeouts))==0)
return false;
m_CommTimeouts.ReadIntervalTimeout =ReadIntervalTimeout;
m_CommTimeouts.ReadTotalTimeoutConstant =ReadTotalTimeoutConstant;
m_CommTimeouts.ReadTotalTimeoutMultiplier =ReadTotalTimeoutMultiplier;
m_CommTimeouts.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
m_CommTimeouts.WriteTotalTimeoutMultiplier =WriteTotalTimeoutMultiplier;
m_bPortReady = SetCommTimeouts (hComm, &m_CommTimeouts);
if(m_bPortReady ==0){
MessageBox(_T("StCommTimeouts function failed","Com Port Error",MB_OK+MB_ICONERROR));
CloseHandle(hComm);
return false;}
return true;
}
BOOL CLoadcell::WriteByte(BYTE bybyte)
{
iBytesWritten=0;
if(WriteFile(hComm,&bybyte,1,&iBytesWritten,NULL)==0)
return false;
else return true;
}
BOOL CLoadcell::ReadByte(BYTE &resp)
{
BYTE rx;
resp=0;
DWORD dwBytesTransferred=0;
if (ReadFile (hComm, &rx, 1, &dwBytesTransferred, 0)){
if (dwBytesTransferred == 1){
resp=rx;
return true;}}
return false;
}
void CLoadcell::ClosePort()
{
CloseHandle(hComm);
return;
}