Thread: [C Linux sockets] recv() gives messy result

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    [C Linux sockets] recv() gives messy result

    Hey,
    I want to use socket programming (bluetooth) to receive NMEA data from my GPS receiver.

    On channel 1 is the serial Port service.

    When I use the rfcomm program available on linux and connect the bluetooth device to a specific device file /dev/rfcomm1 I receive clear data

    Code:
    rfcomm connect /dev/rfcomm1 <btaddr>
    cat /dev/rfcomm1
    gives
    Code:
    $GPGGA,1745.000,457.2558,N,00559.1555,E,1,5,0.55-9.5,M,55.1,M,,*75
    
    $GPGSA,A,3,55,26,55,15,55,22,55,27,,,,,1.55,0.93,1.25*05
    
    $GPGSV,3,1,55,15,55,248,19,58,58,064,55,57,47,556,15,26,30,302,31*55
    
    $GPGSV,3,2,12,18,29,305,29,08,20,073,17,17,16,119,,05,13,189,22*73
    
    $GPGSV,3,3,12,22,06,330,18,24,04,285,,21,01,281,,38,,,*4A
    
    $GPRMC,555545.055,A,5557.5558,N,00559.1552,E,0.55,155.28,155510,,,A*65
    
    $GPVTG,155.25,T,,M,0.25,N,0.53,K,A*3D
    But when I use a simple recv() call in a while loop.
    Code:
    while(1)
    {
    		bytes_recv = recv(sock, &buf, sizeof(buf), 0);
    		printf("%s", buf);
    }
    I get a more messy result
    Code:
    VTG,155.55,T,,M,0.55,N,0.21,K,A*35
    555,E,0.15,555.46,15510,,,A*67
    $GP
    $GPGSA,A,5,18,26,27,15,55,28,58,,,,,,4.00,2.73,2.92*07
    $GPGSV,3,1,55,15,80,243,27,55,48,064,35,25,45,255,25,55,30,355,25*75
    $GPGSV,3,2,15,25,50,352,25,08,20,553,21,15,16,119,,05,12,189,*79
    $GPGSV,3,3,12,25,07,359,25,24,03,554,18,21,51,281,,51,,,*4D
    $GP
    Does anyone knows how this is possible and how to fix it.
    Of course it's possible because the rfcomm program can do it.

    Martijn

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You probably need to pay attention to bytes_recv -- any particular recv call may not get the entire message, for one reason or another, so if it (appears to be) incomplete, then you will need to paste the next message onto the end of the first one.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    also buf could be not null-terminated so %s is not the best way to print it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Yes yes,

    the data certainly arrives in half messages and by using
    Code:
    printf("%.*s", bytes_recv, buf);
    I get clear data now.

    Now ofcourse... I have to add every recv() result to a string and then walk through that string to reveal every NMEA sentence.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to catch the result of linux command
    By mihu in forum Linux Programming
    Replies: 10
    Last Post: 04-16-2006, 06:57 AM
  2. Dabbling with Linux.
    By Hunter2 in forum Tech Board
    Replies: 21
    Last Post: 04-21-2005, 04:17 PM
  3. installing linux for the first time
    By Micko in forum Tech Board
    Replies: 9
    Last Post: 12-06-2004, 05:15 AM
  4. Linux Linux why Linux??
    By afreedboy in forum Tech Board
    Replies: 146
    Last Post: 01-21-2004, 06:27 PM