dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Serial Port

  1. #1
    Join Date
    Apr 2007
    Posts
    16

    Serial Port

    Does anyone know how to send and to read data from/to serial port?
    I am using C++ borland 6. ( console format in C)
    Code:
    #include <bios.h>
    #include <conio.h>
    #define COM1       0
    #define DATA_READY 0x100
    #define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)
    int main(void)
    {
       int in, out, status;
       bioscom(0, SETTINGS, COM1); /*initialize the port*/
       cprintf("Data sent to you:  ");
       while (1)
       {
          status = bioscom(3, 0, COM1); /*wait until get a data*/
          if (status & DATA_READY)
               if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)  /*input a data*/
                  putch(out);
               if (kbhit())
               {
                  if ((in = getch()) == 27)   /* ASCII of Esc*/
                     break;
                  bioscom(1, in, COM1);   /*output a data*/
               }
       }
       return 0;
    }
    the warning error is that bios.h is unable to open.
    any suggestion what's wrong with the code?

    cheers

    ryann2k1
    Last edited by Ejaz; June 27th, 2007 at 12:57 AM. Reason: Code tag added

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,649

    Re: Serial Port

    Seems like an old 16-bit environment is the target?

  3. #3
    Join Date
    May 2007
    Posts
    680

    Re: Serial Port

    I am also using the same compiler, and I think that borland does not hve that include file...

    To check go to borland C++ builder root folder and look under the include folder and if bios is there it should work otherwise not...

  4. #4
    Join Date
    Apr 2007
    Posts
    16

    Re: Serial Port

    Hi..
    Thank for your responds.
    I want to read and send data from the serial port of my PC. would like to advice. what should I revise in my code since bios.h is not in include files of borland or any code that I can refer to?
    Thank you.

    Regards,

    Anshar

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,649

    Re: Serial Port

    Code fragments for a quick start
    Code:
    HANDLE hPort = CreateFile( "Com1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL );
    
    DCB                     dcb;  // Set ALL params for these
    COMMTIMEOUTS  ct;
    
    SetCommState( hPort, &dcb );
    SetCommTimeouts( hPort, &ct );
    ...
    ulong count;
    WriteFile( hport, &str, sizeof(str), &count, NULL );
    ReadFile( hport, &str, 10, &count, NULL );
    ...
    CloseHandle( hPort );

  6. #6
    Join Date
    Apr 2007
    Posts
    16

    Re: Serial Port

    Hi SMA,

    Thank you.. really appreciate that.

    cheers

    ryann2k1

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,649

    Re: Serial Port

    You're welcome

  8. #8
    Join Date
    Apr 2007
    Posts
    16

    Re: Serial Port

    Hi again SMA,
    I have been busy doing my study till dont have enough time to work on my serial control.
    In your code, where exactly that I can put my data that i want to send?
    sorry, I am still a beginner for C or C++.
    thank you...

    cheers,

    ryann2k1

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,649

    Re: Serial Port

    A serial port is handled just like a file (more or less anyway) so reading and writing from/to the serial port is done with ReadFile/WriteFile. Parameters for those functions (and of course also the other) can be read about here http://msdn2.microsoft.com/en-us/library/aa365467.aspx

  10. #10
    Join Date
    Apr 2007
    Posts
    16

    Re: Serial Port

    Thanks again SMA...

    cheers,

    ryannk21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)