Бывалый

Профиль
Группа: Участник
Сообщений: 207
Регистрация: 2.9.2007
Репутация: нет Всего: нет
|
| Код | }//ThreadSend
DRIVER_API bool __cdecl DrvMsgSend( UCHAR * buffer,int DataSize ) { hMutexRecv = CreateMutex(NULL, FALSE, NULL); if ( hMutexRecv == NULL ) { ExitProcess( ErrMutexRecv ) ; } hMutexSend = CreateMutex(NULL, FALSE, NULL); if ( hMutexSend == NULL ) { ExitProcess( ErrMutexSend ) ; } hThreadSerialClose = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadSerialClose, // thread function 0, // argument to thread function 0, // use default creation flags 0); // returns the thread identifier if ( hThreadSerialClose == NULL ) { ExitProcess( ErrThreadCreate ); }
hThreadQSend = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadQSend, // thread function 0, // argument to thread function 0, // use default creation flags 0); // returns the thread identifier if ( hThreadQSend == NULL ) { ExitProcess( ErrThreadCreate ); } hThreadRecv = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadRecv, // thread function DrvBufferSend, // argument to thread function 0, // use default creation flags 0); // returns the thread identifier if ( hThreadRecv == NULL ) { ExitProcess( ErrThreadCreate ); }
hThreadSend = CreateThread( NULL, // default security attributes 0, // use default stack size ThreadSend, // thread function 0, // argument to thread function 0, // use default creation flags 0); // returns the thread identifier if ( hThreadSend == NULL ) { ExitProcess( ErrThreadCreate ); }
|
Добавлено через 48 секунд| Код |
DrvBufSendSize = DataSize+2 ; //Размер массива в драйвере WORD rc ; //CRC CCRC() ; if((rc = calc16( (BYTE*) &buffer,(WORD)DataSize))== 0 ) { return 0 ; } if ( DrvBufferSend == NULL ) { DrvBufferSend = new UCHAR[DrvBufSendSize] ; } memcpy( &DrvBufferSend[0],&buffer[0],DataSize ) ; // (WORD &)( *(DrvBufferSend +DataSize) ) = rc ; //Добавление crc в буфер //#ifdef _WINDOWS FQSendWrite = true ; //#endif _WINDOWS //Флаг-записать в очередь delete [] buffer ; return true ;
}//DrvMsgSend
//---------------------PING---------------------------------------- #ifdef _WINDOWS DWORD __cdecl ThreadConExist( LPVOID lpParamConExist )
{ while( !StopThreadConExist ) { if( qSend.empty() && qRecv.empty() && FSerialSendIsClosed && FSerialRecvIsClosed ) { qSend.push ( (const char &)Ping ) ; Sleep(3000) ; } } return 0 ; }
#endif _WINDOWS
|
Добавлено через 1 минуту и 51 секунду| Код |
serial.cpp
#include "stdafx.h" //#include <vcl.h>
#include "serial.h"
#define my_printf #define min(a, b) (((a) < (b)) ? (a) : (b))
static DCB backup_dcb;
/* Close the serial port and restore old settings. */ int serial_close(HANDLE hPort) { /* disable event notification and wait for thread to halt */ SetCommMask(hPort, 0);
/* purge any outstanding reads/writes and close device handle */ PurgeComm(hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
/* drop DTR */ EscapeCommFunction(hPort, CLRDTR);
if (SetCommState(hPort, &backup_dcb)==0) my_printf("SetCommState in serial_close");
if( CloseHandle(hPort)==0 ) my_printf("CloseHandle in serial_close");
return 0; }
HANDLE serial_open(LPCTSTR FileName, int Speed) { HANDLE hPort; DCB dcb;
hPort = CreateFile(FileName, GENERIC_READ | GENERIC_WRITE, 0, /* exclusive access */ NULL, /* no security attrs */ OPEN_EXISTING, 0, /* overlapped I/O */ NULL);
if (hPort == INVALID_HANDLE_VALUE) { my_printf("CreateFile in serial_open"); return NULL; }
backup_dcb.DCBlength = sizeof(DCB); if( GetCommState(hPort, &backup_dcb)==0 ) { my_printf("ReadDevice in serial_open"); return NULL; }
SetCommMask(hPort, EV_RXCHAR);
/* try to setup device buffers */ if( SetupComm(hPort, 4096, 4096) == 0 ) // my settings my_printf("Setup device buffers failed. See GetLastError()\n");
/* purge any information in the buffer */ PurgeComm(hPort, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
/* Initialise the port settings */ memcpy(&dcb, &backup_dcb, sizeof(DCB));
dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; dcb.BaudRate = Speed;
/* No Xon/Xof flow control */ if (SetCommState(hPort, &dcb)==0) { my_printf("WriteDevice in serial_open"); return NULL; }
return hPort; }
int serial_setparity(HANDLE hPort, bool parity) { DCB dcb;
dcb.DCBlength = sizeof(DCB); if( GetCommState(hPort, &dcb)==0 ) { my_printf("ReadDevice in serial_setparity"); return 1; }
if (parity) dcb.Parity = ODDPARITY; else dcb.Parity = NOPARITY;
if( SetCommState(hPort, &dcb)==0 ) { my_printf("WriteDevice in serial_setparity"); return 2; }
return 0; }
|
Добавлено через 2 минуты и 39 секунд| Код |
/* Set the DTR and RTS bit of the serial device. */ int serial_setdtrrts(HANDLE hPort, bool dtr, bool rts) { DCB dcb;
dcb.DCBlength = sizeof(DCB); if (GetCommState(hPort, &dcb)==0) { my_printf("ReadDevice in serial_setdtrrts"); return 1; }
dcb.fOutxDsrFlow = 0; if (dtr) dcb.fDtrControl = DTR_CONTROL_ENABLE; else dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fOutxCtsFlow = 0; if (rts) dcb.fRtsControl = RTS_CONTROL_ENABLE; else dcb.fRtsControl = RTS_CONTROL_DISABLE;
/* no software (Xon/Xof) flow control */ dcb.fInX = dcb.fOutX = 0;
if (SetCommState(hPort, &dcb)==0) { my_printf("WriteDevice in serial_setdtrrts"); return 2; }
dcb.DCBlength = sizeof(DCB); GetCommState(hPort, &dcb);
my_printf("Serial device:"); my_printf(" DTR is "); switch (dcb.fDtrControl) { case DTR_CONTROL_ENABLE : my_printf("up"); break; case DTR_CONTROL_DISABLE : my_printf("down"); break; case DTR_CONTROL_HANDSHAKE : my_printf("handshake"); break; } my_printf(", RTS is "); switch (dcb.fRtsControl) { case RTS_CONTROL_ENABLE : my_printf("up"); break; case RTS_CONTROL_DISABLE : my_printf("down"); break; case RTS_CONTROL_HANDSHAKE : my_printf("handshake"); break; case RTS_CONTROL_TOGGLE : my_printf("toggle"); break; } my_printf("\n"); if ( dtr && dcb.fDtrControl != DTR_CONTROL_ENABLE ) return 3; if (!dtr && dcb.fDtrControl != DTR_CONTROL_DISABLE) return 4; if ( rts && dcb.fRtsControl != RTS_CONTROL_ENABLE ) return 5; if (!rts && dcb.fRtsControl != RTS_CONTROL_DISABLE) return 6;
return 0; }
/* Change the speed of the serial device. * RETURNS: Success */ int serial_setspeed(HANDLE hPort, int speed) { DCB dcb;
dcb.DCBlength = sizeof(DCB); if (GetCommState(hPort, &dcb)==0) { my_printf("Failed GetCommState in serial_setspeed"); return 1; }
dcb.BaudRate = speed;
if (SetCommState(hPort, &dcb)==0) { my_printf("Failed SetCommState in serial_setspeed"); return 2; }
return 0; }
/* Read from serial device. */ int serial_read(HANDLE hPort, void *buf, int nbytes) { bool fReadStat; COMSTAT ComStat; DWORD dwErrorFlags; DWORD dwLength=0; DWORD dwReturn = 0;
/* only try to read number of bytes in queue */ if( ClearCommError(hPort, &dwErrorFlags, &ComStat) == 0 ) my_printf("Error %d wait comm event.\n", GetLastError());
dwLength = min((DWORD) nbytes, ComStat.cbInQue); if (dwLength > 0) { fReadStat = ReadFile(hPort, buf, dwLength, &dwReturn, NULL); if (!fReadStat) my_printf("Read from COM failed\n"); }
#if FULL_SERIAL_DUMP if( dwLength > 0 ) { DWORD i; char St[100] = "";
my_printf("Received %d bytes from COM:", dwLength); for(i=0; i<dwLength; i++) { char sDig[10];
if( (i & 0xF) == 0 ) { my_printf("%s\n", St); St[0] = 0; } sprintf(sDig, "%02x ", ((BYTE*)buf)[i]); strcat(St, sDig); } my_printf("%s\n", St); } #endif
return (dwReturn); }
/* Write to serial device. */ int serial_write(HANDLE hPort, void *buf, int nbytes) { bool fWriteStat; DWORD dwBytesSent = 0;
fWriteStat = WriteFile(hPort, buf, nbytes, &dwBytesSent, NULL);
/* Note that normally the code will not execute the following * because the driver caches write operations. Small I/O requests * (up to several thousand bytes) will normally be accepted * immediately and WriteFile will return true even though an * overlapped operation was specified */ if (!fWriteStat) my_printf("Write to COM failed\n");
#if FULL_SERIAL_DUMP if( dwBytesSent > 1 ) { DWORD i; char St[100] = "";
my_printf("Sent to COM %d bytes:", dwBytesSent); for(i=0; i<dwBytesSent; i++) { char sDig[10];
if( (i & 0xF) == 0 ) { my_printf("%s\n", St); St[0] = 0; } sprintf(sDig, "%02x ", ((BYTE*)buf)[i]); strcat(St, sDig); } my_printf("%s\n", St); } #endif return dwBytesSent; }
|
|