Требуется программист в Зеленограде
- обработка данных с датчиков; ColdFire; 40 тыс. e-mail:jobsmp@pochta.ru |
Вот пример из боевой проги:
//---------------------------------------------------------------------------
BOOL CloseCommPort(void)
{
if (MyThreadFlag)
{
MyThreadFlag = -1;
while (MyThreadFlag != 0)
{
Sleep(10);
}
Sleep(10);
CloseHandle(hCom);
CommOpenFlag = 0;
}
return(0);
}
//---------------------------------------------------------------------------
BOOL OpenCommPort(long CommNum, long BaudRate, char Parity)
{
CloseCommPort();
CommOpenFlag = 0;
sprintf(CommPortMes, "----");
sprintf(CommResultMes, "----");
sprintf(CommStatusMes, "----");
if (CommNum < 1 || CommNum > 8) return(FALSE);
sprintf(CommPortMes, "Com%u", CommNum);
hCom = CreateFile(CommPortMes, // pointer to name of the file
GENERIC_READ | GENERIC_WRITE, // access (read-write) mode
0, // share mode (exclusive access)
NULL, // pointer to security attributes (no)
OPEN_EXISTING, // how to create (must use OPEN_EXISTING)
0, // file attributes (not overlapped I/O)
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
sprintf(CommResultMes, "Cannot open");
return(FALSE);
}
sprintf(CommResultMes, "Ok");
CommPortNum = CommNum;
fResult = GetCommState(hCom, &dcb);
dcb.BaudRate = BaudRate;
dcb.ByteSize = 8;
if (Parity == 'o') dcb.Parity = ODDPARITY;
if (Parity == 'e') dcb.Parity = EVENPARITY;
if (Parity == 'n') dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = True; // binary mode, no EOF check
dcb.fParity = True; // enable parity checking
dcb.fOutxCtsFlow = False; // CTS output flow control
dcb.fOutxDsrFlow = False; // DSR output flow control
dcb.fDtrControl = DTR_CONTROL_DISABLE;// DTR flow control type
dcb.fDsrSensitivity = False; // DSR sensitivity
dcb.fTXContinueOnXoff = True; // XOFF continues Tx
dcb.fOutX = False; // XON/XOFF out flow control
dcb.fInX = False; // XON/XOFF in flow control
dcb.fErrorChar = False; // enable error replacement
dcb.fNull = False; // enable null stripping
dcb.fRtsControl = RTS_CONTROL_DISABLE;// RTS flow control
dcb.fAbortOnError = False; // abort reads/writes on
fResult = SetCommState(hCom, &dcb);
if (!fResult)
{
dwError = GetLastError();
sprintf(CommStatusMes, "StateError");
return(FALSE);
}
else
{
dwError = GetLastError();
sprintf(CommStatusMes, "Ok");
}
CommTimeOuts[0] = MAXDWORD; // ReadIntervalTimeout
CommTimeOuts[1] = 0; // ReadTotalTimeoutMultiplier
CommTimeOuts[2] = 0; // ReadTotalTimeoutConstant
CommTimeOuts[3] = 1; // WriteTotalTimeoutMultiplier
CommTimeOuts[4] = 1; // WriteTotalTimeoutConstant
fResult = SetCommTimeouts(hCom, (LPCOMMTIMEOUTS) CommTimeOuts);
if (!fResult)
{
dwError = GetLastError();
sprintf(CommStatusMes, "TimeOutError %x", dwError);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), CommStatusMes, 390, NULL);
sprintf(CommStatusMes, "TimeOutError %x", dwError);
return(FALSE);
}
else
{
sprintf(CommStatusMes, "Ok %X %x %x %x %x", CommTimeOuts[0], CommTimeOuts[1],
CommTimeOuts[2], CommTimeOuts[3], CommTimeOuts[4]);
}
ClearCommError(hCom, &Errors, NULL);
CommOpenFlag = 1;
CreateMyThread();
if (MyThreadFlag != 1)
{
CloseCommPort();
return(FALSE);
}
return(TRUE);
}
//---------------------------------------------------------------------------
void MyThread(void)
{
static int len = 0;
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
while (MyThreadFlag == 1)
{
Sleep(10);
len = FIFO_LENGTH - Fifo_Last;
fResult = ReadFile(hCom, &FIFO[Fifo_Last], len, &NumberOfBytesRead, NULL);
ClearCommError(hCom, &Errors, NULL);
if (Errors)
{
if (Errors & CE_RXOVER) SoftOverCnt++;
if (Errors & CE_OVERRUN) OverRunCnt++;
if (Errors & CE_RXPARITY) ParityCnt++;
if (Errors & CE_FRAME) FrameCnt++;
if (Errors & CE_BREAK) BreakCnt++;
ErrorCnt++;
}
if (fResult == 0)
{
// Sleep(100);
continue;
}
Fifo_Last += NumberOfBytesRead; // Продвигаем указатель.
Fifo_Last &= (FIFO_LENGTH - 1); // И заворачиваем его.
if (fResult && NumberOfBytesRead == 0) TimeOutFlag = 1;
else TimeOutFlag = 0;
if (TimeOutFlag || Errors)
{
InputByte = 0;
CommReadState = 0;
}
}
MyThreadFlag = 0;
ExitThread(0);
}
//---------------------------------------------------------------------------
void CreateMyThread(void)
{
hMyThread = CreateThread(
NULL, // pointer to thread security attributes
0, // initial thread stack size, in bytes (default)
(unsigned long (__stdcall *)(void *)) MyThread, // pointer to thread func
0, // argument for new thread
0, // creation flags (CREATE_SUSPENDED ?)
&lpMyThreadId // pointer to returned thread identifier
);
if (hMyThread != INVALID_HANDLE_VALUE) MyThreadFlag = 1;
}
//---------------------------------------------------------------------------
int GetByte(void)
{
static int c = 0;
if (Fifo_First == Fifo_Last) return -1; // Если буфер пуст.
c = FIFO[Fifo_First]; // Берём один байт.
c &= 0x000000FF;
Fifo_First++; // Двигаем указатель.
Fifo_First &= (FIFO_LENGTH - 1); // Заворачиваем указатель.
return c;
}
//---------------------------------------------------------------------------