Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 20th, 2005, 12:09 PM   #1
Automatik
Newbie
 
Join Date: May 2005
Posts: 16
Rep Power: 0 Automatik is on a distinguished road
Speed Up Serial Windows API

I got serial communication to work without using MSComm. The problem is that now the program is so slow and takes up all of the computers CPU resources. What can I do to fix this. My only thought would be to use threads instead...
Automatik is offline   Reply With Quote
Old May 20th, 2005, 3:01 PM   #2
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
Post the code here, using CODE tags.
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old May 20th, 2005, 3:10 PM   #3
Automatik
Newbie
 
Join Date: May 2005
Posts: 16
Rep Power: 0 Automatik is on a distinguished road
#include <vcl.h>
#include "Unit2.h"
#include "Unit3.h"
#include "serial.h"
TRead *ReadThread;
TWrite *WriteThread;

Serial::Serial ( void ) {
}

bool Serial::Open ( AnsiString portName, int speed ) {
        HANDLE hdlPort;

        hdlPort = CreateFile ( portName.c_str(), GENERIC_WRITE | GENERIC_READ,
                               0, NULL, OPEN_EXISTING, 0, NULL );

        if ( hdlPort == INVALID_HANDLE_VALUE ) {
          return ( FALSE );
        }

        DCB dcb;
        dcb.DCBlength = sizeof(DCB);
        GetCommState ( hdlPort, &dcb );
        switch ( speed ) {
          case 115200: {
            dcb.BaudRate = CBR_115200;
            break;
          }
          case 57600: {
            dcb.BaudRate = CBR_57600;
            break;
          }
          case 38400: {
            dcb.BaudRate = CBR_38400;
            break;
          }
          case 19200: {
            dcb.BaudRate = CBR_19200;
            break;
          }
          case 9600: {
            dcb.BaudRate = CBR_9600;
            break;
          }
          default: {
            return ( FALSE );
          }
        }
        dcb.Parity = NOPARITY;
        dcb.ByteSize = 8;
        dcb.StopBits = ONESTOPBIT;
        dcb.fDsrSensitivity = FALSE;
        dcb.fOutxCtsFlow = FALSE;
        dcb.fOutxDsrFlow = FALSE;
        dcb.fOutX = FALSE;
        dcb.fInX = FALSE;
        dcb.fBinary = 1;

        SetCommState ( hdlPort, &dcb );

        hdlCom = hdlPort;

        if ( !SetTimeouts ()) {
          return ( FALSE );
        }

        return ( TRUE );
}

bool Serial::Close ( void ) {
  PurgeComm(hdlCom,PURGE_RXABORT);
  if ( hdlCom != INVALID_HANDLE_VALUE ) {
    if ( !CloseHandle ( hdlCom )) {
      return ( FALSE );
    }

    hdlCom = INVALID_HANDLE_VALUE;
  }

  return ( TRUE );
}

bool Serial::WriteChar ( char *byte ) {
  DWORD bytesWritten = 0;
  WriteFile ( hdlCom, byte, 1, &bytesWritten, NULL );
  if( !bytesWritten ) {
    return ( FALSE );
  }

  return ( TRUE );
}

bool Serial::ReadChar ( char &out_byte ) {

  DWORD bytesRead = 0;
  ReadFile ( hdlCom, &out_byte, 1, &bytesRead, NULL );
  if( !bytesRead ) {
        return ( FALSE );
  }
  return ( TRUE );
  //ReadThread = new TRead(false);
  //return (TRUE);
}

bool Serial::SetTimeouts ( void ) {
  COMMTIMEOUTS Timeouts;
  Timeouts.ReadIntervalTimeout = MAXDWORD;
  Timeouts.ReadTotalTimeoutMultiplier = 0;
  Timeouts.ReadTotalTimeoutConstant = 0;
  Timeouts.WriteTotalTimeoutMultiplier = 0;
  Timeouts.WriteTotalTimeoutConstant = 0;

  if ( !SetCommTimeouts ( hdlCom, &Timeouts )) {
    return ( FALSE );
  }

  return ( TRUE );
}

bool Serial::Write ( char *output, int size  ) {
        DWORD bytesWritten = 0;
        WriteFile ( hdlCom,output,size+1,&bytesWritten,NULL );

        if( !bytesWritten ) {
                return ( FALSE );
        }
        return ( TRUE );
        //WriteThread = new TWrite(false);
        //return ( TRUE );
}

