Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. problem in reading data from com port

problem in reading data from com port

Scheduled Pinned Locked Moved C / C++ / MFC
comhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manju23reddy
    wrote on last edited by
    #1

    hi i have a code which was supposed to read data from serial com (bluetooth) and save in a file. but when i send a audio file from my mobile to pc the program will not read the port i have configured the bluetooth connection for serial communication (i.e from system stray selecting bluetooth icon and from quick connect selecting serial connection). instead it shows that connection is succesfull for PIM transfer, so do i need to write a application in my mobile to send data serially.. if u have any details about it please post me.. (right now i dont have any application in mobile iam directly sending file via bluetooth)...

    R 1 Reply Last reply
    0
    • M manju23reddy

      hi i have a code which was supposed to read data from serial com (bluetooth) and save in a file. but when i send a audio file from my mobile to pc the program will not read the port i have configured the bluetooth connection for serial communication (i.e from system stray selecting bluetooth icon and from quick connect selecting serial connection). instead it shows that connection is succesfull for PIM transfer, so do i need to write a application in my mobile to send data serially.. if u have any details about it please post me.. (right now i dont have any application in mobile iam directly sending file via bluetooth)...

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      Are you sure your mobile supports the Bluetooth Serial Port Profile? Try to use Hyperterminal, or similar, to open a serial connection to the mobile and read data. When you get that working you can worry about your code on the PC side.

      "It's supposed to be hard, otherwise anybody could do it!" - selfquote
      "High speed never compensates for wrong direction!" - unknown

      M 1 Reply Last reply
      0
      • R Roger Stoltz

        Are you sure your mobile supports the Bluetooth Serial Port Profile? Try to use Hyperterminal, or similar, to open a serial connection to the mobile and read data. When you get that working you can worry about your code on the PC side.

        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        M Offline
        M Offline
        manju23reddy
        wrote on last edited by
        #3

        hi the at command works correctly it echos ok. this is my read data code bool ReadPort() { long int dwSize = 0; bool hResult = false; std::string sb = ""; DWORD dwEventMask; if(!SetCommMask(hComm, EV_RXCHAR)) /* Setting Event Type */ { return hResult; } if(WaitCommEvent(hComm, &dwEventMask, NULL)) /* Waiting For Event to Occur */ { char szBuf[1024]; DWORD dwIncommingReadSize; do { if(ReadFile(hComm, &szBuf, 1024, &dwIncommingReadSize, NULL) != 0) { if(dwIncommingReadSize > 0) { dwSize += dwIncommingReadSize; sb.append(szBuf); } hResult = true; } else { unsigned long error = ::GetLastError(); hResult = false; printf("the error while reading error is %dl\n", error); break; } } while(dwIncommingReadSize > 0); *readData = new char[dwSize]; strcpy(*readData, sb.c_str()); return hResult; } else { return hResult; } } it stops to WaitCommEvent function i feel i need to do some settings for my phone and pc to transfer the data serially but now it is PIM iam totally blank if u have any such settings for the transfer of data via serial com of bluetooth plz send in me... my phone is nokia 6288 i know this is not the forum for phone settings but please help me...

        R 1 Reply Last reply
        0
        • M manju23reddy

          hi the at command works correctly it echos ok. this is my read data code bool ReadPort() { long int dwSize = 0; bool hResult = false; std::string sb = ""; DWORD dwEventMask; if(!SetCommMask(hComm, EV_RXCHAR)) /* Setting Event Type */ { return hResult; } if(WaitCommEvent(hComm, &dwEventMask, NULL)) /* Waiting For Event to Occur */ { char szBuf[1024]; DWORD dwIncommingReadSize; do { if(ReadFile(hComm, &szBuf, 1024, &dwIncommingReadSize, NULL) != 0) { if(dwIncommingReadSize > 0) { dwSize += dwIncommingReadSize; sb.append(szBuf); } hResult = true; } else { unsigned long error = ::GetLastError(); hResult = false; printf("the error while reading error is %dl\n", error); break; } } while(dwIncommingReadSize > 0); *readData = new char[dwSize]; strcpy(*readData, sb.c_str()); return hResult; } else { return hResult; } } it stops to WaitCommEvent function i feel i need to do some settings for my phone and pc to transfer the data serially but now it is PIM iam totally blank if u have any such settings for the transfer of data via serial com of bluetooth plz send in me... my phone is nokia 6288 i know this is not the forum for phone settings but please help me...

          R Offline
          R Offline
          Roger Stoltz
          wrote on last edited by
          #4

          When using the "Bluetooth Serial Port Profile" there are three main issues to overcome: 1. Setting up the Bluetooth connection to make it behave like a serial device and 2. Setting up the serial device 3. Read and write from/to the port When you say you have tried to open the serial device, but it presents itself as "PIM" instead of a serial device, you probably have a problem with the first issue above. To verify this you can try using the Hyperterminal to send and receive the exact same data with the same port settings as you're trying to do in your code. If that test succeeds you can start worrying about your code. Regarding programming for serial communication I have done it quite a lot, but I've never used ::SetCommMask() or ::WaitCommEvent(). I use overlapped mode and I have one worker thread that reads data and another worker thread that sends data. The data to be sent is put in a queue from which the sending thread reads it and calls ::WriteFile(). A semaphore that the sending thread is waiting on with ::WaitForMultipleObjects() is released when a new block of data is added to the send queue. The reading thread issues a read request with a call to ::ReadFile(). Since overlapped mode is used the call will return immediately and the reading thread can wait on the event in the OVERLAPPED struct that will get signalled when data arrives on the port. The read data will be put in a queue and a user defined windows message gets posted to the main thread in order to inform it about the newly received data so it can take proper action. Of course the queues are protected with critical sections. Joseph Newcomer has written a good article about serial port programming here[^]. He uses a slightly different approach than I've described above, but the mind set is very much the same.

          "It's supposed to be hard, otherwise anybody could do it!" - selfquote
          "High speed never compensates for wrong direction!" - unknown

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups