[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Redirect serial port source code..

Posted on 1999-11-14
10
Medium Priority
?
1,058 Views
Last Modified: 2013-11-15
Hi...
I need a redirect serial port source code in c++ (visual c++ or borland c++/ borland c++ builder)..

thanks for all
0
Comment
Question by:sennaspy
  • 5
  • 5
10 Comments
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 400 total points
ID: 2206441
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
 
LVL 3

Author Comment

by:sennaspy
ID: 2206581
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
 
LVL 3

Author Comment

by:sennaspy
ID: 2206595
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2206605
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
 
LVL 3

Author Comment

by:sennaspy
ID: 2207807
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
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2208448
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
 
LVL 3

Author Comment

by:sennaspy
ID: 2226184
Thanks...
But the link:

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

is only a blank page.. thanks
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2227298
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
 
LVL 3

Author Comment

by:sennaspy
ID: 2302080
Do you have another ready example ?

Thanks
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Virtualization software lets you run different versions of Windows, Ubuntu Linux and other versions of Linux all at the same time, rather than running each one directly from your computer's hard drive.
In today’s time where quality is an essential factor all over the world, software testing and effective QA (Quality Assurance) is an all-important element for any business to ensure less risk for an end product. A good software testing company deliv…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question