473,386 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trying to make a thread

I'm trying to create a thread to write to the serial port and I keep
getting an odd error

cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned
long (__stdcall *)(void *)'

and it points to the CreateThread call. Here is my class, why is it
doing this and how can I fix it?

<TxdRxd.h>

class TxdRxd
{
CString TempData;
DCB dcb;
DWORD NumBytes, BaudRate, ModemStatus;
BYTE Byte;
HANDLE PortHandle;
int Wait, DataLength;
CString ComPort;

public:

TxdRxd ();
TxdRxd (DWORD NewBaudRate);
~TxdRxd();
bool Transmit (CString PortSpecifier, CString Data);
bool Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate);
int Receive (CString PortSpecifier, CString &Data);
int Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate);
void SetBaud (DWORD NewBaudRate);
void CALLBACK EXPORT TimerProc (HWND hWnd,UINT nMsg,UINT
nIDEvent,DWORD dwTime);
DWORD WINAPI ThreadProc (LPVOID lpParam);

};

<TxdRxd.cpp>

#include "stdafx.h"
#include <process.h>
#include "TxdRxd.h"

TxdRxd::TxdRxd ()
{
BaudRate = CBR_9600;
}

TxdRxd::TxdRxd (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}
}

TxdRxd::~TxdRxd()
{
}

bool TxdRxd::Transmit (CString PortSpecifier, CString Data)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}
bool TxdRxd::Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}

void CALLBACK EXPORT TxdRxd::TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
{
Wait = -1;
}

DWORD WINAPI TxdRxd::ThreadProc (LPVOID lpParameter)
{
PortHandle = CreateFile(ComPort, GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

if (!GetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

SetCommMask (PortHandle, EV_RXCHAR | EV_ERR); //receive
character event
WaitCommEvent (PortHandle, &ModemStatus, 0); //wait for
character

if (ModemStatus & EV_RXCHAR)
{
ReadFile(PortHandle, &Byte, 1, &NumBytes, 0); //read
1
Wait = 1;
}
else if (ModemStatus & EV_ERR)
{
TempData = "COM Error";
return 2;
}

CloseHandle(PortHandle);
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data)
{
int Timer;
DWORD ThreadID;
HANDLE ThreadHandle;
void* lpParameter;

bool Ready = TRUE;
Data.Delete(0,1000);

Wait = 0;
while (Ready)
{
ThreadHandle = CreateThread(NULL, 0, ThreadProc, NULL,
0, &ThreadID);

Timer = SetTimer(NULL, NULL, 3000, NULL);

while (Wait == 0)
{}

if (Wait == 1)
{
if (Byte == 0x03) Ready = FALSE;
else Data = Data + char(Byte);
KillTimer(NULL, Timer);
}
else if (Wait == -1)
{
Ready = FALSE;
MessageBox(NULL, "Timed Out", "ERROR", MB_OK);
}
}
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate)
{
return 1;
}

void TxdRxd::SetBaud (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

}
Jul 22 '05 #1
5 3878
The Beast wrote:
I'm trying to create a thread to write to the serial port and I keep
getting an odd error

cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned
long (__stdcall *)(void *)'

and it points to the CreateThread call. Here is my class, why is it
doing this and how can I fix it?


C++ has no function CreateThread, so you're much better off asking in
a newsgroup where it is on topic (like your platform newsgroup, e.g.).
From the language point of view, it seems that you're trying to use
a non-static member function as a thread procedure. That's not allowed.
Make your member function static and proceed from there.

Victor
Jul 22 '05 #2

"The Beast" <Ob********@columbus.rr.com> wrote in message
news:9s********************************@4ax.com...
I'm trying to create a thread to write to the serial port and I keep
getting an odd error

cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned
long (__stdcall *)(void *)'

and it points to the CreateThread call. Here is my class, why is it
doing this and how can I fix it?
Despite all the nonstandard Microsoft trappings, the problem is
still evident. You're trying to pass the address of a member
function as a parameter which specifies the address of a nonmember
function. This issue is addressed in the C++ FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-33.2

You also might want to check with the folks at newsgruop
'comp.os.ms-windows.programmer.win32' for suggestions about
the best way to approach this in a Windows environment (preferred
idioms, potential problems, etc.).

(You may also need to deal with the '__stdcall' issue, but that's
a Windows issue, not topical here).

-Mike


<TxdRxd.h>

class TxdRxd
{
CString TempData;
DCB dcb;
DWORD NumBytes, BaudRate, ModemStatus;
BYTE Byte;
HANDLE PortHandle;
int Wait, DataLength;
CString ComPort;

public:

TxdRxd ();
TxdRxd (DWORD NewBaudRate);
~TxdRxd();
bool Transmit (CString PortSpecifier, CString Data);
bool Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate);
int Receive (CString PortSpecifier, CString &Data);
int Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate);
void SetBaud (DWORD NewBaudRate);
void CALLBACK EXPORT TimerProc (HWND hWnd,UINT nMsg,UINT
nIDEvent,DWORD dwTime);
DWORD WINAPI ThreadProc (LPVOID lpParam);

};

<TxdRxd.cpp>

#include "stdafx.h"
#include <process.h>
#include "TxdRxd.h"

TxdRxd::TxdRxd ()
{
BaudRate = CBR_9600;
}

TxdRxd::TxdRxd (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}
}

TxdRxd::~TxdRxd()
{
}

bool TxdRxd::Transmit (CString PortSpecifier, CString Data)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}
bool TxdRxd::Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}

void CALLBACK EXPORT TxdRxd::TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
{
Wait = -1;
}

DWORD WINAPI TxdRxd::ThreadProc (LPVOID lpParameter)
{
PortHandle = CreateFile(ComPort, GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

if (!GetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

SetCommMask (PortHandle, EV_RXCHAR | EV_ERR); //receive
character event
WaitCommEvent (PortHandle, &ModemStatus, 0); //wait for
character

if (ModemStatus & EV_RXCHAR)
{
ReadFile(PortHandle, &Byte, 1, &NumBytes, 0); //read
1
Wait = 1;
}
else if (ModemStatus & EV_ERR)
{
TempData = "COM Error";
return 2;
}

CloseHandle(PortHandle);
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data)
{
int Timer;
DWORD ThreadID;
HANDLE ThreadHandle;
void* lpParameter;

bool Ready = TRUE;
Data.Delete(0,1000);

Wait = 0;
while (Ready)
{
ThreadHandle = CreateThread(NULL, 0, ThreadProc, NULL,
0, &ThreadID);

Timer = SetTimer(NULL, NULL, 3000, NULL);

while (Wait == 0)
{}

if (Wait == 1)
{
if (Byte == 0x03) Ready = FALSE;
else Data = Data + char(Byte);
KillTimer(NULL, Timer);
}
else if (Wait == -1)
{
Ready = FALSE;
MessageBox(NULL, "Timed Out", "ERROR", MB_OK);
}
}
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate)
{
return 1;
}

void TxdRxd::SetBaud (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

}

Jul 22 '05 #3
Thank you! I tried making it static and it then denied me access to
the member variables. I'll try a C++ in windows forum, but thank you
anyway!

The Beast

On Fri, 10 Sep 2004 11:56:02 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
The Beast wrote:
I'm trying to create a thread to write to the serial port and I keep
getting an odd error

cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned
long (__stdcall *)(void *)'

and it points to the CreateThread call. Here is my class, why is it
doing this and how can I fix it?


C++ has no function CreateThread, so you're much better off asking in
a newsgroup where it is on topic (like your platform newsgroup, e.g.).
From the language point of view, it seems that you're trying to use
a non-static member function as a thread procedure. That's not allowed.
Make your member function static and proceed from there.

Victor


Jul 22 '05 #4
Thank you for your help! The forum you suggested does indeed look
helpful!

The Beast

On Fri, 10 Sep 2004 15:59:54 GMT, "Mike Wahler"
<mk******@mkwahler.net> wrote:

"The Beast" <Ob********@columbus.rr.com> wrote in message
news:9s********************************@4ax.com.. .
I'm trying to create a thread to write to the serial port and I keep
getting an odd error

cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned
long (__stdcall *)(void *)'

and it points to the CreateThread call. Here is my class, why is it
doing this and how can I fix it?


Despite all the nonstandard Microsoft trappings, the problem is
still evident. You're trying to pass the address of a member
function as a parameter which specifies the address of a nonmember
function. This issue is addressed in the C++ FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-33.2

You also might want to check with the folks at newsgruop
'comp.os.ms-windows.programmer.win32' for suggestions about
the best way to approach this in a Windows environment (preferred
idioms, potential problems, etc.).

(You may also need to deal with the '__stdcall' issue, but that's
a Windows issue, not topical here).

-Mike


<TxdRxd.h>

class TxdRxd
{
CString TempData;
DCB dcb;
DWORD NumBytes, BaudRate, ModemStatus;
BYTE Byte;
HANDLE PortHandle;
int Wait, DataLength;
CString ComPort;

public:

TxdRxd ();
TxdRxd (DWORD NewBaudRate);
~TxdRxd();
bool Transmit (CString PortSpecifier, CString Data);
bool Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate);
int Receive (CString PortSpecifier, CString &Data);
int Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate);
void SetBaud (DWORD NewBaudRate);
void CALLBACK EXPORT TimerProc (HWND hWnd,UINT nMsg,UINT
nIDEvent,DWORD dwTime);
DWORD WINAPI ThreadProc (LPVOID lpParam);

};

<TxdRxd.cpp>

#include "stdafx.h"
#include <process.h>
#include "TxdRxd.h"

TxdRxd::TxdRxd ()
{
BaudRate = CBR_9600;
}

TxdRxd::TxdRxd (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}
}

TxdRxd::~TxdRxd()
{
}

bool TxdRxd::Transmit (CString PortSpecifier, CString Data)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}
bool TxdRxd::Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}

void CALLBACK EXPORT TxdRxd::TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
{
Wait = -1;
}

DWORD WINAPI TxdRxd::ThreadProc (LPVOID lpParameter)
{
PortHandle = CreateFile(ComPort, GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

if (!GetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

SetCommMask (PortHandle, EV_RXCHAR | EV_ERR); //receive
character event
WaitCommEvent (PortHandle, &ModemStatus, 0); //wait for
character

if (ModemStatus & EV_RXCHAR)
{
ReadFile(PortHandle, &Byte, 1, &NumBytes, 0); //read
1
Wait = 1;
}
else if (ModemStatus & EV_ERR)
{
TempData = "COM Error";
return 2;
}

CloseHandle(PortHandle);
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data)
{
int Timer;
DWORD ThreadID;
HANDLE ThreadHandle;
void* lpParameter;

bool Ready = TRUE;
Data.Delete(0,1000);

Wait = 0;
while (Ready)
{
ThreadHandle = CreateThread(NULL, 0, ThreadProc, NULL,
0, &ThreadID);

Timer = SetTimer(NULL, NULL, 3000, NULL);

while (Wait == 0)
{}

if (Wait == 1)
{
if (Byte == 0x03) Ready = FALSE;
else Data = Data + char(Byte);
KillTimer(NULL, Timer);
}
else if (Wait == -1)
{
Ready = FALSE;
MessageBox(NULL, "Timed Out", "ERROR", MB_OK);
}
}
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate)
{
return 1;
}

void TxdRxd::SetBaud (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

}


Jul 22 '05 #5
Just wanted to add that the void* parameter to ThreadProc() is your true
friend;-)

Make two ThreadProc() methods:
static DWORD WINAPI ThreadProc (LPVOID lpParam);
DWORD ThreadProc ();

When calling CreateThread(), pass the this pointer as user data
(lpParameter). In the static ThreadProc(), cast lpParam back to TxdRxd* and
call the non-static ThreadProc() through that pointer. Solves the problem of
member variable access.
"The Beast" <Ob********@columbus.rr.com> skrev i meddelandet
news:9s********************************@4ax.com...
I'm trying to create a thread to write to the serial port and I keep
getting an odd error

cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned
long (__stdcall *)(void *)'

and it points to the CreateThread call. Here is my class, why is it
doing this and how can I fix it?

<TxdRxd.h>

class TxdRxd
{
CString TempData;
DCB dcb;
DWORD NumBytes, BaudRate, ModemStatus;
BYTE Byte;
HANDLE PortHandle;
int Wait, DataLength;
CString ComPort;

public:

TxdRxd ();
TxdRxd (DWORD NewBaudRate);
~TxdRxd();
bool Transmit (CString PortSpecifier, CString Data);
bool Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate);
int Receive (CString PortSpecifier, CString &Data);
int Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate);
void SetBaud (DWORD NewBaudRate);
void CALLBACK EXPORT TimerProc (HWND hWnd,UINT nMsg,UINT
nIDEvent,DWORD dwTime);
DWORD WINAPI ThreadProc (LPVOID lpParam);

};

<TxdRxd.cpp>

#include "stdafx.h"
#include <process.h>
#include "TxdRxd.h"

TxdRxd::TxdRxd ()
{
BaudRate = CBR_9600;
}

TxdRxd::TxdRxd (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}
}

TxdRxd::~TxdRxd()
{
}

