Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Serial port in windows

Posted on 1998-03-05
15
Medium Priority
?
694 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

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 400 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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 …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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