rs232/485 communication

This is a discussion on rs232/485 communication within the C++ Programming forums, part of the General Programming Boards category; hi, i'm going to develop one application to communicate rs232 or by using c++ or c# language. can u give ...

  1. #1
    sgh
    sgh is offline
    Registered User
    Join Date
    Apr 2008
    Posts
    58

    rs232/485 communication

    hi,
    i'm going to develop one application to communicate rs232 or by using c++ or c# language.
    can u give me a snippet code or url for it?
    any suggestion will be welcome!!!
    regrads
    so

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so to start with, there is no STANDARD way to communicate on a serial port in C. It is all depending on the OS and hardware you have to work with. So if you tell us what OS and hardware you have, we MAY be able to help you (but of course, it is also possible that we can't, if the hardware or OS is really unusual).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Go with C#, can't be any easier:
    Code:
    using(var Port = new System.IO.Ports.SerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One))
    {
    	Port.Open();
    	Port.Write("Hello");
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Magos View Post
    Go with C#, can't be any easier:
    Yes, and it's not NECESSARILY much harder in C++ or C. You may need two or three lines to open the port, then you just write to and read from the port - in some OS's. Presumably C# isn't really viable for real-time OS systems that do not support files and such things, so in that case, it's more C or C++ only.

    Obviously, in any language, it gets more complex if we have to deal with transmission errors, time-outs due to other end dropping out, etc, etc, which is usually necessary for REAL systems.

    But giving examples of how it's done in one or another environment is pointless if the environment that the original poster is asking about is one that is not at all similar - and it doesn't matter if we give a simple or complex example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    the previous principal software engineer at my company picked these out:

    http://marshallsoft.com/

    i've used them for years. their documentation could be a lot better, but i've never had any real problems with it.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    I quite like this site - http://www.lvr.com/serport.htm
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    sgh
    sgh is offline
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    i appreciate your help.
    and thx, Magos.
    but i want to get some code about 485 communicate.
    can u help me?
    regards
    so

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Magos View Post
    Go with C#, can't be any easier:
    Code:
    using(var Port = new System.IO.Ports.SerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One))
    {
        Port.Open();
        Port.Write("Hello");
    }
    Go with C/C++ its even easier than that

    Code:
    FILE* pFile = fopen("COM1:" , "w+b");
    fprintf("Hello World\n",pFile);
    For more detailed control see http://msdn.microsoft.com/en-us/library/ms810467.aspx
    Last edited by abachler; 02-21-2009 at 02:42 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by sgh View Post
    i appreciate your help.
    and thx, Magos.
    but i want to get some code about 485 communicate.
    can u help me?
    regards
    so
    RS-485? Never used that, but should be the same like RS-232 from what I heard.
    Could you be more specific in what you're trying to achieve?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Magos View Post
    RS-485? Never used that, but should be the same like RS-232 from what I heard.
    From a software point of view there is none, but note that RS-485 does not require a particular protocol, so your software would have to impliment any protocol already in use on the system.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  11. #11
    sgh
    sgh is offline
    Registered User
    Join Date
    Apr 2008
    Posts
    58
    thank u for your help.
    i need one more thing.
    it's usb communication.
    i bought one device(fingerprint) and i want to develop program to use this one using rs232/485 or usb.
    i finished already developing program via rs232 communicating.
    and i'd like to communicate via usb bus.
    how can i do it?
    best regards.
    p.s.
    my english is no so good.
    thx

  12. #12
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    USB communication is much harder. Unlike RS232 it's not a port but a bus.
    You have to google some info.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    you should also make sure you back up your files before embarking on a usb driver.

    the equipment i use that uses a USB connection is VERY finnicky.

    for example, providing the wrong ADC code would cause the whole OS to lock up on one device.

    on another, closing an invalid device handle gave me 'the blue screen of death' :|

    of course, these are problems with the OEM's code and not with USB, but you should be very careful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 09:33 AM
  2. Communication between programs?
    By johny145 in forum Windows Programming
    Replies: 3
    Last Post: 06-01-2005, 10:14 PM
  3. Looking for communication lib
    By BrownB in forum C Programming
    Replies: 3
    Last Post: 04-27-2005, 10:01 AM
  4. Serial communication packets
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 04-26-2003, 08:33 AM
  5. communication
    By in need of help in forum C Programming
    Replies: 3
    Last Post: 12-27-2002, 02:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21