Basic messenger via serial port

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bassem
    Contributor
    • Dec 2008
    • 344

    #1

    Basic messenger via serial port

    Hello everyone,

    What I'm trying to do is a basic messenger (char receiver/ transmitter) via serial port in a console application.
    in main function i call setup_configura tion function ---- no poroblem
    then i use two functions; transmit, receive as follow:

    Code:
    void transmit (char ch)
    {
    do{
     int y = inportb(0x3fd);
     y = y & 0x40;
    }while (y == 0);
    outportb(0x3f8, (int) ch);
    }
    Code:
    char receive ()
    {
    if( inport (0x3fd) & 1 == 1)
    return inport (0x3f8);
    return -1;
    }
    I've no error, but the problem is i check if there is a received char in the register and print it or not, but for transmitting a char i should use cin, or getch, getche.
    any function of them i have to wait until the user enter a char, so i stopped from receiving.. i have to execute only one statement at a time.

    I use console so there is no events and event handler, i don't want to go to threads to monitor both transmit and receive.

    If anyone can help me, I'll be thankful.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    You have to use select function call to monitor for input or output and then call your function to read it.


    Raghu

    Comment

    • Bassem
      Contributor
      • Dec 2008
      • 344

      #3
      Sorry Raghu, can you explain a little!

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Select is a system call with which you can find whether data is available on the file descriptor.
        Read this for more info on select.

        Raghu

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Have you considered placing your tramsmit on one thread and the receive on another thread? That way you can tramsmit and receive independently.

          Comment

          • Bassem
            Contributor
            • Dec 2008
            • 344

            #6
            Thanks Raghu,
            but i couldnt understand select function.

            it seems noway to use multiple threads as weaknessforcats said, and that was i'm trying to avoid, because i didn't deal with threads in c++.

            Thanks all for your help,
            Bassem

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Multuthreading is not that hard.

              Each thread is basically a function call and your operating system already API functions to create threads from these functions and to allow you to communicate from one thread to another.

              There is also a critical section where one thread can block other threads to gain update access to data shared between the threads without fear of the other thread trying to so the same at the same time.

              A simple program like this should go pretty quick.

              Comment

              Working...