Solved

Serial port in windows

Posted on 1998-03-05
15
689 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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