Thread: UART Serial Communication

  1. #1
    Registered User
    Join Date
    Nov 2013

    UART Serial Communication

    Hi, I am trying to implement a code that is able to receive input from a serial communication. I believe that it is possible to do this using fstream, but so far I have been unsuccessful. The code below should echo everything received on screen:

    #include <iostream>
    #include <fstream>
    int main(int argc, const char * argv[])
        std::string msg = "";
        std::cout << "Hello, World!\n";
        std::fstream file("/dev/tty.usbserial-A800F2GZ");
        if (file.good())
            file >> msg;
            std::cout << "Response: " << msg << std::endl;
        return 0;
    Any ideas?

    Currently using: Mac OS X 10.8.5, Xcode 5

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Define "unsuccessful"; compile issue? runtime fault? just no output? something else? If nothing else, you should have an else for your if that tells you why file is not good().

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    It also won't work if the sender isn't sending using the default baud rate and parity settings for your dev/tty
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial communication
    By cbrandy in forum C Programming
    Replies: 2
    Last Post: 02-24-2012, 07:50 AM
  2. Serial communication
    By chico1st in forum C Programming
    Replies: 8
    Last Post: 09-10-2008, 02:01 PM
  3. Serial Communication in C++
    By NewGuy100 in forum C++ Programming
    Replies: 8
    Last Post: 04-24-2006, 01:56 PM
  4. serial communication
    By hick.hack in forum C++ Programming
    Replies: 1
    Last Post: 02-13-2003, 12:18 PM
  5. Serial Communication
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 01-17-2002, 08:36 AM