UART Serial Communication

This is a discussion on UART Serial Communication within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I am trying to implement a code that is able to receive input from a serial communication. I believe ...

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    1

    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:

    Code:
    #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())
        {
     while(1){
            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
    Posts
    14,185
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 06: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, 11:18 AM
  5. Serial Communication
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 01-17-2002, 07:36 AM

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