[an error occurred while processing this directive]
Так и у меня тоже. Привожу свою программу целиком
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено _Bill 11 октября 2006 г. 00:44
В ответ на: А таймауты на 100мс для чтения у меня стоят отправлено <font color=gray>arreg</font> 11 октября 2006 г. 00:24

Все происходит по событиям таймера (период примерно 100 мс).


void SendCmd(char command, char data)
{
static char _buf[2];
unsigned long _nbytes;

_buf[0] = command;
_buf[1] = data;
WriteFile(PortHandle, &_buf, 2, &_nbytes, NULL);
FlushFileBuffers(PortHandle);
}

TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::BitBtn3Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::BitBtn1Click(TObject *Sender)
{
static char PortName[6], MsgBuffer[256];
unsigned short i;
DWORD _evtMask;
// DWORD numRead;

strcpy(PortName, (PortBox->Items->Strings[PortBox->ItemIndex]).c_str());
if (PortHandle != NULL)
{
SetCommState(PortHandle, &SaveDCB);
CloseHandle(PortHandle);
}
if ((PortHandle = CreateFile(PortName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
{
ErrorMsg("Can't open port " + AnsiString(PortName));
return;
}
GetCommState(PortHandle, &PortDCB);
if (SaveDCB.DCBlength == 0)
SaveDCB = PortDCB;
//
// Setup hardware interface
//
PortDCB.fDtrControl = DTR_CONTROL_DISABLE;
PortDCB.BaudRate = CBR_14400;
PortDCB.StopBits = ONESTOPBIT;
PortDCB.ByteSize = 8;
PortDCB.Parity = NOPARITY;
PortDCB.fParity =
PortDCB.fDsrSensitivity =
PortDCB.fInX =
PortDCB.fAbortOnError =
PortDCB.fNull = false;
PortDCB.EvtChar = 'R';
SetCommState(PortHandle, &PortDCB);
//
// Set timeouts
//
PortTimeouts.ReadIntervalTimeout = 50;
PortTimeouts.ReadTotalTimeoutMultiplier =
PortTimeouts.ReadTotalTimeoutConstant = 50;
PortTimeouts.WriteTotalTimeoutMultiplier =
PortTimeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(PortHandle, &PortTimeouts);
SetupComm(PortHandle, 32, 32); // Setup communication buffers size
// SetCommMask(PortHandle, EV_RXFLAG);
// Timer1->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::FormCreate(TObject *Sender)
{
PortHandle = NULL;
SaveDCB.DCBlength = 0;
SpeedButton1->Down = false;
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::Button1Click(TObject *Sender)
{
Timer1->Enabled = true;
}
//---------------------------------------------------------------------------
#define NBYTES ((sizeof (struct _data)) + 1)

void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
static unsigned long evMask = EV_RXFLAG;
bool result;
DWORD numRead;
static char _buf[16];

SendCmd(RQST_CMD, 0); // Request of info

result = ReadFile(PortHandle, _buf, (sizeof (struct _data) +1), &numRead, NULL);
if (!result || numRead {
Timer1->Enabled = false;
InformMsg("Time out!\n"+IntToStr(numRead)+" bytes are read");
return;
}
if (_buf[0] != RQST_ANS) // Unknown reply
{
InformMsg("Unknown reply");
return;
}
Data.arc = _buf[1]; // Fill the data structure
Data.pitch = _buf[2]; //
Data.bank = _buf[3]; //
Data.status = _buf[4]; //
//
// Display the new device state
//
StaticText1->Caption = IntToStr(Data.pitch);
StaticText2->Caption = IntToStr(Data.bank);
StaticText3->Caption = IntToStr(Data.arc);
if (Data.status & EL_START_BIT)
{
Label5->Show();
Shape2->Brush->Color = clLime;
Label5->Caption = "Начало";
Label5->Font->Color = clGreen;
}
else if (Data.status & EL_END_BIT)
{
Label5->Show();
Shape2->Brush->Color = clRed;
Label5->Caption = "Конец";
Label5->Font->Color = clRed;
}
else {
Label5->Hide();
Shape2->Brush->Color = clGreen;
}
if (Data.status & ARC_SC_BIT)
{
Shape1->Brush->Color = clLime;
}
else {
Shape1->Brush->Color = clGreen;
}
}


Составить ответ  |||  Конференция  |||  Архив

Ответы


Отправка ответа
Имя (обязательно): 
Пароль: 
E-mail: 

Тема (обязательно):
Сообщение:

Ссылка на URL: 
Название ссылки: 
URL изображения: 


Rambler's Top100 Рейтинг@Mail.ru
Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание