Click here to Skip to main content
15,905,414 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Toolbar Pin
«_Superman_»5-Jul-05 1:57
professional«_Superman_»5-Jul-05 1:57 
GeneralRe: Toolbar Pin
Anonymous5-Jul-05 17:50
Anonymous5-Jul-05 17:50 
GeneralHelp ! Reading a file from the web Pin
nirishere5-Jul-05 0:03
nirishere5-Jul-05 0:03 
GeneralRe: Help ! Reading a file from the web Pin
2249175-Jul-05 0:20
2249175-Jul-05 0:20 
GeneralAbout OMA Device Management client Pin
Maer7274-Jul-05 23:44
Maer7274-Jul-05 23:44 
GeneralAccessing resources in a Thread Pin
Juergen Kordes4-Jul-05 23:22
Juergen Kordes4-Jul-05 23:22 
GeneralRe: Accessing resources in a Thread Pin
Cedric Moonen4-Jul-05 23:54
Cedric Moonen4-Jul-05 23:54 
GeneralRe: Accessing resources in a Thread Pin
Juergen Kordes5-Jul-05 0:48
Juergen Kordes5-Jul-05 0:48 
Sorry that I didn't express myself exactly. Let me briefly explain the class I used:

class SERIAL<br />
{<br />
  ...<br />
protected:<br />
  // Declaration of the two Threadfunctions<br />
  static UINT SendQueueThread(LPVOID x);      // Send Thread<br />
  static UINT PortReadThread(LPVOID x);       // Receive Thread<br />
<br />
  // <br />
  CWinThread*	m_pPortReadThread;<br />
  CWinThread*	m_pSendQueueThread;<br />
<br />
public:<br />
  bool PortInitialize(...);  // Initialization function<br />
  HANDLE hComPort;   // Handle to the serial interface<br />
  int send(BYTE IDByte,unsigned char* pFirstByte,int len);<br />
<br />
  ...<br />
};


Within the SERIAL :: PortInitialize(...) I do the following:

bool SERIAL::PortInitialize(...)<br />
{<br />
  ...<br />
  hComPort=CreateFile ( portName,        // e.g. "COM1:"<br />
                        GENERIC_READ | GENERIC_WRITE,	// read/write mode<br />
			0,				// share mode<br />
			NULL,	                	// security attribute<br />
			OPEN_EXISTING,			// How to open serial port<br />
		        0,				// Port attributes<br />
			NULL );				// to copy<br />
	<br />
<br />
  if(hComPort==INVALID_HANDLE_VALUE)<br />
  {<br />
     ...<br />
     return false;<br />
  }	<br />
<br />
  // now doing all the stuff to initialize the interface<br />
<br />
  GetCommState(hComPort,&PortDCB);<br />
  ...<br />
  SetCommState(hComPort,&PortDCB);<br />
<br />
  GetCommTimeouts(hComPort,&CommTimeouts);<br />
  ...<br />
  SetCommTimeouts(hComPort,&CommTimeouts);<br />
<br />
  EscapeCommFunction(hComPort,SETDTR); <br />
  EscapeCommFunction(hComPort,SETRTS); <br />
<br />
  // Createing the sendthread<br />
  m_pSendQueueThread = AfxBeginThread( SendQueueThread, this );<br />
  if( m_pSendQueueThread == (CWinThread*)0 )<br />
  {<br />
     displayWindowsError( "SERIAL::PortInitialize(...) Error starting SendQueueThread." );<br />
     return( false );<br />
  }<br />
  else<br />
  {	<br />
     m_pSendQueueThread->SetThreadPriority(THREAD_PRIORITY_ABOVE_NORMAL );<br />
  }<br />
<br />
  // the PortReadThread is started in the same way<br />
  ...<br />
}  // end bool SERIAL :: PortInitialize(...)


The sendthread contains the following code fragments:

