Sending Byte to serial port - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 5 FirstFirst 12345 LastLast
Results 16 to 30 of 73

Thread: Sending Byte to serial port

  1. #16
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    @VictorN: I read that error when i compile only the class in http://www.naughter.com/serialport.html.
    The errors/warnings you showed are NOT the "compile" ones. They are runtime errors/warnings.
    Victor Nijegorodov

  2. #17
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    yes, I'm sorry

  3. #18
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    The connection between bluetooth device and PC is working.
    In the code of http://www.naughter.com/serialport.html there is the app.cpp code. When it runs the port is opened and the connection is made but after some seconds appears this error:

    CSerialPort::Write, Failed in call to WriteFile, Error:997


    This is a part of app.cpp code:


    PHP Code:
     CSerialPort port2;
        
    port2.Open(69600CSerialPort::NoParity8CSerialPort::OneStopBitCSerialPort::XonXoffFlowControlTRUE);

        
    CEvent event(FALSETRUE);
        
    OVERLAPPED overlapped;
        
    memset(&overlapped0sizeof(overlapped));
        
    overlapped.hEvent event;
        try
        {
          
    port2.Write(pBuf10000overlapped);
        }
        catch(
    CSerialExceptionpEx)
        {
          if (
    pEx->m_dwError == ERROR_IO_PENDING)
          {
            
    DWORD dwBytesTransferred 0;
            
    port2.GetOverlappedResult(overlappeddwBytesTransferredTRUE);
            
    pEx->Delete();
          }
          else
          {
            
    DWORD dwError pEx->m_dwError

    Instead this is the part of code od SerialPort.cpp of error message:

    PHP Code:
    void CSerialPort::Write(const voidlpBufDWORD dwCountOVERLAPPEDoverlappedDWORDpBytesWritten)
    {
      
    //Validate our parameters
      
    ASSERT(IsOpen());

      
    DWORD dwBytesWritten 0;
      
    BOOL bSuccess WriteFile(m_hCommlpBufdwCount, &dwBytesWritten, &overlapped);
      if (!
    bSuccess)
      {
        
    DWORD dwLastError GetLastError();
        
    TRACE(_T("CSerialPort::Write, Failed in call to WriteFile, Error:%d\n"), dwLastError);
        
    ThrowSerialException(dwLastError);
      } 

  4. #19
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Sending Byte to serial port

    ERROR_IO_PENDING (error 997) just means that Write operation is not completed yet! From MSDN:
    ... GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation.
    Please, read about Serial Communications in MSDN:
    http://msdn.microsoft.com/en-us/library/ff802693.aspx
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Victor Nijegorodov

  5. #20
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    thank you very much Victor for your help! I'm a new programmer, but i'm improving and the communication between PC and device has been established also with C++ language. I discovered that I have problems with writefile becouse the device bluetooth use Cobs codification and now I'm trying to understand the following Cobs code:

    PHP Code:
    /*
     * StuffData byte stuffs "length" bytes of
     * data at the location pointed to by "ptr",
     * writing the output to the location pointed
     * to by "dst".
     */

    #define FinishBlock(X) \
        
    (*code_ptr = (X),  \
        
    code_ptr dst++,  \
        
    code 0x01)

    void StuffData(const unsigned char *ptr,
    unsigned long lengthunsigned char *dst)
        {
        const 
    unsigned char *end ptr length;
        
    unsigned char *code_ptr dst++;
        
    unsigned char code 0x01;

        while (
    ptr end)
            {
        if (*
    ptr == 0FinishBlock(code);
        else
            {
            *
    dst++ = *ptr;
            
    code++;
            if (
    code == 0xFFFinishBlock(code);
            }
        
    ptr++;
        }
        
    FinishBlock(code);
        } 
    this code is used to Cobs codification, but I don't understand how i can use it to codify my array of bytes.

  6. #21
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Sending Byte to serial port

    Sorry, but I have no idea what these FinishBlock macro and StuffData function are for and where they come from...

    BTW, you should use Code tags (using # toolbar button) rather than PHP or HTML ones.
    Besides, your code is very bad formatted: messed indentations and more than one statement on the same line make reading/understanding this code very difficult!
    Victor Nijegorodov

  7. #22
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    I copied that from here: http://www.stuartcheshire.org/papers/COBSforSIGCOMM/ (at the end of page)

  8. #23
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Sending Byte to serial port

    Well, then try to encode your byte sequence using this code according to this comments:
    Code:
    /* 
     * StuffData byte stuffs "length" bytes of 
     * data at the location pointed to by "ptr", 
     * writing the output to the location pointed 
     * to by "dst". 
     */
    Then send theough the port the encoded byte sequence pointed to by "dst"
    Victor Nijegorodov

  9. #24
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Yes, but i don't understand well how i can do it.

  10. #25
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Sending Byte to serial port

    You have to declare a destination buffer long enough to contain your encoded byte sequence, then call this StuffData function passing in your original byte array, its length and destination buffer.
    Then send what destination buffer will get after this function returns to the serial port.
    Victor Nijegorodov

  11. #26
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    I have some problems...I'm a new programmer and all codes are not simple for me now. Are there some example code to understand how i can do it?
    Thank you and I'm sorry.

  12. #27
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    I have some problems...I'm a new programmer and all codes are not simple for me now. Are there some example code to understand how i can do it?
    Thank you and I'm sorry.
    Well, did you read some books about progamming with C++ for Windows?
    Did you try some simple sample applications like "Hello Worlds"?
    Some others a little more complicated?

    Note, that you cannot from the level "zero" jump to a level for programming serial port communications...
    So...
    Victor Nijegorodov

  13. #28
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    I'm reading and I'm trying many applications.

  14. #29
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    I'm reading and I'm trying many applications.
    And???
    Victor Nijegorodov

  15. #30
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    I'm improving and now i'm trying and I'd like to estabilish a connection between 2 devices also if it's not very simple for me.

Page 2 of 5 FirstFirst 12345 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center