Click here to Skip to main content
15,902,939 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Problems synchronizing multiple threads?? Pin
John M. Drescher27-Jun-06 10:17
John M. Drescher27-Jun-06 10:17 
AnswerRe: Problems synchronizing multiple threads?? Pin
valikac27-Jun-06 9:12
valikac27-Jun-06 9:12 
AnswerRe: Problems synchronizing multiple threads?? Pin
Michael Dunn27-Jun-06 9:47
sitebuilderMichael Dunn27-Jun-06 9:47 
GeneralRe: Problems synchronizing multiple threads?? Pin
pavanbabut29-Jun-06 12:11
pavanbabut29-Jun-06 12:11 
Questionbug of mouse_event() Pin
V_shr27-Jun-06 7:28
V_shr27-Jun-06 7:28 
QuestionCOM Port Acesss in VC++ Pin
tanksali27-Jun-06 6:17
tanksali27-Jun-06 6:17 
AnswerRe: COM Port Acesss in VC++ Pin
David Crow27-Jun-06 6:41
David Crow27-Jun-06 6:41 
AnswerRe: COM Port Acesss in VC++ Pin
mikeorama1234527-Jun-06 8:43
mikeorama1234527-Jun-06 8:43 
#ifndef _COMPORT_H
#define _COMPORT_H
#include <afxwin.h>
//////////////////////////////////////////////////////////////////////
class Comport
{
private:
HANDLE hdev;
int slen;
int rxp;
char sbuf [1024];
public:
int Com, Baud;
Comport ();
~ Comport ();
int Open ( int com, int baud );
void Close ();
void SetBaud ( int baud );
void CommState ();
void CommTO ();
int read ( char * s, int size );
int write ( char * s, int size );
int putc ( char c );
int puts ( char * s );
int getc ();
int Read ();
int SendFile ( const char * );
};
//////////////////////////////////////////////////////////////////////
#endif

#include "timer.h"
#include "comport.h"
#include <stdio.h>
/**************************************************************/
Comport :: Comport ()
{
hdev = 0;
Com =
Baud = 0;
}
/**************************************************************/
Comport :: ~ Comport ()
{
Close ();
}
/**************************************************************/
void Comport :: Close ()
{
if ( hdev && CloseHandle ( hdev ) == FALSE )
{
}
hdev = 0;
Com =
Baud = 0;
}
/**************************************************************/
Comport :: Open ( int com, int baud )
{
if ( Com && hdev ) Close ();

if (!( Com = com )) return 0;

Baud = baud;

char s [255];
sprintf ( s, "\\\\.\\COM%d", Com );

hdev = CreateFile ( s, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );

int error = GetLastError();

// hdev = (void*)HFILE_ERROR;
if ( (int) hdev != HFILE_ERROR )
{
CommState ();
CommTO ();
}
else Com = 0;

return Com;
}
/**************************************************************/
void Comport :: SetBaud ( int baud )
{
if ( Com )
{
Baud = baud;
CommState ();
}
}
/**************************************************************/
void Comport :: CommState ()
{
DCB dcb;
GetCommState ( hdev, & dcb );

dcb .BaudRate = Baud;
dcb .ByteSize = 8;
dcb .StopBits = ONESTOPBIT;
dcb .Parity = NOPARITY;

dcb .fBinary = 0;

dcb .fParity = 0;
dcb .fOutxCtsFlow = 0;
dcb .fOutxDsrFlow = 0;
dcb .fDtrControl = DTR_CONTROL_DISABLE;
dcb .fDsrSensitivity = 0;
// dcb .fTXContinueOnXoff =
dcb .fOutX = 0;
dcb .fInX = 0;
dcb .fErrorChar = 0;
dcb .fNull = 0;
dcb .fRtsControl = RTS_CONTROL_DISABLE;
dcb .fAbortOnError = 0;

SetCommState ( hdev, & dcb );
}
/**************************************************************/
void Comport :: CommTO ()
{
COMMTIMEOUTS cto;
cto .ReadIntervalTimeout = MAXDWORD;
cto .ReadTotalTimeoutMultiplier = 0;
cto .ReadTotalTimeoutConstant = 0;
cto .WriteTotalTimeoutMultiplier = 0;
cto .WriteTotalTimeoutConstant = 0;

SetCommTimeouts ( hdev, & cto );
}
/**************************************************************/
int Comport :: read ( char * b, int size )
{
DWORD n = 0; //bytesRead
ReadFile ( hdev, b, size, & n, 0 );
return n;
}
/**************************************************************/
int Comport :: write ( char * b, int size )
{
DWORD n = 0; //bytesWritten
WriteFile ( hdev, b, size, & n, 0 );
return n;
}
/**************************************************************/
int Comport :: putc ( char c )
{
return write ( & c, 1 );
}
/**************************************************************/
int Comport :: puts ( char * s )
{
return write ( s, strlen (s) );
}
/**************************************************************/
int Comport :: SendFile ( const char * path )
{
int n = 0;
if (Com)
if ( FILE * fp = fopen ( path, "rt" ) )
{
Ticker t(50);
char s [100] = {0};
while ( fgets ( s, 100, fp ) )
{
while (!t .expire () );
n += write ( s, strlen (s) );
}
}
return n;
}
/**************************************************************/
int Comport :: Read ()
{
rxp = 0;
DWORD size = 0;
if (Com)
ReadFile ( hdev, sbuf, 1024, & size, 0 );
return slen = size;
}
/**************************************************************/
int Comport :: getc ()
{
coerce ( 0, 1023, rxp );
coerce ( 0, 1023, slen );

return ( rxp < slen ) ? sbuf [ rxp ++ ] : 0 ;
}
/**************************************************************/
/*int Comport :: getc ()
{
char inbuf [25] = {0};
read ( inbuf, 1 );
return inbuf [0];
}
/**************************************************************/





Just livin a dream.. dont wake me!
AnswerRe: COM Port Acesss in VC++ Pin
Hamid_RT27-Jun-06 19:17
Hamid_RT27-Jun-06 19:17 
QuestionMultiple string tables in exe Pin
Andre xxxxxxx27-Jun-06 6:06
Andre xxxxxxx27-Jun-06 6:06 
AnswerRe: Multiple string tables in exe Pin
Joe Woodbury27-Jun-06 6:15
professionalJoe Woodbury27-Jun-06 6:15 
GeneralRe: Multiple string tables in exe Pin
Andre xxxxxxx27-Jun-06 6:22
Andre xxxxxxx27-Jun-06 6:22 
GeneralRe: Multiple string tables in exe Pin
Joe Woodbury27-Jun-06 6:29
professionalJoe Woodbury27-Jun-06 6:29 
GeneralRe: Multiple string tables in exe Pin
Andre xxxxxxx27-Jun-06 6:35
Andre xxxxxxx27-Jun-06 6:35 
GeneralRe: Multiple string tables in exe Pin
Joe Woodbury27-Jun-06 6:47
professionalJoe Woodbury27-Jun-06 6:47 
GeneralRe: Multiple string tables in exe Pin
Andre xxxxxxx27-Jun-06 8:30
Andre xxxxxxx27-Jun-06 8:30 
AnswerRe: Multiple string tables in exe Pin
Zac Howland27-Jun-06 7:21
Zac Howland27-Jun-06 7:21 
GeneralRe: Multiple string tables in exe Pin
Andre xxxxxxx27-Jun-06 8:29
Andre xxxxxxx27-Jun-06 8:29 
GeneralRe: Multiple string tables in exe Pin
Zac Howland27-Jun-06 9:20
Zac Howland27-Jun-06 9:20 
Questionstring tables Pin
big_denny_20027-Jun-06 5:43
big_denny_20027-Jun-06 5:43 
AnswerRe: string tables Pin
Neil Van Eps27-Jun-06 5:48
Neil Van Eps27-Jun-06 5:48 
AnswerRe: string tables Pin
sach!!27-Jun-06 18:55
sach!!27-Jun-06 18:55 
AnswerRe: string tables Pin
Hamid_RT27-Jun-06 19:04
Hamid_RT27-Jun-06 19:04 
Questionwhat is a source control? [modified] Pin
big_denny_20027-Jun-06 5:34
big_denny_20027-Jun-06 5:34 
AnswerRe: what is a source control? Pin
Chris Losinger27-Jun-06 5:52
professionalChris Losinger27-Jun-06 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.