View Single Post
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