bool TxdRxd::Transmit (CString PortSpecifier, CString Data)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}
bool TxdRxd::Transmit (CString PortSpecifier, CString Data, DWORD
NewBaudRate)
{
Data = Data + char(0x03);
PortHandle = CreateFile(PortSpecifier, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
DataLength = strlen(Data);

switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

if (!GetCommState(PortHandle,&dcb))
{
return false;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb)) return false;

bool retVal =
WriteFile(PortHandle,Data,DataLength,&NumBytes,NUL L);

CloseHandle(PortHandle); //close the handle

return retVal;
}

void CALLBACK EXPORT TxdRxd::TimerProc(
HWND hWnd, // handle of CWnd that called SetTimer
UINT nMsg, // WM_TIMER
UINT nIDEvent, // timer identification
DWORD dwTime // system time
)
{
Wait = -1;
}

DWORD WINAPI TxdRxd::ThreadProc (LPVOID lpParameter)
{
PortHandle = CreateFile(ComPort, GENERIC_READ, 0, NULL,
OPEN_EXISTING, 0, NULL);

if (!GetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

dcb.BaudRate = BaudRate; //Set Baud Rate
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop

if (!SetCommState(PortHandle,&dcb))
{
TempData = "Port Could Not Be Opened";
return 1;
}

SetCommMask (PortHandle, EV_RXCHAR | EV_ERR); //receive
character event
WaitCommEvent (PortHandle, &ModemStatus, 0); //wait for
character

if (ModemStatus & EV_RXCHAR)
{
ReadFile(PortHandle, &Byte, 1, &NumBytes, 0); //read
1
Wait = 1;
}
else if (ModemStatus & EV_ERR)
{
TempData = "COM Error";
return 2;
}

CloseHandle(PortHandle);
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data)
{
int Timer;
DWORD ThreadID;
HANDLE ThreadHandle;
void* lpParameter;

bool Ready = TRUE;
Data.Delete(0,1000);

Wait = 0;
while (Ready)
{
ThreadHandle = CreateThread(NULL, 0, ThreadProc, NULL,
0, &ThreadID);

Timer = SetTimer(NULL, NULL, 3000, NULL);

while (Wait == 0)
{}

if (Wait == 1)
{
if (Byte == 0x03) Ready = FALSE;
else Data = Data + char(Byte);
KillTimer(NULL, Timer);
}
else if (Wait == -1)
{
Ready = FALSE;
MessageBox(NULL, "Timed Out", "ERROR", MB_OK);
}
}
return 0;
}

int TxdRxd::Receive (CString PortSpecifier, CString &Data, DWORD
NewBaudRate)
{
return 1;
}

void TxdRxd::SetBaud (DWORD NewBaudRate)
{
switch (NewBaudRate)
{
case 110:
BaudRate = CBR_110;
break;
case 300:
BaudRate = CBR_300;
break;
case 600:
BaudRate = CBR_600;
break;
case 1200:
BaudRate = CBR_1200;
break;
case 2400:
BaudRate = CBR_2400;
break;
case 4800:
BaudRate = CBR_4800;
break;
case 9600:
BaudRate = CBR_9600;
break;
case 14400:
BaudRate = CBR_14400;
break;
case 19200:
BaudRate = CBR_19200;
break;
case 38400:
BaudRate = CBR_38400;
break;
case 56000:
BaudRate = CBR_56000;
break;
case 57600:
BaudRate = CBR_57600;
break;
case 115200:
BaudRate = CBR_115200;
break;
case 128000:
BaudRate = CBR_128000;
break;
case 256000:
BaudRate = CBR_256000;
break;
}

}

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Eric Marvets | last post by:
I want to make the main thread in my app sleep while I have a worker do some arbitrary task. The problem is waking the main thread back up. Can anyone make this code sample work? private void...
3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
3
by: David Thielen | last post by:
Hi; I created a virtual directory in IIS 6.0 and my asp.net app runs fine. But when it tries to write a file I get: Access to the path is denied. - C:\Inetpub\wwwroot\RunReportASP\images ...
4
by: WinDev | last post by:
What does this exception message mean? Type System.DelegateSerializationHolder and the types derived from it (such as System.DelegateSerializationHolder) are not permitted to be deserialized at...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
13
by: cj | last post by:
Stephany Young provided me with the following code to count threads created by my program. Public Class MyThreadCount Private Shared m_lock As New Object Private Shared m_threadcount As Int32...
7
by: UJ | last post by:
Has anybody seen an 'Access denied' error when trying to create a mutex? If so - what does that mean? I though Mutex's were not supposed to error out. TIA - Jeff.
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.