Source file: /~heha/mb-iwp/Bergwerk/dumpcom.zip/src/dumpcom.cpp

#include <windows.h>
#include <shlwapi.h>
#include <stdio.h>
// Einfaches Konsolenprogramm welches die serielle Schnittstelle abfragt
// und Daten als Hexbytes ausgibt.
// Bei zeitlichen Lücken (soweit feststellbar) neue Zeile
// Beenden des Programms mit ^C

// Zeit eines Bytes in Bits
int bytebits(const DCB&dcb) {
 return 1		// Startbit
	+dcb.ByteSize
	+(dcb.Parity!=NOPARITY?1:0)
	+(dcb.StopBits!=ONESTOPBIT?2:1);
}

EXTERN_C _CRTIMP int _cdecl __getmainargs(int&,char**&,char**&,int,int&);

EXTERN_C void mainCRTStartup() {
 int argc,newmode;
 char**argv,**envp;
 __getmainargs(argc,argv,envp,false,newmode);
// argv[1] = COM-Port-Name,
// argv[2] = Einstellungen in der Form 19200,n,8,1 (Google „DCB“)
 if (argc<2) {	// jaja, Umlaute kommen falsch zur Standard-Konsole mit CP850
  fprintf(stderr,"Schnittstelle und Schnittstellenparameter müssen angegeben werden!\n");
  exit(1);
 }
 HANDLE h=CreateFile(argv[1],GENERIC_READ,0,
 0,OPEN_EXISTING,0,0);
 if (h==INVALID_HANDLE_VALUE) {
  fprintf(stderr,"Konnte %s nicht öffnen!\n",argv[1]);
  exit(1);
 }
 DCB dcb;
 GetCommState(h,&dcb);
 COMMTIMEOUTS to;
 GetCommTimeouts(h,&to);
 if (argc>2) {
  if (!BuildCommDCBAndTimeouts(argv[2],&dcb,&to)) {
   fprintf(stderr,"Konnte Schnittstellenparameter %s nicht parsen!\n",argv[2]);
   exit(1);
  }
 }
 SetCommState(h,&dcb);
 to.ReadIntervalTimeout=1;
 SetCommTimeouts(h,&to);
// LARGE_INTEGER qpf;
// QueryPerformanceFrequency(&qpf);
// LARGE_INTEGER t0;
// Einheitenkontrolle: tick/s * bit/byte ÷ bit/s = tick/byte
// int tickperbyte=MulDiv(qpf.LowPart,bytebits(dcb),dcb.BaudRate);
// QueryPerformanceCounter(&t0);
 for(;;) {
  BYTE a[32];
  DWORD br;
  if (!ReadFile(h,a,sizeof a,&br,0)) break;
  for (DWORD i=0; i<br; i++) {
   if (2<=i && i<br-2) {
    unsigned v=a[i]+(a[i+1]<<8); ++i;
    printf("%4u ",v);
   }else{
    printf("%02X%c",a[i],i==br-1?'\n':' ');
   }
  }
 }
 printf("\n");
 CloseHandle(h);
}
Detected encoding: ANSI (CP1252)4
Wrong umlauts? - Assume file is ANSI (CP1252) encoded