Code:// Konstanden für Parity #define P_NONE 0 // 0 = kein Parity Bit #define P_EVEN 1 // 1 = gerade #define P_ODD 2 // 2 = ungerade #define P_SPACE 3 // 3 = immer 0 #define P_MARK 4 // 4 = immer 1 #define D_7BIT 0 // 7-Databits #define D_8BIT 1 // 8-Databits #define S_1BIT 0 // 1 Stopbit #define S_1_5BIT 1 // 1.5 Stopbits #define S_2BIT 2 // 2 Stopbits int ComOpen (unsigned Nr,int Baud=9600,int Parity=P_NONE,int Stopbits=S_1BIT,int Databits=D_8BIT);
Code://***************************************************************************** // Öffnet eine serielle Verbindung // Nr : Ist die Nummer des Com-Ports (0=COM1 1=COM2 ...) // Baud : Ist die Bautrate // Parity : 0 = kein Parity Bit // 1 = gerade // 2 = ungerade // 3 = immer 0 // 4 = immer 1 // Stopbits : 0 = Ein Stopbit // 1 = Ein/einhalb Stopbits // 2 = Zwei Stopbits // Bits : 0 = 7 Datenbits // 1 = 8 Datenbits // 7 = 7 Datenbits // 8 = 8 Datenbits // Ergibt 1 wenn eine Schnittstelle geöffnet wurde int ComOpen(unsigned Nr,int Baud,int Parity,int Stopbits,int Databits) { static const int iPMode[]={NOPARITY,EVENPARITY,ODDPARITY,SPACEPARITY,MARKPARITY}; static const int iSMode[]={ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS}; char cName[]="\\\\.\\COM1"; HANDLE hFile; COMMTIMEOUTS sTo; DCB sDcb; if(Nr>=MAX_COM_PORTS)return 0; if(bIsOpen[Nr])return 0; cName[7]='1'+Nr; hFile= CreateFile(cName,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); if(hFile==INVALID_HANDLE_VALUE) { hFile=0; return 0; } if(Databits==7)Databits=0; memset(&sDcb,0,sizeof(sDcb)); sDcb.DCBlength=sizeof(sDcb); sDcb.BaudRate = Baud; sDcb.fParity = (Parity!=0)? TRUE:FALSE; sDcb.fBinary = TRUE; sDcb.Parity = iPMode[Parity]; sDcb.StopBits = ONESTOPBIT; sDcb.fOutxCtsFlow = FALSE; sDcb.fOutxDsrFlow = FALSE; sDcb.fDtrControl = DTR_CONTROL_ENABLE; sDcb.fRtsControl = RTS_CONTROL_ENABLE; sDcb.fDsrSensitivity= FALSE; sDcb.fAbortOnError = FALSE; sDcb.ByteSize = (Databits)? 8:7; if(!SetCommState(hFile,&sDcb)) { CloseHandle(hFile); return 0; } sTo.ReadIntervalTimeout = MAXDWORD; // 0 ms Read-Tomeout sTo.ReadTotalTimeoutMultiplier = 0; sTo.ReadTotalTimeoutConstant = 0; sTo.WriteTotalTimeoutMultiplier= 1; // 1*2 ms Write Timeout sTo.WriteTotalTimeoutConstant = 2; if(!SetCommTimeouts((HANDLE)hFile,&sTo)) { CloseHandle(hFile); return 0; } hComFile[Nr]=hFile; bIsOpen [Nr]=TRUE; return 1; }







Zitieren

Lesezeichen