Click here to Skip to main content
15,900,510 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Modeless Property Sheet Pin
Jay Hova18-Jun-03 8:16
Jay Hova18-Jun-03 8:16 
GeneralRe: Modeless Property Sheet Pin
Ryan Binns18-Jun-03 15:26
Ryan Binns18-Jun-03 15:26 
GeneralRe: Modeless Property Sheet Pin
Jay Hova19-Jun-03 1:39
Jay Hova19-Jun-03 1:39 
GeneralRe: Modeless Property Sheet Pin
Ryan Binns19-Jun-03 1:51
Ryan Binns19-Jun-03 1:51 
Generaldesign question Pin
monrobot1318-Jun-03 3:36
monrobot1318-Jun-03 3:36 
GeneralRe: design question Pin
valikac18-Jun-03 6:54
valikac18-Jun-03 6:54 
GeneralSerial communication problem Pin
justin22318-Jun-03 2:31
justin22318-Jun-03 2:31 
GeneralRe: Serial communication problem Pin
justin22318-Jun-03 3:17
justin22318-Jun-03 3:17 
This code is run on both computer. The only diffrent setting is the comport.
If I connect to the port with hyperterminal I get OK.

DWORD Datatal::CSerialSocket::Connect()
{
//Disconnect if we are connected.
if (m_hComm)
{
CloseHandle(m_hComm);
}


//Lets connect to the port
char szPortName[10];
sprintf(szPortName, "\\\\.\\COM%d", m_dwComPort);
m_hComm = CreateFile(
szPortName, // Name
of port
GENERIC_READ|GENERIC_WRITE, // access ( read and write)
0, // (share) 0: cannot share
the COM port
0, // security (None)
OPEN_EXISTING, // creation : open_existing
FILE_FLAG_OVERLAPPED, // we want overlapped
operation FILE_FLAG_OVERLAPPED
0 // no templates file for
COM port...
);


// Check if we could open the port
if (m_hComm == INVALID_HANDLE_VALUE)
{
DWORD dwRes = GetLastError();

if (ERROR_ACCESS_DENIED == dwRes) WriteLog(1, "Connect
failed, Port is already open");
if (ERROR_FILE_NOT_FOUND == dwRes) WriteLog(1, "Connect
failed, The specified port do not exist");
WriteLog(1, "Connect failed, CreateFile, unspecified
error: %d", dwRes);
return dwRes;
}

//Com settings structure
DCB dcb = {0};
dcb.DCBlength = sizeof(DCB);

if (!GetCommState (m_hComm, &dcb))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, GetCommState, unspecified
error: %d", dwRes);
return dwRes;
}

dcb.fBinary=TRUE;
dcb.fDsrSensitivity=false;
dcb.fOutX=false;
dcb.fInX=false;
dcb.fNull=false;
dcb.fAbortOnError=TRUE;
dcb.fOutxCtsFlow=FALSE;
dcb.fOutxDsrFlow=false;
dcb.fDtrControl=DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity=false;
dcb.fRtsControl=RTS_CONTROL_DISABLE;
dcb.fOutxCtsFlow=false;
dcb.fOutxCtsFlow=false;

// custom
dcb.BaudRate = m_dwBaudRate;
dcb.ByteSize = m_bByteSize;
dcb.Parity = m_bParity;
if ( m_dwStopBits == 1 )
dcb.StopBits = ONESTOPBIT;
else if (m_dwStopBits == 2 )
dcb.StopBits = TWOSTOPBITS;
else
dcb.StopBits = ONE5STOPBITS;


//Tell the comport how it should work.
if (!SetCommState (m_hComm,&dcb))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, SetCommState, unspecified
error: %d", dwRes);
return dwRes;
}

// We want to Receive events.
//SetCommMask( m_hComm, EV_RXCHAR|EV_TXEMPTY);


// Set some timeouts.
COMMTIMEOUTS timeouts;
if(!GetCommTimeouts (m_hComm, &timeouts))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, GetCommTimeouts,
unspecified error: %d", dwRes);
return dwRes;
}


WriteLog(2, "timeouts: ReadInterval: %d, ReadTotalConstant: %d,
ReadTotalMultiplier: %d, WriteTotalConstant: %d, WriteTotalMultiplier:
%d",
timeouts.ReadIntervalTimeout,
timeouts.ReadTotalTimeoutConstant,
timeouts.ReadTotalTimeoutMultiplier,
timeouts.WriteTotalTimeoutConstant,
timeouts.WriteTotalTimeoutMultiplier
);

//defaults, I think...
//ReadInterval: 10, ReadTotalConstant: 0, ReadTotalMultiplier:
0, WriteTotalConstant: 5000, WriteTotalMultiplier: 0

timeouts.ReadIntervalTimeout = 10; // time
between chars
timeouts.ReadTotalTimeoutConstant = 0; //
timeouts.ReadTotalTimeoutMultiplier = 0;
timeouts.WriteTotalTimeoutConstant = 5000;
timeouts.WriteTotalTimeoutMultiplier = 0;

if(!SetCommTimeouts (m_hComm, &timeouts))
{
CloseHandle(m_hComm);
DWORD dwRes = GetLastError();
WriteLog(1, "Connect failed, SetCommTimeouts,
unspecified error: %d", dwRes);
return dwRes;
}


//Flush the port.
//PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR |
PURGE_RXABORT | PURGE_TXABORT);


// Done with connecting, launch the thread
ResetEvent(m_hStopEvent);
ResetEvent(m_hNewDataEvent);

// start the thread
if (!m_bThreadRunning)
{
WriteLog(1, "Connect OK, Trying to start the thread");
Start();
}
else
WriteLog(1, "Connect OK, Thread is already running");

//connect
HandleConnect();

return 0;
}


