Thread: Simple RS232 Question

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

    Question Simple RS232 Question

    Hi, could someone please supply me with code for Visual C++ to read from an RS232 connection on Com port 1 with 9600 baud, even parity, 1 stop bit and 7 data bits. I don't care about writing information I just want to read the data.

    I have a powder metering system controlled via my PC and want to stop it when the reading from the RS232 connected balance hits the level I require.

    many many thanks

    Rich

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Win32 treats the serial port as a kind of file
    Start by reading all about createfile
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    So would you use "COM1" as the file to open? I'm just guessing; I remember doing something like that back in days of QBasic
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Here is some code for doing com port I/O.

    gg

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    When I try to compile that I get

    Compiling...
    SFile.cpp
    Linking...
    LIBCD.lib(crt0.obj) : error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup
    Debug/rs232.exe : fatal error LNK1120: 1 unresolved externals

    If someone could supply me with code which would read from Com port 1, 9600 baud, even parity, 7 data bits and 1 stop bit I promise to thank them forever (and possibly financially if this project works!). Please help!

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    If you want to use 7 data bits and even parity then you need to make some changes in SFile.cpp:
    Code:
    // Change this line
    static TCHAR MODESETTINGS[]= _T("baud=%d parity=N data=8 stop=1  ");
    // to
    static TCHAR MODESETTINGS[]= _T("baud=%d parity=E data=7 stop=1  ");
    Here is some simple code that demonstrates how to use SFile.
    Code:
    #include <windows.h>
    #include "SFile.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        SFile sf;
    
        // open COM1 at 9600, 7E1
        if (!sf.Open(1, 9600))
        {
            cerr << "Failed to open COM1, ec = " << GetLastError() << endl;
            return 1;
        }//if
    
        // read a byte at a time and display it
        // (this should loop forever)
        BYTE b;
        while (sf.ReadByte(b))
        {
            cout << (int)b << endl;
        }//while
    
        return 0;
    }//main
    gg

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    Thanks so much!! Am now able to get the reading from the balance! However, I need to get the reading every 5 seconds or so, which requires sending the command 'S(carriage return)', could you explain simply how the writebyte command works since I cannot get it to work correctly.

    Thanks again, you are a star!

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    ...since I cannot get it to work correctly.
    Show what you have tried.

    Here is how to use SFile::Write()
    Code:
        char cmd[] = "S\r";
        DWORD written;
        ....
        sf.Write(cmd, 2, written);
    You can use Sleep(5000) to sleep for 5 seconds.

    gg

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    I'm trying to send the command using writebyte since write did not seem to work. I just don't know the exact syntax to use :s

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    7
    I've worked it out!

    Thanks again for your help, if you are ever in Oxford then I owe you at least one pint!

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    1
    Hello Codeplug

    I am using this library thank you very much for providing it. I have tried the example above and it works perfectly. So i have serial data in the form of (40 45 FE 12 34) coming from the serial port and the BYTE b displays them in line (it is pre formatted from my microcontroler) The thing is i would like to store the icoming bytes into a const char* to send it to TCP socket (I allready have that part of code working) Ive tried different manners but it seems i need to convert them to a string or something similar.
    I am sorry i am not very strong in C++ and i have searched a lot i can only ask u now if u can give me a lil clue or an idea of how to perform this im sure this is easy for u
    I thank u very much in advance

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Please dont open long closed threads, please start a new one.

    CLOSED

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM