Redirect serial port source code..

Hi...
I need a redirect serial port source code in c++ (visual c++ or borland c++/ borland c++ builder)..

thanks for all
LVL 3
sennaspyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexVirochovskyCommented:
Full example (a bit large) of using serial port
http://www.codeguru.com/misc/serialport.shtml
Article about all aspects of serail port programming
http://msdn.microsoft.com/library/sdkdoc/winbase/commun_4m7n.htm
Not so large and good example of
programm.
http://home.das-netz.de/pat/serial.htm
Alex
BTW: i send this as "answer", but may be
i again don't understand you:
What you means: "redirect"?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sennaspyAuthor Commented:
Hi...
"Redirect" is the transfer all data in serial port output to any file...
I have any examples for MS-DOS, but I need for windows...
Looking in altavista by "com2file"...
thanks
0
sennaspyAuthor Commented:
Hi...
"Redirect" is the transfer all data in serial port output to any file...
I have any examples for MS-DOS, but I need for windows...
Looking in altavista by "com2file"...
thanks
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

AlexVirochovskyCommented:
Aha, next is small example of programm, that get every delta_time
data from serial port:

#include  <windows.h>
#include  <stdio.h>

int
main(int argc,char *argv[])
{
  HANDLE  *cfd;
  int     errno;
  int     i;
  int     nBytesRead;
  int     nBytesToRead;
  char    inBuffer[1024];
  int     nBytesWritten;
  int     nBytesToWrite;
  char    outBuffer[1024];

  COMMTIMEOUTS ct;
  DCB          dcb;

  if ( (cfd=CreateFile("COM1:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL))==INVALID_HANDLE_VALUE ) {
    errno = GetLastError();
    printf("CreatFile failed. Errno=%d\n",errno);
    return -1;
  }
  printf("CreatFile OK\n");
    
  if ( !SetupComm(cfd,1024,1024)!=0 ) {
    errno = GetLastError();
    printf("SetupComm failed. Errno=%d\n",errno);
    return -1;
  }
  printf("SetupComm OK\n");

  if ( !GetCommState(cfd,&dcb) ) {
    errno = GetLastError();
    printf("GetCommState failed. Errno=%d\n",errno);
    return -1;
  }
  printf("GetCommState OK\n");
  printf("DCBlength=0x%04x\n",dcb.DCBlength);
  printf("BaudRate=%d\n",dcb.BaudRate);
  printf("fBinary=0x%04x\n",dcb.fBinary);
  printf("fParity=0x%04x\n",dcb.fParity);
  printf("fOutxCtsFlow=0x%04x\n",dcb.fOutxCtsFlow);
  printf("fOutxDsrFlow=0x%04x\n",dcb.fOutxDsrFlow);
  printf("fDtrControl=0x%04x\n",dcb.fDtrControl);
  printf("fDsrSensitivity=0x%04x\n",dcb.fDsrSensitivity);
  printf("fTXContinueOnXoff=0x%04x\n",dcb.fTXContinueOnXoff);
  printf("fOutX=0x%04x\n",dcb.fOutX);
  printf("fInX=0x%04x\n",dcb.fInX);
  printf("fErrorChar=0x%04x\n",dcb.fErrorChar);
  printf("fNull=0x%04x\n",dcb.fNull);
  printf("fRtsControl=0x%04x\n",dcb.fRtsControl);
  printf("fAbortOnError=0x%04x\n",dcb.fAbortOnError);
  printf("XonLim=0x%02x\n",dcb.XonLim);
  printf("XoffLim=0x%02x\n",dcb.XoffLim);
  printf("ByteSize=0x%02x\n",dcb.ByteSize);
  printf("Parity=0x%02x\n",dcb.Parity);
  printf("StopBits=0x%02x\n",dcb.StopBits);
  printf("XonChar=0x%02x\n",dcb.XonChar);
  printf("XoffChar=0x%02x\n",dcb.XoffChar);
  printf("ErrorChar=0x%02x\n",dcb.ErrorChar);
  printf("EofChar=0x%02x\n",dcb.EofChar);
  printf("EvtChar=0x%02x\n",dcb.EvtChar);

  dcb.BaudRate = 9600;
  if ( !SetCommState(cfd,&dcb) ) {
    errno = GetLastError();
    printf("SetCommState failed. Errno=%d\n",errno);
    return -1;
  }
  printf("SetCommState OK\n");

  if ( !GetCommTimeouts(cfd,&ct) ) {
    errno = GetLastError();
    printf("GetCommTimeouts failed. Errno=%d\n",errno);
    return -1;
  }
  printf("Got CT\n");
  printf("ReadIntervalTimeout=%d\n",ct.ReadIntervalTimeout);
  printf("ReadTotalTimeoutMultiplier=%d\n",ct.ReadTotalTimeoutMultiplier);
  printf("ReadTotalTimeoutConstant=%d\n",ct.ReadTotalTimeoutConstant);
  printf("WriteTotalTimeoutMultiplier=%d\n",ct.WriteTotalTimeoutMultiplier);
  printf("WriteTotalTimeoutConstant=%d\n",ct.WriteTotalTimeoutConstant);

  ct.ReadIntervalTimeout = 10;
  ct.ReadTotalTimeoutMultiplier = 10;
  ct.ReadTotalTimeoutConstant = 10;
  ct.WriteTotalTimeoutMultiplier = 10;
  ct.WriteTotalTimeoutConstant = 50;
  if ( !SetCommTimeouts(cfd,&ct) ) {
    errno = GetLastError();
    printf("SetCommTimeouts failed. Errno=%d\n",errno);
    return -1;
  }
  printf("Set CT\n");

while (1)//or some End flag
  {
    nBytesToRead = 80;//or max of string
   if ( !ReadFile(cfd,inBuffer,nBytesToRead,&nBytesRead,NULL) ) {
    printf("EOF\n");
    return -1;
  }
  printf("BytesRead: %d\n",nBytesRead);
//you code, for example
//write to file
 Sleep(DELTA_TIMER);
}

  if ( CloseHandle(cfd) )
    printf("Close OK\n");
  else
    printf("Close failed\n");

  return 0;
}
0
sennaspyAuthor Commented:
Thanks...

But this program dont capture all data ... I need redirect all data send to com1 ... in background if possible...
because the data cannot sent to serial printer, but to a text file ....

thanks
0
AlexVirochovskyCommented:
1. Redirect all data:
  instead of :
  while(1)
use function
  WaitForSingleObject()
see example of using in  
http://www.codeguru.com/misc/serialport.shtml 
and while for every getting data.
2.
Send to text file:
In begin of programm:
HANDLE hFile = CreateFile("my.txt", ...);

//after every read from cfd:
WriteFile(hFile, inBuffer,nBytesRead,..);
//in end:
CloseHandle(hFile);
Alex
0
sennaspyAuthor Commented:
Thanks...
But the link:

http://www.codeguru.com/misc/serialport.shtml

is only a blank page.. thanks
0
AlexVirochovskyCommented:
See
http://codeguru.developer.com/mfc/comments/791.shtml(modified
serial port), or write you EMail and
i 'll send you original project,
that has been deleted(???) from
codeguru.
0
sennaspyAuthor Commented:
Do you have another ready example ?

Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Software

From novice to tech pro — start learning today.