Serielle Datenausgabe mit C



  • Hallo!

    Ich suche seit einiger Zeit verzweifelt ein Codebeispiel oder eine Erklärung wie man mit C Zeichenketten über die Serielle Schnittstelle des PCs an die Außenwelt schicken kann. 😕
    Ich muss dazu Einstellungen wie Baudrate, Anzahl der Daten- & Stopbits sowie die Parität vornehmen können.
    Aus einigem herumsuchen bin ich auf die WinAPI gestoßen, aber ich hab leider keine Tutorien zur Datenausgabe für C gefunden 😞
    Eventuell hat jemand von euch Ahnung bzw. Erfahrung mit diesem Thema uns kann mir helfen?

    danke auf alle Fälle schon mal im Voraus! 🙂



  • ANSI C kennt keinen seriellen Port und demzufolge auch keine Kommunikation damit.
    Wenn du schon WINAPI Funktionen gefunden hast, warum nimmst du diese nicht? Das sind üblicherweise C Funktionen, wenn auch überwiegend nicht striktes ANSI C.



  • Da bist Du hier falsch.
    Guckst Du Bei anderer Ecke RS232 Basics. 😃
    Da sind auch weiterführende Links zu finden. Und wenn's für Win sein soll, im MSDN ist auch ein lesenswerter Abschnitt.



  • Hier ein CPP-Code für Win

    /*
     * Version 1.0
     * <hdieterm yahoo de>
     */
    
    /*
     * list of return codes:
     * 00 Success
     * 01 User Interrupt (Esc-Key)
     * 02 Timeout
     * 03 COM not open
     * 04 COM is allready open
     * 05 COM wrong interrupt
     * 06 COM wrong baud rate
     * 07 COM no char received
     * 08 COM WriteFile error
     * 09 COM ReadFile error
     * 10 COM CreateFileA error
     * 11 COM GetCommState error
     * 12 COM SetCommState error
     * 13 COM GetCommTimeouts error
     * 14 COM SetCommTimeouts error
     * 15 COM PurgeComm error
     * 16 COM PeekNamedPipe error
     * 20 COM unknown error message
     */
    
    #include "stdafx.h"
    #include "rs232win.h"
    #include "time.h"
    
    static HANDLE hCom = INVALID_HANDLE_VALUE;
    static int port_open = 0;  /* 0 = closed  1 = open */
    static int comerror = 0;
    static double timeout_default;
    
    const char *comerror_description(int ret)
    {
    	static char tmp[64];
    
    	switch (ret) {
    		case 0:  sprintf(tmp,"00 Success"); break;
    		case 1:  sprintf(tmp,"01 User Interrupt (Esc-Key)"); break;
    		case 2:  sprintf(tmp,"02 Timeout"); break;
    		case 3:  sprintf(tmp,"03 COM not open"); break;
    		case 4:  sprintf(tmp,"04 COM is allready open"); break;
    		case 5:  sprintf(tmp,"05 COM wrong interrupt"); break;
    		case 6:  sprintf(tmp,"06 COM wrong baud rate"); break;
    		case 7:  sprintf(tmp,"07 COM no char received"); break;
    		case 8:  sprintf(tmp,"08 COM WriteFile error"); break;
    		case 9:  sprintf(tmp,"09 COM ReadFile error"); break;
    		case 10: sprintf(tmp,"10 COM CreateFileA error"); break;
    		case 11: sprintf(tmp,"11 COM GetCommState error"); break;
    		case 12: sprintf(tmp,"12 COM SetCommState error"); break;
    		case 13: sprintf(tmp,"13 COM GetCommTimeouts error"); break;
    		case 14: sprintf(tmp,"14 COM SetCommTimeouts error"); break;
        case 15: sprintf(tmp,"15 COM PurgeComm error"); break;
        case 16: sprintf(tmp,"16 COM PeekNamedPipe error"); break;
    		default: sprintf(tmp,"20 COM unknown error message"); break;
    	}
    
    	return &tmp[0];
    }
    
    int comsetreadtimeout(double timeout)
    {
      COMMTIMEOUTS cto;
      memset(&cto, 0, sizeof(cto));
      if (!GetCommTimeouts(hCom, &cto)) {
        comerror = 13;
    		goto out;
      }
    
      cto.ReadTotalTimeoutConstant = (unsigned int)(timeout * 1000);
      cto.WriteTotalTimeoutConstant = 0;
    
      if (!SetCommTimeouts(hCom, &cto)) {
        comerror = 14;
    		goto out;
      }
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    
    int cominit(char *port, int speed)
    {
      DCB dcb;
    
    	if (port_open == 1) {
    	  comerror = 4;
    		goto out;
    	}
    
      hCom = CreateFileA(port, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    
      if (hCom == INVALID_HANDLE_VALUE) {
    	  comerror = 10;
    		goto out;
      }
    
      if (!GetCommState(hCom, &dcb)) {
    	  comerror = 11;
    		goto out;
      }
    
      /* Fill in DCB: (speed) bps, 8 data bits, no parity, and 1 stop bit. */
      dcb.BaudRate = speed;           /* set the baud rate */
      dcb.ByteSize = 8;               /* data size, xmit, and rcv */
      dcb.Parity = NOPARITY;          /* no parity bit */
      dcb.StopBits = ONESTOPBIT;      /* one stop bit */
    
      if (!SetCommState(hCom, &dcb)) {
        comerror = 12;
    		goto out;
      }
    
    	timeout_default = 0.8;  /* 0.8 seconds */
    	comerror = comsetreadtimeout(timeout_default);
      if (comerror) goto out;
    
    	port_open = 1;
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comclose(void)
    {
    	if (port_open == 0) {
        comerror = 3;
    		goto out;
    	}
    
      CloseHandle(hCom);
      hCom = INVALID_HANDLE_VALUE;
    	port_open = 0;
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comflush()
    {
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
      if (!PurgeComm(hCom,PURGE_TXCLEAR)) {
        comerror = 15;
    		goto out;	 	  
    	}
    
      if (!PurgeComm(hCom,PURGE_RXCLEAR)) {
    	  comerror = 15;
    		goto out;	 
    	}
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comreadline(char tmp[], unsigned int maxsize)
    {
    	unsigned long ret_bytes;
    	unsigned int i = 0;
    	unsigned char b;
    	int c;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	for (;;) {
    	  ret_bytes = 0;
    		if (!ReadFile(hCom, &b, 1, &ret_bytes, NULL)) {
          comerror = 9;
    		  goto out;	
    		}
    
    		if (ret_bytes == 1) {
    		  if (b == 13) break;
    			tmp[i++] = b;
    
    			if (i == maxsize) break;
    		}
    
        if (_kbhit()) {
    			c = _getch();
    			if (c == 27) { 
    				comerror = 1;
    				goto out;
    			}
    		}
    	}
    
    	tmp[i] = 0;
    	comerror = 0;
    
    	return comerror;
    
    out:
    	tmp[0] = 0;
    	return comerror;
    }
    
    int comwriteline(const char tmp[])
    {
    	unsigned long ret_bytes = 0;
    	unsigned long i = 0;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	comerror = comwritebyte(13);
    	if (comerror) goto out;
    	comerror = comwritebyte(10);
    	if (comerror) goto out;
    
    	while (tmp[i] != 0) {
    		i++;
    	}
    
      ret_bytes = 0;
      if (!WriteFile(hCom,tmp,i,&ret_bytes,NULL)) {
        comerror = 8;
    		goto out;
    	}
    
    	if (ret_bytes != i) {
    	  comerror = 8;
    		goto out;
    	}
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comread(unsigned char tmp[], unsigned int nsize)
    {
    	unsigned long ret_bytes;
    	unsigned int i = 0;
    	int c;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	for (;;) {
    	  ret_bytes = 0;
    		if (!ReadFile(hCom, &tmp[i], nsize - i, &ret_bytes, NULL)) {
    			comerror = 9;
    			goto out;
    		}
    
    		i += ret_bytes;
    
    		if (i == nsize) break;
    
    		if (_kbhit()) {
    			c = _getch();
    			if (c == 27) { 
    				comerror = 1;
    				goto out;
    			}
    		}
    	} 
    
      comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comread_timeout(unsigned char tmp[], unsigned int nsize, int timeout)
    {
    	unsigned long ret_bytes;
    	unsigned int i = 0;
    	int c;
    
    	clock_t timestart, timeend;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	timestart = clock();
    
    	for (;;) {
    	  ret_bytes = 0;
    		if (!ReadFile(hCom, &tmp[i], nsize - i, &ret_bytes, NULL)) {
          comerror = 9;
    		  goto out;
    		}
    
    		i += ret_bytes;
    
    		if (i == nsize) break;
    
    		timeend = clock();
    
    		if (((timeend - timestart) / CLK_TCK) > 1) {
    			timestart = clock();
    			timeout--;
    		}
    
    		if (timeout == 0) {
          comerror = 2;
    		  goto out;		  
    		}
    
    		if (_kbhit()) {
    			c = _getch();
    			if (c == 27) { 
    				comerror = 1;
    				goto out;
    			}
    		}
    	}
    
      comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comreadbyte(unsigned char *tmp)
    {
    	unsigned long ret_bytes;
    	int c;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	for (;;) {
    	  ret_bytes = 0;
    	  if (!ReadFile(hCom, tmp, 1, &ret_bytes, NULL)) {
          comerror = 9;
    		  goto out;	
    		}
    
    		if (ret_bytes == 1) break;
    
    		if (_kbhit()) {
    			c = _getch();
    			if (c == 27) { 
    				comerror = 1;
    				goto out;
    			}
    		}
    	}
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comreadbyte_timeout(unsigned char *tmp, int timeout)
    {
    	unsigned long ret_bytes;
    	int c;
    	clock_t timestart, timeend;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	timestart = clock();
    
    	for (;;) {
    	  ret_bytes = 0;
    	  if (!ReadFile(hCom, tmp, 1, &ret_bytes, NULL)) {
          comerror = 9;
    		  goto out;
    		}
    
    		if (ret_bytes == 1) break;
    
    		timeend = clock();
    
    		if (((timeend - timestart) / CLK_TCK) > 1) {
    			timestart = clock();
    			timeout--;
    		}
    
    		if (timeout == 0) {
          comerror = 2;
    		  goto out;		  
    		}
    
    		if (_kbhit()) {
    			c = _getch();
    			if (c == 27) { 
    				comerror = 1;
    				goto out;
    			}
    		}
    	}
    
      comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comreadbyte_nowait(unsigned char *tmp)
    {
    	unsigned long ret_bytes;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	ret_bytes = 0;
    	if (!ReadFile(hCom, tmp, 1, &ret_bytes, NULL)) {
        comerror = 9;
    		goto out;	
    	}
    
      if (ret_bytes == 0) {
        comerror = 9;
    		goto out;	
    	}	
    
      comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comwrite(const unsigned char tmp[], unsigned int nsize)
    {
    	unsigned long ret_bytes;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	ret_bytes = 0;
      if (!WriteFile(hCom,tmp,nsize,&ret_bytes,NULL)) {
        comerror = 8;
    		goto out;	 
    	}
    
    	if (ret_bytes != nsize) {
        comerror = 8;
    		goto out;	 
    	}
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    
    int comwritebyte(const unsigned char tmp)
    {
    	unsigned long ret_bytes;
    
    	if (port_open == 0) {
        comerror = 3;
    		goto out;	  
    	}
    
    	ret_bytes = 0;
      if (!WriteFile(hCom,&tmp,1,&ret_bytes,NULL)) {
        comerror = 8;
    		goto out;
    	}
    
    	if (ret_bytes != 1) {
        comerror = 8;
    		goto out;
    	}
    
    	comerror = 0;
    
    out:
    	return comerror;
    }
    

    und die header datei:

    /*
     * Version 1.0
     * <hdieterm yahoo de>
     */
    
    #include "windows.h"
    #include "stdio.h"
    #include "conio.h"
    
    #ifndef _RS232WIN_H_
    #define _RS232WIN_H_ 1
    
    /*
     * list of return codes:
     * 00 Success
     * 01 User Interrupt (Esc-Key)
     * 02 Timeout
     * 03 COM not open
     * 04 COM is allready open
     * 05 COM wrong interrupt
     * 06 COM wrong baud rate
     * 07 COM no char received
     * 08 COM WriteFile error
     * 09 COM ReadFile error
     * 10 COM CreateFileA error
     * 11 COM GetCommState error
     * 12 COM SetCommState error
     * 13 COM GetCommTimeouts error
     * 14 COM SetCommTimeouts error
     * 15 COM PurgeComm error
     * 16 COM PeekNamedPipe error
     * 20 COM unknown error message
     */
    
    const char *comerror_description(int ret);
    
    int cominit(char *port, int speed);
    int comclose(void);
    int comflush(void);
    
    int comreadline(char tmp[], unsigned int maxsize);
    int comwriteline(const char tmp[]);
    
    int comread(unsigned char tmp[], unsigned int nsize);
    int comread_timeout(unsigned char tmp[], unsigned int nsize, int timeout);
    
    int comwrite(const unsigned char tmp[], unsigned int nsize);
    
    int comreadbyte(unsigned char *tmp);
    int comwritebyte(const unsigned char tmp);
    
    int comreadbyte_nowait(unsigned char *tmp);
    int comreadbyte_timeout(unsigned char *tmp, int timeout);
    
    #endif
    

    Ich habe auch eine Version für DOS (C-Code)
    Bei Bedarf werde ich diesen veröffentlichen


Anmelden zum Antworten