Solved

Serial port in windows

Posted on 1998-03-05
15
693 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
Veeam gives away 10 full conference passes

Veeam is a VMworld 2017 US & Europe Platinum Sponsor. Enter the raffle to get the full conference pass. Pass includes the admission to all general and breakout sessions, VMware Hands-On Labs, Solutions Exchange, exclusive giveaways and the great VMworld Customer Appreciation Part

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

634 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