bool Serial::IsOpen ( ) {
     if ( hdlCom == INVALID_HANDLE_VALUE ) {
          return ( FALSE );
     }
     return ( TRUE );
}
Automatik is offline   Reply With Quote
Old May 20th, 2005, 3:19 PM   #4
Automatik
Newbie
 
Join Date: May 2005
Posts: 16
Rep Power: 0 Automatik is on a distinguished road
#include <vcl.h>
#include "Unit2.h"
#include "Unit3.h"
#include "serial.h"
TRead *ReadThread;
TWrite *WriteThread;

Serial::Serial ( void ) {
}

bool Serial::Open ( AnsiString portName, int speed ) {
        HANDLE hdlPort;

        hdlPort = CreateFile ( portName.c_str(), GENERIC_WRITE | GENERIC_READ,
                               0, NULL, OPEN_EXISTING, 0, NULL );

        if ( hdlPort == INVALID_HANDLE_VALUE ) {
          return ( FALSE );
        }

        DCB dcb;
        dcb.DCBlength = sizeof(DCB);
        GetCommState ( hdlPort, &dcb );
        switch ( speed ) {
          case 115200: {
            dcb.BaudRate = CBR_115200;
            break;
          }
          case 57600: {
            dcb.BaudRate = CBR_57600;
            break;
          }
          case 38400: {
            dcb.BaudRate = CBR_38400;
            break;
          }
          case 19200: {
            dcb.BaudRate = CBR_19200;
            break;
          }
          case 9600: {
            dcb.BaudRate = CBR_9600;
            break;
          }
          default: {
            return ( FALSE );
          }
        }
        dcb.Parity = NOPARITY;
        dcb.ByteSize = 8;
        dcb.StopBits = ONESTOPBIT;
        dcb.fDsrSensitivity = FALSE;
        dcb.fOutxCtsFlow = FALSE;
        dcb.fOutxDsrFlow = FALSE;
        dcb.fOutX = FALSE;
        dcb.fInX = FALSE;
        dcb.fBinary = 1;

        SetCommState ( hdlPort, &dcb );

        hdlCom = hdlPort;

        if ( !SetTimeouts ()) {
          return ( FALSE );
        }

        return ( TRUE );
}

bool Serial::Close ( void ) {
  PurgeComm(hdlCom,PURGE_RXABORT);
  if ( hdlCom != INVALID_HANDLE_VALUE ) {
    if ( !CloseHandle ( hdlCom )) {
      return ( FALSE );
    }

    hdlCom = INVALID_HANDLE_VALUE;
  }

  return ( TRUE );
}

bool Serial::WriteChar ( char *byte ) {
  DWORD bytesWritten = 0;
  WriteFile ( hdlCom, byte, 1, &bytesWritten, NULL );
  if( !bytesWritten ) {
    return ( FALSE );
  }

  return ( TRUE );
}

bool Serial::ReadChar ( char &out_byte ) {

  DWORD bytesRead = 0;
  ReadFile ( hdlCom, &out_byte, 1, &bytesRead, NULL );
  if( !bytesRead ) {
        return ( FALSE );
  }
  return ( TRUE );
  //ReadThread = new TRead(false);
  //return (TRUE);
}

bool Serial::SetTimeouts ( void ) {
  COMMTIMEOUTS Timeouts;
  Timeouts.ReadIntervalTimeout = MAXDWORD;
  Timeouts.ReadTotalTimeoutMultiplier = 0;
  Timeouts.ReadTotalTimeoutConstant = 0;
  Timeouts.WriteTotalTimeoutMultiplier = 0;
  Timeouts.WriteTotalTimeoutConstant = 0;

  if ( !SetCommTimeouts ( hdlCom, &Timeouts )) {
    return ( FALSE );
  }

  return ( TRUE );
}

bool Serial::Write ( char *output, int size  ) {
        DWORD bytesWritten = 0;
        WriteFile ( hdlCom,output,size+1,&bytesWritten,NULL );

        if( !bytesWritten ) {
                return ( FALSE );
        }
        return ( TRUE );
        //WriteThread = new TWrite(false);
        //return ( TRUE );
}

bool Serial::IsOpen ( ) {
     if ( hdlCom == INVALID_HANDLE_VALUE ) {
          return ( FALSE );
     }
     return ( TRUE );
}
Automatik is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC