write() to serial port (Bluetooth) does not always work except when debugging
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: write() to serial port (Bluetooth) does not always work except when debugging

  1. #1
    Join Date
    Apr 2005
    Posts
    151

    write() to serial port (Bluetooth) does not always work except when debugging

    It's sunday again so time to work on my robot project again...

    I have a problem that I can't seem to figure out :

    I have a serial communication through Bluetooth between my Raspberry Pi and my PC with Putty.
    I use the following code to send text from my RPi to Putty :
    Code:
    int serial::writeSerialPort(char const *string)
    {
       /*
       write a string to the serial connection
       */
       int result = write(fd, string, strlen(string)) ;
       tcflush(fd, TCOFLUSH);
       return result ;
    }
    The strange thing now is that sometimes the text is being send to Putty and that works a while.
    Then suddenly it stops working, after a while it starts working again (all in the same session).

    When I set a breakpoint and debug my code the text is always being outputted right after the write() function (before the tcflush() ).

    This of course makes it hard to find out why I have this "bug".

    Anyone that can point me in the right direction?

    edit :

    I changed my code to
    Code:
    ssize_t serial::writeSerialPort(char const *string)
    {
    	/*
    	 write a string to the serial connection
    	 */
    	
    	ssize_t result = write(fd, string, strlen(string)) ;
    	
    	tcflush(fd, TCOFLUSH);
    	if (result != strlen(string))
    		printf("Failed to write to serial port!\n") ;
    	else
    		printf("Wrote to serial port!\n") ;
    	
    	return result ;
    }
    and it seems that the write function always return a value that is equal to strlen(string) so I'm sure that the write() worked!
    Last edited by da_cobra; February 26th, 2017 at 04:11 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)