Thread: read com port in winapi

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    read com port in winapi

    Hi

    I am making some hardware to a pc were i need some software to get all info that the com 2 ( $2f8 ) sends into the pc collected in a text file and in the same time being viewed on the screen while running.
    the buffer needs to be read every sec. the signal that is comming in is 8 bit.

    now i hoped that some of the clever guys in programming could help me out with an with an exsample that does this. i got the dev c++ compiler. or is willing to give me a big hand with this since i am new to this


    brgrds

    Glenn

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Here is an algorithm to one solution.

    -----
    ifstream input;
    input.open("COM2", ios::in);
    read();
    input.close();

    ofstream output;
    output.open("COM2", ios:ut);
    write();

    // Another solution running in Win32.

    HANDLE hFile = CreateFile("COM2",...);
    -----

    You will need to implement a non-blocking I/O model for serial-port communication. Read this article at MSDN.

    http://msdn.microsoft.com/library/de...sdn_serial.asp

    Kuphryn

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I may be wrong here and certainly too lazy to look it up myself but I believe you would use DeviceIoControl().

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Angry hmmm

    I think this is to complicated todo myself.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Thumbs down

    I think this is to complicated todo myself.
    Then perhaps you should choose a project that you can handle, right
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    :O) yaa But

    I don`t know any who do c++ and i really need this for my hardware i created....if any of you know someone ( or yourself maybe ) who is good at this ( really good i guess ) and want to earn some money i would be happy to pay for this.

    Glenn

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I assure you that the design and implementation of non-blocking I/O for serial communication is not difficult. I believe the article at MSDN caused you to think different. Look at the article and you will notice there are many lines of comments. The actual design and implementation code is quite simple.

    I highly recommend you read it over and try to code it. Afterward, modify it to fit your design.

    Kuphryn

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    :O) yaa But

    I don`t know any who do c++ and i really need this for my hardware i created....if any of you know someone ( or yourself maybe ) who is good at this ( really good i guess ) and want to earn some money i would be happy to pay for this.

    Glenn

  9. #9
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    Talking i will give it a chance more

    if you say it is not that hard i believe you

  10. #10
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    Mercenary

    > ...and want to earn some money i would be happy to pay for this

    I wrote a C++ class to do this a while back. Check out the public section of class below. I guess you want it documenting, which I could do over the weekend. We would need to agree a price & other details. If you're interested, drop me a mail.

    [email protected]

    Code:
    class CommPort
    {
    private:
    
    // Private stuff cut from here
    
    public:
      CommPort();
      virtual ~CommPort();
      HANDLE getPortHndl();
      bool active();
      void open();
      void open(int portNum);
      void setPortDefaults();
      int  readBlock(void * block, int blkSize);
      char readChar();
      int  peekBlock(void * block, int blkSize);
      char peekChar(int count);
      int  writeBlock(const void * block, int blkSize);
      bool writeChar(char c);
      int  writeString(const AnsiString& str);
      void flushRxBuf();
      void flushTxBuf();
      void close();
      int  getPortNum();
      void setPortNum(int portNum);
      CommBaud getBaud();
      void setBaud(CommBaud baud);
      int  getDataBits();
      void setDataBits(int dbts);
      CommStopBits getStopBits();
      void setStopBits(CommStopBits stopBits);
      CommParity getParity();
      void setParity(CommParity parity);
      int  getRxBufSize();
      void setRxBufSize(int bufSize);
      int  getTxBufSize();
      void setTxBufSize(int bufSize);
      int  getRxBufUsed();
      int  getTxBufUsed();
      bool getRxBufEmpty();
      CommSwFlow getSwFlow();
      void setSwFlow(CommSwFlow flowCntrl);
      int  getRxBufXOn();
      void setRxBufXOn(int xOnLim);
      int  getRxBufXOff();
      void setRxBufXOff(int xOffLim);
      char getXOnChar();
      void setXOnChar(char XOn);
      char getXOffChar();
      void setXOffChar(char XOff);
      bool getTxCtsFlow();
      void setTxCtsFlow(bool cts);
      bool getTxDsrFlow();
      void setTxDsrFlow(bool dsr);
      CommDtrFlow getDtrFlow();
      void setDtrFlow(CommDtrFlow dtr);
      CommRtsFlow getRtsFlow();
      void setRtsFlow(CommRtsFlow rts);
      void clearBreak();
      void setBreak();
      bool getCtsState();
      bool getDsrState();
      bool getRingState();
      bool getRlsdState();
      int  getRxTimeout();
      void setRxTimeout(int millisec);
      int  getRxTimeoutMult();
      void setRxTimeoutMult(int millisec);
      int  getRxTimeoutConst();
      void setRxTimeoutConst(int millisec);
      int  getTxTimeoutMult();
      void setTxTimeoutMult(int millisec);
      int  getTxTimeoutConst();
      void setTxTimeoutConst(int millisec);
      bool showConfigDlg();
      bool showDefConfigDlg();
      AnsiString getConfigStr();
      AnsiString getSettableStr();
    };

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Very impressive class, Davros. Looks like money well spent!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  12. #12
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    balle, what do you need? If it isn't too complicated, I'm sure someone might do it quickly for free.

  13. #13
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    lol programs made for programmers
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    needs

    what i need is a full screen window with 3 pages ( tabs ). each window need to be splittet into 4 windows .

    now tab one need to have one window ( window 1, page 1 ) where the is 2 checkboxes and a textfield showing the content of a file ( this file will have max. 10 letters in upper left frame .)
    The second tab page need to show all input streaming in from comport 2 ( port adress $2f8-3ff ) an update live all the timer when focus on that tab.
    3 tab is for a big picure in the first uppper left frame. The upper right fram is some simple text. the below left frame is showing content of a small textfile too. and the last below right frame is used to enter some text into a textfile for saving it when pushing "save" button.


    hope it is just a bit understandable
    and one of you could help me out.

  15. #15
    Registered User
    Join Date
    Oct 2002
    Posts
    7

    needs

    what i need is a full screen window with 3 pages ( tabs ). each window need to be splittet into 4 windows .

    now tab one need to have one window ( window 1, page 1 ) where the is 2 checkboxes and a textfield showing the content of a file ( this file will have max. 10 letters in upper left frame .)
    The second tab page need to show all input streaming in from comport 2 ( port adress $2f8-3ff ) an update live all the timer when focus on that tab.
    3 tab is for a big picure in the first uppper left frame. The upper right fram is some simple text. the below left frame is showing content of a small textfile too. and the last below right frame is used to enter some text into a textfile for saving it when pushing "save" button.


    hope it is just a bit understandable
    and one of you could help me out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serial port read() blocking
    By DavidDobson in forum C Programming
    Replies: 8
    Last Post: 04-01-2009, 10:45 AM
  2. Read from 15-pin port
    By C_ntua in forum C Programming
    Replies: 23
    Last Post: 07-11-2008, 09:09 AM
  3. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  4. serial port - read chars twice
    By matze in forum Linux Programming
    Replies: 1
    Last Post: 12-07-2007, 07:32 AM
  5. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 08:21 AM