thread related issue

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



hi all,
i am having one problem with threading

i created one dialog based application whose work is to read the data
from serial port and display it.so i created one thred for this work.

means now i got two threads one is application thread and other is
serial communication thread which is created in OnInitDialog.

now everything is working i mean the thread got created it read some
bytes
from port and now it wants to show the data so need to send this data
to main
thread. so i am doing this with ::SendMessage() but it is giving access
violation. i don't know why this is happening and currently i am not
sending the data also i.e. buffer so please help me
below is code (stepwise)

1)In Oninitdialog
THREADPARAM *ptp = new THREADPARAM;
if(ptp)
{
memset(ptp->array,0,30);
strcpy(ptp->array,"Harshal");
ptp->appHandle = this->m_hWnd;
hSerialThread = CreateThread( NULL,
0,
(LPTHREAD_START_ROUTINE)
SerialCommunication,
(LPVOID) NULL,
0,
&dwSerialThreadID);
}



2)Thread function
UINT SerialCommunication(LPVOID pParam)
{
int write_req=0;
THREADPARAM *ptp = (THREADPARAM *)pParam;
while(1)
{
if(write_req == 2)// send write request and read it
{
DWORD length=0;
DWORD dwRead=0;
if (WriteFile(hCom,
SEND_CMD, // pointer to data to write to
file
NOOFBYTE, // number of bytes to write
&length, // pointer to number of bytes
written
NULL) == 0)
{// write failed
write_req = 0;
continue;
}
// writefile succeeded issue readfile
char buffer[MAX_MESSAGE];
memset(buffer,0,MAX_MESSAGE);
if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead,
NULL))
{
write_req = 0;
continue;
}
if(dwRead > 0)
{
//MessageBox(NULL,buffer,"Data with packet",MB_OK);

::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0);//
error is here
}
write_req = 0;
}
else // only read from port and increment counter
{
DWORD dwRead=0;
char buffer[MAX_MESSAGE];
memset(buffer,0,MAX_MESSAGE);
if (!ReadFile(hCom, buffer, MAX_MESSAGE, &dwRead,
NULL))
{
write_req++;
continue;
}
if(dwRead > 0)
{// you are in thread you need to send message to
// main application
//char a;
HWND hwnd = (HWND)AfxGetApp()->m_pMainWindow;
::SendMessage(ptp->appHandle,MY_SHOW_DATA,0,0);
//MessageBox(NULL,buffer,"Data without
packet",MB_OK);
}
write_req++;

}
}


return 0;
}


3) Sent message function

LRESULT CReadDataDlg::OnShowDataMessage(UINT wParam, LONG lParam)
{
return 0;
}

Thanks and regards
Harshal

.



Relevant Pages

  • Re: interthread communication
    ... Since this is a writer thread, typically you will PostThreadMessage to it for each packet ... Note that if you have a serial port you must open it for asynchronous I/O. ... of the buffer in via a PostThreadMessage, so using a single variable cannot make sense. ...
    (microsoft.public.vc.mfc)
  • Re: Serial Communications - Lost Event
    ... "Completion of write" is a moment when all data from the app buffer is ... The driver can keep the data in its internal buffer ... > serial port monitoring and test applications. ... > of a serial port means when all of the data has been transfered. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: thread related issue
    ... from serial port and display it.so i created one thred for this work. ... to properly terminate the buffer. ... The proper types are ... Take a look at my essay on serial port I/O on my MVP Tips site. ...
    (microsoft.public.vc.mfc)
  • Serial Loopback Code
    ... not open the port as it was already opened. ... This function loops continously simple to demonstrate serial reading ... in a specific apllication of reading from the serial port a quit ... DWORD PortReadThread ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Problem synschronizing serialport.read()
    ... interface-matching set of serial port classes and see what they're doing. ... multiple of 273 new bytes have been added to the receive buffer. ... BytesToRead are first stored when BytesToRead equals 273. ...
    (microsoft.public.dotnet.framework.compactframework)