I am tacking this onto my previous thread as it relates to the same problem and code, but the subject is slightly different....
In using MFC's AfxBeginThread, the call to the threads are working, I put message box's inside the functions', the pointer to the threads call and got positive results so I know the thread is working, but now I am trying to figure out how to return the result of the polling thread to my Dialog Window. Not sure if this is something I need to define inside the definition of the function I call with the thread, or if there is some way to pipe the repsonse of the thread back to Dialog and an assigned variable.
Class Declaration and the functions I call in the thread (ConfigurePort and ReadString):
Code:
class VIEWER : public CDialog
{
// Construction
public:
void ControlCap();
VIEWER(CWnd* pParent = NULL); // standard constructor
HANDLE hPort;
void setOwner(VIEWER* ptrDialog)
{
ptrDlg = ptrDialog;
}
static UINT StartThread (LPVOID param);
typedef struct THREADSTRUCT //structure for passing to the controlling function
{
VIEWER* _this;
} THREADSTRUCT;//controlling function header
//define functions for port settings and reading port
//the read byte function
BOOL ReadByte(BYTE &resp)
{
BOOL bReturn = TRUE;
BYTE rx;
DWORD dwBytesTransferred=0;
if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0)
{
if (dwBytesTransferred == 1)
{
resp=rx;
bReturn = TRUE;
}
else bReturn = FALSE;
}
else bReturn = FALSE;
return bReturn;
}
//the read string function
BOOL ReadString(void *outstring, int *length)
{
// MessageBox("In ReadString");
BYTE data;
BYTE dataout[4096]={0};
int index = 0;
while(ReadByte(data)== TRUE)
{
dataout[index++] = data;
}
memcpy(outstring, dataout, index);
*length = index;
return TRUE;
}
//the serial port configuration function
HANDLE ConfigureSerialPort(LPCSTR lpszPortName)
{
// MessageBox("In ConfigPort");
HANDLE hComm = NULL;
DWORD dwError;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
// Open the serial port.
hComm = CreateFile (lpszPortName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute to copy
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hComm, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = 9600; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control
PortDCB.fAbortOnError = TRUE; // Do not abort reads/writes on error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB structure.
if (!SetCommState (hComm, &PortDCB))
{
AfxMessageBox("Could not configure serial port\n");
return NULL;
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (hComm, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts (hComm, &CommTimeouts))
{
AfxMessageBox("Could not set timeouts\n");
return NULL;
}
return hComm;
}
//the close port function
void ClosePort()
{
CloseHandle(hPort);
return;
}
// Dialog Data
//{{AFX_DATA(VIEWER)
enum { IDD = IDD_VIEWER };
CVideoCap m_VideoCap1;
CString m_City;
CString m_State;
CString m_Street;
CString m_Speed;
CString m_Radar;
//}}AFX_DATA
private:
void UpdateConfig();
VIEWER* ptrDlg;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(VIEWER)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
// Generated message map functions
//{{AFX_MSG(VIEWER)
virtual BOOL OnInitDialog();
afx_msg void OnChangeStreet();
afx_msg void OnChangeCity();
afx_msg void OnChangeSpeed();
afx_msg void OnSelchangeState();
virtual void OnOK();
afx_msg void OnCapture();
afx_msg void OnStop();
afx_msg void OnPause();
afx_msg void OnResume();
afx_msg void OnSnapshot();
afx_msg void OnChangeRadar();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_VIEWER_H__4FE30814_FC1C_42DD_B76F_F060E2892084__INCLUDED_)
and now the actual thread execution:
Code:
UINT VIEWER::StartThread (LPVOID param)
{
ofstream fout;
fout.open("C:\\threadtest.txt", ios::app);
char data [10];
char clear [2] = "";
char message[8];
int sizeBuffer =8;
int count = 0;
THREADSTRUCT* ts = (THREADSTRUCT*)param;
//open port and configure it
//here is the time-consuming process which interacts with your dialog
// AfxMessageBox ("Thread is started!");
/* ts->_this->m_ctrl_progress.SetRange (0, 1000);
while (ts->_this->m_ctrl_progress.GetPos () < 1000)
{
Sleep(500);
ts->_this->m_ctrl_progress.StepIt ();
} */
ts->_this->ConfigureSerialPort("COM1");
Sleep(500);
do
{
ts->_this->ReadString(message, &sizeBuffer);
count++;
Sleep(50);
if (count ==100)
{
ts->_this->ClosePort();
fout<<"eo data loop"<<endl;
fout.close();
break;
}
}while (count <=100);
//testing only
AfxMessageBox("Loop Completed");
// ts->_this->ClosePort();
//you can also call AfxEndThread() here
AfxEndThread(0,NULL);
return 1;
}
I have tried creating some variables and setting them equal to the thread call, and this did not work:
Code:
static CString output;
outp+=ts->_this->ReadString(message, &sizeBuffer);
I get the message using the same code in a Win32 Console app (<- just for you Wraithan :-)) and I can "cout" the message string fine, so I know the exact string I should be seeing, but when trying to return in from a thread vs from the call to the function itself, nada.
I will keep cracking at it, but any help would be appreciated -