WWW.ИСХОДНИКИ.РУ cpp.sources.ru
java.sources.ru web.sources.ru soft.sources.ru
jdbc.sources.ru asp.sources.ru api.sources.ru

  Форум на исходниках
  C / C++ / Visual C++
  Kernel и Com... Интерестная лажа...

СПРОСИТЬ  ОТВЕТИТЬ
профайл | регистрация | faq

Автор Тема:   Kernel и Com... Интерестная лажа...
karatay опубликован 21-10-2001 11:49 MSK   Click Here to See the Profile for karatay   Click Here to Email karatay  
Плдскажите PLZ, В чем может быть проблемма? На CBuilder'e опрашиваеться Com порт каждую секунду, но иногда вылетает прога по недопустимому обращению к памяти, за программой вылетает kernel... Такоеже происходит и при переходе в спящий режим... Что это за лажа? И как с ней можно бороться?
lamo опубликован 21-10-2001 13:33 MSK     Click Here to See the Profile for lamo  Click Here to Email lamo     
юзай мс компилеры для мс.
Chief опубликован 21-10-2001 17:19 MSK     Click Here to See the Profile for Chief  Click Here to Email Chief     
ты бы кинул сорцу.......как опрашиваешь.....у меня и чаще сукунды опрос идет по нескольким портам и ничё несбоит
karatay опубликован 21-10-2001 19:24 MSK     Click Here to See the Profile for karatay  Click Here to Email karatay     
bool bResult;
char inBuffer[256];
int nBytesToRead;
unsigned long nBytesRead;
int dwError;
AnsiString ComAnsver, ComAnsverString;
bool isPrint;

// attempt an asynchronous read operation

BOOL fReadStat ;
COMSTAT ComStat ;
DWORD dwErrorFlags, dwLength ;
int nError, nLength ;
char szError[10] ;

SaveLogo("", "DevRead() - 1");

ClearCommError( m_hCom, &dwErrorFlags, &ComStat ) ;
if (dwErrorFlags > 0)
{
//+++
//ShowMessage("Ошибка установки флагов порта");
}

dwLength = ComStat.cbInQue;

//Нет данных для чтения в порте
if (dwLength < 0)
return false;

strcpy(inBuffer, "");
bResult = ReadFile(m_hCom, &inBuffer, dwLength, &nBytesRead, &READ_COM);

//==============================================================================
// if there was a problem, or the async. operation's still pending ...
if (!bResult)
{
READ_COM.Offset += nBytesRead;
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
MessageBox(NULL, "Ошибка. Порт закрыт или занят другим приложением.",
"Information", MB_OK|MB_ICONINFORMATION);
//- Попытка открытьпорт
}

case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress
MessageBox(NULL, "Ошибка. Порт занят.",
"Information", MB_OK|MB_ICONINFORMATION);
return false;
// check on the results of the asynchronous read
bResult = GetOverlappedResult(m_hCom, &READ_COM,
&nBytesRead, FALSE) ;
// if there was a problem ...
if (!bResult)
{
// deal with the error code
switch (dwError = GetLastError())
{
case ERROR_HANDLE_EOF:
{
// we're reached the end of the file
//during asynchronous operation
MessageBox(NULL, "Ошибка. Ошибка. Порт закрыт или занят другим приложением.",
"Information", MB_OK|MB_ICONINFORMATION);
return false;
}

// deal with other error cases
MessageBox(NULL, "Ошибка. Некоректное чтение из потока COM порта!",
"Information", MB_OK|MB_ICONINFORMATION);
return false;
}
}
READ_COM.Offset += nBytesRead;
} // end case
// deal with other error cases

MessageBox(NULL, "Ошибка чтения из COM порта!",
"Information", MB_OK|MB_ICONINFORMATION);
return false;
} // end switch
} // end if

SaveLogo("", "DevRead() - 2");

READ_COM.Offset += nBytesRead - 1;
ComAnsver = inBuffer;
if(ComAnsver.Length() > nBytesRead)
{
ComAnsver.Delete(nBytesRead + 1, ComAnsver.Length() - nBytesRead + 1);
}
LogosMemo->Lines->SetText((LogosMemo->Lines->GetText() + ComAnsver).c_str());
Analis();
return true;

Обрабатываю то что лежит в LogosMemo

migel опубликован 22-10-2001 10:52 MSK     Click Here to See the Profile for migel  Click Here to Email migel     
Ну вот ваи и ответ:

dwLength = ComStat.cbInQue;
//Нет данных для чтения в порте

if (dwLength < 0)
return false;
strcpy(inBuffer, "");
bResult = ReadFile(m_hCom, &inBuffer, dwLength, &nBytesRead, &READ_COM);
А если принято больше 256 байт?
надо писать
dwLength = ComStat.cbInQue > sizeof(inBuffer) ? sizeof(inBuffer) : ComStat.cbInQue;
и если пришло больше 256 байт, то читать в цикле.

СПРОСИТЬ  ОТВЕТИТЬ
Перейти:


E-mail | WWW.ИСХОДНИКИ.RU

Powered by: Ultimate Bulletin Board, Freeware Version 5.10a
Purchase our Licensed Version- which adds many more features!
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.