void __fastcall TAppForm::Button1Click(TObject *Sender) { // // ***************************************** // Ausschnitt aus Hauptroutine // ***************************************** // if (com_hnd != INVALID_HANDLE_VALUE) { CommClose(com_hnd); com_hnd = INVALID_HANDLE_VALUE; Application->ProcessMessages(); } com_nr = 1; com_hnd = CommOpen(com_nr,dcb,4096,128); if (com_hnd == INVALID_HANDLE_VALUE) { //Message ausgeben } //Aufforderungssequenz an ext. Gerät senden success = GetItemStatus(com_hnd,-1,&Stat[0],tx_mode); if (success == true) { // hier weitermachen } else { //hier Fehlermeldung } } //--------------------------------------------------------- void *__fastcall::CommOpen(int com_nr,DCB dcb,int rec_buf,int trm_buf) { bool success; char com_name[10]; char *s_build; DWORD dummy; void *hnd; COMMTIMEOUTS cto; com_name[0] = 'C'; com_name[1] = 'O'; com_name[2] = 'M'; if (com_nr <= 9) { com_name[3] = 0x30 + com_nr; com_name[4] = '\0'; } else { com_name[3] = 0x30 + (com_nr / 10); com_name[4] = 0x30 + (com_nr % 10); com_name[5] = '\0'; } // hnd = CreateFile(&com_name[0], GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // if (hnd != INVALID_HANDLE_VALUE) { dcb.DCBlength = sizeof(DCB); GetCommState(hnd,&dcb); dcb.BaudRate = CBR_115200; dcb.fBinary = true; dcb.fParity = false; dcb.fOutxCtsFlow = false; dcb.fOutxDsrFlow = false; dcb.fDtrControl = DTR_CONTROL_DISABLE; dcb.fDsrSensitivity = false; dcb.fTXContinueOnXoff = true; //kein XON/XOFF Betrieb dcb.fOutX = false; //kein XON/XOFF Betrieb dcb.fInX = false; //kein XOFF-Char senden dcb.fErrorChar = false; dcb.fNull = false; //keine NULL-Bytes ersetzen dcb.fRtsControl = RTS_CONTROL_DISABLE; dcb.fAbortOnError = false; //weitersenden, wenn Fehler aufgetreten ist dcb.XonChar = 0x0B; //XOn und XOff müssen unterschiedlich dcb.XoffChar = 0xFF; // sein, sonst geht SetCommState nicht dcb.XonLim = rec_buf; // dcb.XoffLim = rec_buf / 2; dcb.ByteSize = 8; //8 Datenbits dcb.Parity = 0; //keine Paritaet dcb.StopBits = 0; //1 Stop Bit if (!SetCommState(hnd,&dcb)) { CloseHandle(hnd); hnd = INVALID_HANDLE_VALUE; GetLastError(); return(DLL_COMMSETERROR); } } // if (hnd != INVALID_HANDLE_VALUE) { GetCommTimeouts(hnd,&cto); cto.ReadIntervalTimeout = 0; cto.ReadTotalTimeoutMultiplier = 0; cto.ReadTotalTimeoutConstant = 0; cto.WriteTotalTimeoutMultiplier = 0; cto.WriteTotalTimeoutConstant = 0; SetCommTimeouts(hnd,&cto); } if (hnd != INVALID_HANDLE_VALUE) { if (!SetupComm(hnd,rec_buf,trm_buf)) { CloseHandle(hnd); hnd = INVALID_HANDLE_VALUE; GetLastError(); return(DLL_COMMSETUPERROR); } } else return(DLL_COMMERROR); // return(hnd); } //--------------------------------------------------------------------------- bool __fastcall TAppForm::GetItemStatus(void *hnd,int dev_adr,LPItemStatus Stat,bool mode) { // hier stehen noch allg. Variablen Deklarationen usw. tx_buffer[0] = (unsigned char)(RID); tx_buffer[1] = (unsigned char)(TID); tx_buffer[2] = 0x05; tx_buffer[3] = (unsigned char)(GETITEMSTATUS); tx_buffer[4] = LOBYTE((unsigned char)(RID^TID^GETITEMSTATUS^0x05)); // // Telegramm senden und warten bis alle Bytes übertragen sind // success = CommTransmit(hnd,&tx_buffer[0],(int)(tx_buffer[2])); //dann Telegramm senden // // jetzt Ergebnis einlesen // (ACK,TID,RID,TelLen,Data0..Data8,Checksum) // if (success == true) { success = CommReceive(hnd,&rx_buffer[0],14); //jetzt sukzessive Bytes empfangen } // // jetzt Telegramm auswerten // if (success == true) { } } //--------------------------------------------------------------------------- bool __fastcall::CommTransmit(void *hnd,char *buffer,DWORD buf_len) { bool success; unsigned long Bytes_written,c_error; COMSTAT Com_stat; CommClearBuffer(hnd,PURGE_TXCLEAR); //TX-BUFFER löschen CommClearBuffer(hnd,PURGE_RXCLEAR); //RX-BUFFER löschen // success = ClearCommError(hnd,&c_error,&Com_stat); //alle Fehler löschen if (success == false) GetLastError(); success = WriteFile(hnd,&buffer[0],buf_len,&Bytes_written,NULL); if (success == false) GetLastError(); // return(success); } //--------------------------------------------------------------------------- bool __fastcall::CommReceive(void *hnd,char *buffer,int rx_count) { bool success; unsigned long Bytes_read,rx_anz,c_error; unsigned long t_diff; COMSTAT Com_stat; success = ClearCommError(hnd,&c_error,&Com_stat); if (success == false) GetLastError(); // success = true; t_diff = timeGetTime(); do { rx_anz = read_buffer(hnd); if ((timeGetTime() - t_diff) > TIME_OUT) { success = false; break; } } while ((int)(rx_anz) < rx_count); // if (success == true) { success = ReadFile(hnd,&buffer[0],rx_count,&Bytes_read,NULL); if (success == false) GetLastError(); else buffer[Bytes_read] = '\0'; } return(success); } //--------------------------------------------------------------------------- unsigned long __fastcall::read_buffer(void *hnd) { unsigned long in,rx_buf_count; COMSTAT CommStat; if (ClearCommError(hnd,&in,&CommStat)) rx_buf_count = CommStat.cbInQue; else { rx_buf_count = 0; GetLastError(); } return (rx_buf_count); } //---------------------------------------------------------------------------