Solved

Serial port in windows

Posted on 1998-03-05
15
682 Views
Last Modified: 2013-12-03

I want to use the serial port in windows and i can't use the _bios_serialcom cause it's a #ifdef in its header so Borland C++ 5.0 dosen't include it...

Witch function should i use in win program ??

0
Comment
Question by:superabbe
  • 8
  • 2
  • 2
  • +3
15 Comments
 
LVL 1

Expert Comment

by:rayofunreal
ID: 1411753
I don't understand you. Rewrite #ifdef directive. Or what is the problem ? :)
0
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1411754
Which OS do you need to support? And is it 16-bits or 32-bits?

0
 
LVL 7

Expert Comment

by:galkin
ID: 1411755
Serial port is treated as a file in win32. So you can open serial port with CreateFile win32 API function.

HANDLE   hPort = ::CreateFile( "COM1",
            GENERIC_READ | GENERIC_WRITE,
            0,      // exclusive access                
            NULL,  // no security attributes
            OPEN_EXISTING,
            FILE_ATTRIBUTE_NORMAL,
            NULL  // No template
  );

Besides I guess there is OWL class TFile so you can use it instead of API
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:superabbe
ID: 1411756
rayofunreal:
if i comment the ifdef statement I'll recive a unresolved external in _bios_serialcom..
thui:
Os is win95 ....

Galkin........

i need some more info from you..
How would i config the port to read chars from it during let's say 10 sec ??

i'm gonna receive about 5-10 chars an send 3 char to my d-box..
it must use 19200 baud 8 databits no parity 1 stopbit...


0
 
LVL 7

Expert Comment

by:galkin
ID: 1411757
To configer port use  
SetCommConfig, SetCommMask, SetCommTimeouts and WaitCommEvent win32 API functions. to read and wrtite to port use ReadFile and WriteFile.
 

0
 

Author Comment

by:superabbe
ID: 1411758
Argh

Could you give a small example ???
like to config it to 19200 and receive a char or 2 ??

it's a bit hard to understand how microsoft want it to be done ,i think.

ByTheWay this old dos _bios_serialcom , doesen't it support 19200 ??

0
 
LVL 1

Expert Comment

by:JamieR
ID: 1411759
If your still interested, there is a really crap example in MSDN called Comm (TTY).

0
 

Author Comment

by:superabbe
ID: 1411760
Since i hvent recived a commet from galkin i have to reopen this question.
0
 

Accepted Solution

by:
hf earned 100 total points
ID: 1411761
This should work (crude, but it will help)  :-)
Note that the timeout will stop the thread while waiting


//port = "COM2"
int Open(char *port)
{
      hcomm = CreateFile(port,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
          if (hcomm == INVALID_HANDLE_VALUE) {
              return 1;
        }
      return 0;
}

int Close(void)
{
      CloseHandle(hcomm);
      return 0;
}

//Setting in format "9600,n,8,1"
int (char *settings)
{
        FillMemory(&dcb, sizeof(dcb), 0);
        dcb.DCBlength = sizeof(dcb);
        if(!BuildCommDCB(settings, &dcb))  {
          //could not build dcb
          return 1;
        }else  {
          if(!SetCommState(hcomm, &dcb))
            return 2;
        }
        return 0;
}


int SetTimeOut(void)
{      
      timeouts.ReadIntervalTimeout = 20;
              timeouts.ReadTotalTimeoutMultiplier = 20;
         timeouts.ReadTotalTimeoutConstant = 200;
      timeouts.WriteTotalTimeoutMultiplier = 20;
      timeouts.WriteTotalTimeoutConstant = 200;
      if(!SetCommTimeouts(hcomm, &timeouts))
        return 1;
        return 0;
}

int SetDTR(int value)
{
        if(value)  {
        if (EscapeCommFunction(hcomm,SETDTR) == 0)   {
            return 1;
          }
        }else  {
          if (EscapeCommFunction(hcomm,CLRDTR) == 0)   {
            return 1;
          }
        }
        return 0;
}

int SetRTS(int value)
{
        if(value)  {
        if (EscapeCommFunction(hcomm,SETRTS) == 0)   {
            return 1;
          }
        }else  {
          if (EscapeCommFunction(hcomm,CLRRTS) == 0)   {
            return 1;
          }
        }
        return 0;
}

int Tx(char *pBuffer,int nLen)
{
  DWORD nWritten;

  if (WriteFile(hcomm,pBuffer,nLen,&nWritten,NULL) == 0)
    return -1;
  else
    return nWritten;
}

int Rx(char *pBuffer,int nSize)
{
  DWORD nRead;

  if (ReadFile(hcomm,pBuffer,nSize,&nRead,NULL) == 0)
    return -1;
  else
    return nRead;
}

0
 

Author Comment

by:superabbe
ID: 1411762
As i remember the _bios_serilal com does not accept higher speed than 9600  , does this code allow me 19200
if i chage the createfile param ?

And i'm not sure what the rts and dtr is.

BTW a good example , where did you find it ? :)

0
 

Author Comment

by:superabbe
ID: 1411763
As i remember the _bios_serilal com does not accept higher speed than 9600  , does this code allow me 19200
if i chage the createfile param ?

And i'm not sure what the rts and dtr is.

BTW a good example , where did you find it ? :)

0
 

Author Comment

by:superabbe
ID: 1411764
As i remember the _bios_serilal com does not accept higher speed than 9600  , does this code allow me 19200
if i chage the createfile param ?

And i'm not sure what the rts and dtr is.

BTW a good example , where did you find it ? :)

0
 

Expert Comment

by:hf
ID: 1411765
Higher speeds should work - havn't tried it though

rts & dtr are handshacking signals for hardwate flow control (actually RTS & CTS)

wrote it myself of the win api help files (sorry its crude - but came of a comms & protocoll object i wrote)
0
 

Author Comment

by:superabbe
ID: 1411766
No more comments...
0
 

Author Comment

by:superabbe
ID: 1411767
No more comments...
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

803 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