Send strings to port in Linux

This is a discussion on Send strings to port in Linux within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I want to pass strings to the following port ( /dev/ttyUSB1 ) using Linux, but I can't get a ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22

    Send strings to port in Linux

    Hi,

    I want to pass strings to the following port (/dev/ttyUSB1) using Linux, but I can't get a succesful connection. The code I am using is the following:

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    #define MODEL_ID "at+gmm"
    
    int main (){
    	
    	string result;
    	ofstream oUSB1;
    	ifstream inUSB1;
    	
    	oUSB1.open ("/dev/ttyUSB1");
    	inUSB1.open ("/dev/ttyUSB1");
    	
    	if (oUSB1.is_open() && inUSB1.is_open()){
    		cout << "Successfully opened communications to Modem" << endl;
    		oUSB1 << MODEL_ID;
    		cout << "Sending command " << MODEL_ID << endl;
    		inUSB1 >> result;
    		oUSB1.close();
    		inUSB1.close();
    		cout << "Received the following back: " << result << endl;
    		}
    	else {
    		cout << "Unsuccessful" << endl;
    		}
    }
    I would be grateful for any help.

    The compiler I am using is g++

    Regards

    Brownie

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One subject for this is probably enough - so I'd suggest that the moderators delete the other one.

    Exactly what happens? Does it give an error message, or simply fails to do what you expect?

    If it's a USB modem, are you sure that it will at all respond to simple text packets like that, and that is doesn't have a protocol on that emulates the serial interface?

    --
    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
    Registered User
    Join Date
    Feb 2006
    Location
    Ballincollig, co. cork, Eire
    Posts
    22
    Hi Mats,

    The program runs ok, no errors generated by the compiler. It seems to just fail in opening the connection to the port located at /dev/ttyUSB1. It skips straight to the else and prints Unsuccessful to the console. This is my first time programming for Linux, so I was just wondering if I had done anything wrong.

    I just presumed it accepted text packets and I'm not sure if there are any protocols on it. I'll look into it.

    I did change the code slightly (shown below).

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    #define MODEL_ID "at+gmm"
    
    int main (){
    	
    	string result;
    	fstream USB1;
    	
    	USB1.open ("/dev/ttyUSB1", fstream::in | fstream::out);
    	
    	if (USB1.is_open()){
    		cout << "Successfully opened communications to Modem" << endl;
    		USB1 << MODEL_ID;
    		cout << "Sending command... " << MODEL_ID << endl;
    		USB1 >> result;
    		USB1.close();
    		cout << "Received the following back: " << result << endl;
    		}
    	else {
    		cout << "Unsuccessful" << endl;
    		}
    }
    Thanks for the suggestion

    Brownie
    Thanks

    Brownie

    ### Novice programmer since forever ###

    ### Currently using Microsoft Visual C++ 2008 Express Edition on Windows Vista ###

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    you may want to try printing errno in the case of "unsuccesful" - I presume you have rights to open that file (e.g. you are running the program under root priviliges)?

    To check that the file can be opened at all, you could do something like "cat /dev/ttyUSB1" or "cat > /dev/ttyUSB1".

    You could also try using miniterm (I think that's the name of it), which is a terminal emulator for Linux - sort of like Hyperterminal (but not at all like it in most respects).

    --
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Why are you opening the device twice? Open it once, in read/write mode.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    Why are you opening the device twice? Open it once, in read/write mode.
    Which the second posted code does, right?

    --
    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.

  7. #7
    Registered User
    Join Date
    Jul 2009
    Posts
    1
    Quote Originally Posted by Brownie View Post
    Hi Mats,

    The program runs ok, no errors generated by the compiler. It seems to just fail in opening the connection to the port located at /dev/ttyUSB1. It skips straight to the else and prints Unsuccessful to the console. This is my first time programming for Linux, so I was just wondering if I had done anything wrong.

    I just presumed it accepted text packets and I'm not sure if there are any protocols on it. I'll look into it.

    I did change the code slightly (shown below).

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    #define MODEL_ID "at+gmm"
    
    int main (){
    	
    	string result;
    	fstream USB1;
    	
    	USB1.open ("/dev/ttyUSB1", fstream::in | fstream::out);
    	
    	if (USB1.is_open()){
    		cout << "Successfully opened communications to Modem" << endl;
    		USB1 << MODEL_ID;
    		cout << "Sending command... " << MODEL_ID << endl;
    		USB1 >> result;
    		USB1.close();
    		cout << "Received the following back: " << result << endl;
    		}
    	else {
    		cout << "Unsuccessful" << endl;
    		}
    }
    Thanks for the suggestion

    Brownie
    You may configure the serial port first, i have done using the system call and stty i have some code like you if you are active yet, i can post it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inet_ntoa to strings
    By TomChesley in forum Networking/Device Communication
    Replies: 7
    Last Post: 02-24-2009, 05:01 PM
  2. Segmentation Fault - Trying to access parallel port
    By tvsinesperanto in forum C Programming
    Replies: 3
    Last Post: 05-24-2006, 04:28 AM
  3. Port app from Windows to Linux
    By BobS0327 in forum Linux Programming
    Replies: 12
    Last Post: 02-12-2006, 02:35 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Stencil Buffer Tutorial Port.. test me! (linux)
    By Perspective in forum Game Programming
    Replies: 3
    Last Post: 08-15-2004, 10:35 PM

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