UINT SERIAL :: SendQueueThread(LPVOID lpvoid)<br />
{<br />
  ...<br />
  SERIAL *pSerial=(SERIAL*) lpvoid;<br />
  BYTE ClassID,CommandID;<br />
  int len;<br />
  unsigned char *data;<br />
  ...<br />
  // pSerial->m_pMessage contains the data to send<br />
  ClassID=pSerial->m_pMessage->GetClassID();<br />
  CommandID=pSerial->m_pMessage->GetCommandID();<br />
  len=pSerial->m_pMessage->GetMessageLen();<br />
  data=pSerial->m_pMessage->GetMessageString();<br />
  test=pSerial->send(CommandID,data,len);<br />
  <br />
  ...<br />
}


At least, the function SERIAL::send(...) contains the following code:

int SERIAL::send(BYTE IDByte,unsigned char*  pFirstByte, int len)<br />
{<br />
  ...<br />
<br />
  // only test code<br />
  DWORD dwNumBytesWritten;<br />
  char* buffer;<br />
  buffer = new char[100];<br />
  for( int i = 0; i < 99; i++ )<br />
	buffer[i] = i + 48;<br />
  buffer[99] = 0;<br />
<br />
  try<br />
  {<br />
     DWORD strLen = strlen(buffer);<br />
     WriteFile(hComPort,	//port handle<br />
		(void*)buffer,  //Pointer to the date to write<br />
		(DWORD)strlen( buffer ),	//Number of bytes to write<br />
		&dwNumBytesWritten,		//Number of written bytes<br />
		NULL);				//		<br />
  }<br />
  catch ( CException *e )<br />
  {<br />
     ...<br />
  }<br />
<br />
  delete [] buffer;<br />
<br />
  ...<br />
  }  // end int SERIAL::send(BYTE IDByte,unsigned char*  pFirstByte, int len)


That's all! When calling WriteFile(...) in SERIAL::send(...) the thread crashes, as mentioned before.

Thanks for your help!

Juergen Kordes
GeneralRe: Accessing resources in a Thread Pin
Cedric Moonen5-Jul-05 1:03
Cedric Moonen5-Jul-05 1:03 
GeneralRe: Accessing resources in a Thread Pin
Juergen Kordes5-Jul-05 3:58
Juergen Kordes5-Jul-05 3:58 
Generalwe can't use some API functions Pin
mohsen nowruzi4-Jul-05 23:12
mohsen nowruzi4-Jul-05 23:12 
GeneralRe: we can't use some API functions Pin
Christian Graus4-Jul-05 23:48
protectorChristian Graus4-Jul-05 23:48 
GeneralRe: we can't use some API functions Pin
BlackDice5-Jul-05 2:49
BlackDice5-Jul-05 2:49 
GeneralRe: we can't use some API functions Pin
Blake Miller5-Jul-05 4:49
Blake Miller5-Jul-05 4:49 
GeneralRe: we can't use some API functions Pin
BlackDice5-Jul-05 5:01
BlackDice5-Jul-05 5:01 
GeneralRe: we can't use some API functions Pin
Blake Miller5-Jul-05 5:17
Blake Miller5-Jul-05 5:17 
General'^' operator Pin
LiYS4-Jul-05 22:52
LiYS4-Jul-05 22:52 
GeneralRe: '^' operator Pin
PravinSingh4-Jul-05 23:33
PravinSingh4-Jul-05 23:33 
GeneralRe: '^' operator Pin
LiYS4-Jul-05 23:48
LiYS4-Jul-05 23:48 
GeneralRe: '^' operator Pin
PravinSingh5-Jul-05 0:04
PravinSingh5-Jul-05 0:04 
GeneralRe: '^' operator Pin
Bob Stanneveld5-Jul-05 0:25
Bob Stanneveld5-Jul-05 0:25 
GeneralRe: '^' operator Pin
LiYS5-Jul-05 0:04
LiYS5-Jul-05 0:04 
GeneralRe: '^' operator Pin
Bob Stanneveld5-Jul-05 0:31
Bob Stanneveld5-Jul-05 0:31 
GeneralRe: '^' operator Pin
Bob Stanneveld5-Jul-05 0:36
Bob Stanneveld5-Jul-05 0:36 
GeneralRe: '^' operator Pin
toxcct5-Jul-05 1:02
toxcct5-Jul-05 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.