unsigned __stdcall Datatal::CSerialSocket::ThreadFunc(void* pClient)
{
OVERLAPPED ovRead, ovWrite,
ovEvents;
HANDLE hEvents[5];
DWORD dwWait;
bool bCanRun = true;
DWORD dwEventMask;
bool bSending = false;
bool bReading = false;
bool bNewData = false;
bool bReadComplete = false;
char
szInBuffer[_COMLIB_WORK_SIZE_]; //Out workbuffer,
char
szOutBuffer[_COMLIB_WORK_SIZE_]; //Out workbuffer,
DWORD dwBytesRead = 0;
DWORD dwFlags = 0;

//fetch the parameters
CSerialSocket& client =
*(CSerialSocket*)pClient;

client.WriteLog(2, "ThreadFunc, Thread is running...");

// Overlapped structures.
memset(&ovRead, 0, sizeof(ovRead));
memset(&ovWrite, 0, sizeof(ovWrite));
memset(&ovEvents, 0, sizeof(ovEvents));

//Create events
ovRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!ovRead.hEvent)
{
client.m_bThreadRunning = false;
client.WriteLog(1, "Failed to create ovRead event: %d",
GetLastError());
return 0;
}
ovWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!ovWrite.hEvent)
{
client.m_bThreadRunning = false;
client.WriteLog(1, "Failed to create ovWrite event: %d",
GetLastError());
return 0;
}
ovEvents.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (!ovEvents.hEvent)
{
client.m_bThreadRunning = false;
client.WriteLog(1, "Failed to create ovEvents event:
%d", GetLastError());
return 0;
}

//Event array
hEvents[0] = client.m_hStopEvent;
hEvents[1] = ovRead.hEvent;
hEvents[2] = ovWrite.hEvent;
hEvents[3] = ovEvents.hEvent;
hEvents[4] = client.m_hNewDataEvent;

// Want to know when some new data have arrived.
dwEventMask = EV_RXCHAR;

//Tell the class that we are running know
client.m_bThreadRunning = true;

while ( bCanRun )
{


//If we are not connected, do a sleep and then continue.
Do this until we are signalled to stop.
if (!client.IsConnected() && bCanRun)
{
bSending = false;
bReading = false;
bNewData = false;
bReadComplete = false;

//Check if we should reconnect.
if (client.GetReconnect())
{
client.WriteLog(3, "Threadfunc,
Reconnect!");
client.Connect();
}

if (!client.IsConnected())
{
client.WriteLog(2, "ThreadFunc, Waiting
15 seconds or on die event..");
DWORD dwRes =
WaitForSingleObject(client.m_hStopEvent, 15000);
if (dwRes == WAIT_OBJECT_0) bCanRun
= false;
continue;
} // if (!client.IsConnected())
else
Sleep(1000);
}


// Tell us that we want to Receive data

if (bReading)
dwWait = WaitForMultipleObjects (5, hEvents,
FALSE, INFINITE);
else
dwWait = 1000; //will not be found in the
switch.

switch ( dwWait )
{

// Stop event
case WAIT_OBJECT_0:
bCanRun = false;
client.WriteLog(1, "DIE EVENT triggered.");
continue;
break;

// Overlapped Read is completed.
case WAIT_OBJECT_0 + 1:
client.WriteLog(2, "Read -> completed");
ResetEvent(ovRead.hEvent);
bReadComplete = true;
break;

// Overlapped write is completed.
case WAIT_OBJECT_0 + 2:
client.HandleSendComplete();
ResetEvent(ovWrite.hEvent);
bSending = false;
if (client.m_nOutBufSize)
bNewData = true;
else
bNewData = false;
client.WriteLog(2, "Write -> completed");
break;


// A Send() have been done.
case WAIT_OBJECT_0 + 4:
client.WriteLog(1, "send -> triggered");
ResetEvent(client.m_hNewDataEvent);
bNewData = true;
break;

// Received new data
case WAIT_OBJECT_0 + 3:
client.WriteLog(1, "read -> event");
ResetEvent(ovEvents.hEvent); //reset it before.
break;

case 1000:
break;

default:
client.WriteLog(1, "ThreadFunc, Incorrect dwRes:
%d, error: %d\n", dwWait, GetLastError());
break;


} //switch


//dwBytesRead = 0;
if (!bReading)
{
bReading = true;
bReadComplete = false;
memset(szInBuffer, 0, _COMLIB_WORK_SIZE_);

//_COMLIB_WORK_SIZE_
client.WriteLog(3, "read -> ReadFile, event:
%d", ovRead.hEvent);
if (!ReadFile(client.m_hComm, szInBuffer, 1,
&dwBytesRead, &ovRead))
{
int nErr = GetLastError();
if (nErr != ERROR_IO_PENDING)
{
client.WriteLog(1, "ReadFile
failed! Error %d.\n", GetLastError());
client.Disconnect();
client.HandleDisconnect();
continue;

}

client.WriteLog(3, "Read -> Pending read
inited", dwBytesRead);
}
else
{
client.WriteLog(3, "Read -> completed
directly, %d bytes", dwBytesRead);
if (dwBytesRead == 0)
{
bReadComplete = false;
bReading = false;
client.WriteLog(3, "Read ->
Nothing read, hotfix, do a read again.");
}
else
{
bReadComplete = true;
bReading = false;
}
}
}

if (bReadComplete)
{

//If a read was not completed directly,
dwReadBytes = 0, fetch overlapped result.
if (dwBytesRead == 0)
{
dwFlags = 0;
if(!GetOverlappedResult(client.m_hComm,
&ovRead, &dwBytesRead, false))
{
client.WriteLog(1,
"GetOverlappedResult() failed on Read! Bytes read is %d. Error %d.\n",
dwBytesRead, GetLastError());
client.Disconnect();
client.HandleDisconnect();
continue;
}
}

//Closed?
if ( dwBytesRead == 0)
{
client.WriteLog(1, "Socket closed by
Host when waiting for Read..\n");
client.Disconnect();
client.HandleDisconnect();
continue;
}

client.m_critRead.Lock();

if (client.m_nInBufSize + dwBytesRead >
_COMLIB_IN_SIZE_)
{
client.m_critRead.Unlock();
client.WriteLog(1, "Read -> BUFFER
OVERFLOW, disconnecting");
client.Disconnect();
continue;
}

//Copy Received data to our inbuffer
memcpy(client.m_pInBuf + client.m_nInBufSize,
szInBuffer, dwBytesRead);
client.m_nInBufSize += dwBytesRead;
client.WriteLog(3, "Read -> Data: %s, bytes:
%d", szInBuffer, dwBytesRead);

//Tell derived class that new data is in the
buffer
//More than one packet can be in the same
buffer, so handle it.
int nBytesOk;
nBytesOk = client.HandleReceive(client.m_pInBuf,
client.m_nInBufSize);
client.WriteLog(3, "Read -> Bytes handled: %d",
nBytesOk);

//Check if there are more data to trigger on.
int nByteCount = nBytesOk;
while (nBytesOk && (nByteCount <
client.m_nInBufSize))
{
nBytesOk =
client.HandleReceive(client.m_pInBuf + nByteCount, client.m_nInBufSize -
nByteCount);
client.WriteLog(3, "Read -> Bytes
handled2: %d", nBytesOk);
nByteCount += nBytesOk;
}

//derived class should return the number of
bytes that were accepted
//remove those bytes from the buffer
if (nByteCount > 0)
{
//WHole buffer?
if (nByteCount == client.m_nInBufSize)
{
client.WriteLog(3, "Read ->
Cleahing whole buffer");
memset(client.m_pInBuf, 0,
_COMLIB_IN_SIZE_);
client.m_nInBufSize = 0;
}
else
{
client.WriteLog(3, "Read ->
Cleaing partial buffer");
memmove(client.m_pInBuf,
client.m_pInBuf + nByteCount, client.m_nInBufSize - nByteCount);
client.m_nInBufSize -=
nByteCount;

} //if (nBytesOk == *pTP->pnInBufSize)

} //if (nBytesOk > 0)

memset(szInBuffer, 0, _COMLIB_WORK_SIZE_);
bReadComplete = false;
bReading = false;
client.m_critRead.Unlock();
}

//We gotto write some stuff, and we only do it if no
operations are ongoing
if (bNewData && !bSending)
{
//Lock buffer and fetch data
client.m_critWrite.Lock();
ResetEvent(client.m_hNewDataEvent);

bSending = true;
int nBufferLen = 0;

//TODO: large for our workbuffer?
if (client.m_nOutBufSize > _COMLIB_WORK_SIZE_)
{
memcpy(szOutBuffer, client.m_pOutBuf,
_COMLIB_WORK_SIZE_); // fill the buffer
memmove(client.m_pOutBuf,
client.m_pOutBuf + _COMLIB_WORK_SIZE_, client.m_nOutBufSize -
_COMLIB_WORK_SIZE_);
memset(client.m_pOutBuf +
_COMLIB_OUT_SIZE_ - _COMLIB_WORK_SIZE_, 0, _COMLIB_OUT_SIZE_ -
_COMLIB_WORK_SIZE_);
nBufferLen = _COMLIB_WORK_SIZE_;
client.m_nOutBufSize -=
_COMLIB_WORK_SIZE_;
}
else
{
memcpy(szOutBuffer, client.m_pOutBuf,
client.m_nOutBufSize);
memset(client.m_pOutBuf, 0,
_COMLIB_OUT_SIZE_);
nBufferLen = client.m_nOutBufSize;
client.m_nOutBufSize = 0;
}

DWORD dwBytesWritten;
if (!WriteFile(client.m_hComm, szOutBuffer,
nBufferLen, &dwBytesWritten, &ovWrite))
{
//Overlapped?
if (GetLastError() != ERROR_IO_PENDING)
{
client.m_critWrite.Unlock();
client.WriteLog(3, "WriteFile()
failed! Error %d", GetLastError());
client.Disconnect();
client.HandleDisconnect();
break;
}
bSending = true; //only set pending
write if we do not complete directly.
}
else
bSending = false;

client.m_critWrite.Unlock();


} //if (!bSending && !bReading)


} //while

client.m_bThreadRunning = false;
return 0;
}
GeneralRe: Serial communication problem Pin
justin22318-Jun-03 3:41
justin22318-Jun-03 3:41 
GeneralRe: Serial communication problem Pin
justin22318-Jun-03 4:01
justin22318-Jun-03 4:01 
Generalproblem with EnumDisplayDevices(..) Pin
Anonymous18-Jun-03 2:03
Anonymous18-Jun-03 2:03 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Anonymous18-Jun-03 4:18
Anonymous18-Jun-03 4:18 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Anonymous18-Jun-03 4:51
Anonymous18-Jun-03 4:51 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Ryan Binns18-Jun-03 15:31
Ryan Binns18-Jun-03 15:31 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Hatem Darweesh18-Jun-03 20:56
Hatem Darweesh18-Jun-03 20:56 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Ryan Binns18-Jun-03 21:06
Ryan Binns18-Jun-03 21:06 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Hatem Darweesh18-Jun-03 21:18
Hatem Darweesh18-Jun-03 21:18 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Ryan Binns18-Jun-03 21:33
Ryan Binns18-Jun-03 21:33 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Hatem Darweesh18-Jun-03 22:02
Hatem Darweesh18-Jun-03 22:02 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Anonymous18-Jun-03 22:09
Anonymous18-Jun-03 22:09 
GeneralRe: problem with EnumDisplayDevices(..) Pin
Ryan Binns18-Jun-03 22:21
Ryan Binns18-Jun-03 22:21 
Generaluse of CFrameWnd Pin
gucy18-Jun-03 0:40
gucy18-Jun-03 0:40 
GeneralLogonUser without Password Pin
isriniv18-Jun-03 0:22
isriniv18-Jun-03 0:22 
GeneralRe: LogonUser without Password Pin
geo_m18-Jun-03 0:45
geo_m18-Jun-03 0:45 
GeneralRe: LogonUser without Password Pin
AlexO18-Jun-03 3:58
AlexO18-Jun-03 3:58 

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.