Solved

Serial port in windows

Posted on 1998-03-05
15
673 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now