![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Posts: 16
Rep Power: 0
![]() |
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...
|
|
|
|
|
|
#2 |
|
Professional Programmer
|
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;} |
|
|
|
|
|
#3 |
|
Newbie
Join Date: May 2005
Posts: 16
Rep Power: 0
![]() |
#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 );
} |
|
|
|
|
|
#4 |
|
Newbie
Join Date: May 2005
Posts: 16
Rep Power: 0
![]() |
#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 );
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|