Thread: WriteFile question???

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    56

    WriteFile question???

    I have a question regarding the WriteFile in serial communication. I am trying to send data from a hex file to a microcontroller using a VARIANT command, but I get an error from the WriteFile saying "actual parameter is not a pointer : parameter 2." How can i fix this or is there another function where i can output to the serial command.

    Example:
    VARIANT vOut;

    //vOut is filled with data

    WriteFile(hCom,vOut,65535,&iBytesWritten,NULL);

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Variant (or similar) is not a standard C type. Most common forms of such things I see are as a container for more than one type of data (eg if you store an int in a variant, you must retrieve it as an int).

    WriteFile (in the win32 SDK) does not include any built in intelligence about variant types. IOW, it simply sees a pointer to raw data as the second argument, and outputs that.

    In the end, that means you need to interpret the content of vOut before passing it to WriteFile(). For example (in pseudo-code);
    Code:
    if (vOut_contains_string)
       WriteFile(hCom, vOut.string_data(), vOut.string_length(), &iBytesWritten, NULL);

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    What i was trying to do is that I am trying to compress the hex file and send it 128 bytes at a time.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    In C terms

    unsigned char vOut[256]; // or however many you want
    WriteFile(hCom,vOut,sizeof(vOut),&iBytesWritten,NU LL);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Can anybody explain what this does and how can i convert it to C?

    Code:
         vOut.vt = VT_ARRAY|VT_UI1;                   // Set to array of binary mode for sending
         vOut.parray = SafeArrayCreateVector(VT_UI1,0,134);// Create an array of 128 data + 6 misc bytes
         ucArray=(unsigned char*)vOut.parray->pvData; // Cast the pointer to an array pointer
         ucArray[0]=HEADER;                           // Head of package
         ucArray[4]=0;                                // Meanwhile the package type will be data
         
         while (curr->next) {                         // Move thru whole prepared package link list
              curr=curr->next;
    
              vOut.parray->rgsabound->cElements=curr->len+6;// Send only amount of data in that package
              ucArray[1]=curr->len;                   // Using casted array to store len and addr
              ucArray[2]=curr->start/0x100;
              ucArray[3]=curr->start&0xFF;
              memcpy(&ucArray[5],&memCont[curr->start],curr->len);// Copy data to sending array
              ucArray[curr->len+5]=curr->chksum;
    
              pCommCtrl->SetOutput(vOut);             // The actual sending (pass to it variant structure)
    
              do {
                   while (!pCommCtrl->GetInBufferCount());// Wait for receiving
                   ch=pCommCtrl->GetInput();
              } while (ch!=ACKNOWLEDGE);              // Wait for acknowledge character
         }
    
         vOut.parray->rgsabound->cElements=6;         // Same thing send the last package (termination)
         ucArray[1]=ucArray[2]=ucArray[3]=0x00;
         ucArray[4]=0x01;
         ucArray[5]=0xFF;
    
         pCommCtrl->SetOutput(vOut);

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    All that code is valid C.

    Tell us what part of it you can't understand, and someone might try to help.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > pCommCtrl->SetOutput(vOut); // The actual sending (pass to it variant structure)
    > while (!pCommCtrl->GetInBufferCount());// Wait for receiving
    > ch=pCommCtrl->GetInput();
    > pCommCtrl->SetOutput(vOut);

    These lines of code are all calling functions which are contained within an object called pCommCtrl.

    So you'd have to take these three functions (SetOutput(), GetInBufferCount(), and GetInput()) and move them out of the declaration for pCommCtrl's class type.

    You'd have to change the functions so that the first parameter passed would be pCommCtrl. In C++ you don't have to do this, as the functions are already part of pCommCtrl.

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Here is what I did, will it give me the same result? I just modified the input and output setting.

    Code:
         vOut.vt = VT_ARRAY|VT_UI1;                   // Set to array of binary mode for sending
         vOut.parray = SafeArrayCreateVector(VT_UI1,0,134);// Create an array of 128 data + 6 misc bytes
         ucArray=(unsigned char*)vOut.parray->pvData; // Cast the pointer to an array pointer
         ucArray[0]=HEADER;                           // Head of package
         ucArray[4]=0;                                // Meanwhile the package type will be data
         
         while (curr->next) {                         // Move thru whole prepared package link list
              curr=curr->next;
    
              vOut.parray->rgsabound->cElements=curr->len+6;// Send only amount of data in that package
              ucArray[1]=curr->len;                   // Using casted array to store len and addr
              ucArray[2]=curr->start/0x100;
              ucArray[3]=curr->start&0xFF;
              memcpy(&ucArray[5],&memCont[curr->start],curr->len);// Copy data to sending array
              ucArray[curr->len+5]=curr->chksum;
    
              WriteFile(hCom,vOut.parray,sizeof(vOut.parray),&iBytesWritten,NULL);
    //		  pCommCtrl->SetOutput(vOut);             // The actual sending (pass to it variant structure)
    
              do {
                   while (!ReadFile(hCom,&sBuffer,20,&iBytesRead,NULL));// Wait for receiving
    //               ch=pCommCtrl->GetInput();
    //				ReadFile(hCom,&ch,20,&iBytesRead,NULL);
              }    while (ch!=ACKNOWLEDGE);              // Wait for acknowledge character
         }
    
         vOut.parray->rgsabound->cElements=6;         // Same thing send the last package (termination)
         ucArray[1]=ucArray[2]=ucArray[3]=0x00;
         ucArray[4]=0x01;
         ucArray[5]=0xFF;
    
         WriteFile(hCom, vOut.parray,sizeof(vOut.parray),&iBytesWritten,NULL);
    //   pCommCtrl->SetOutput(vOut);
    
         SafeArrayDestroy(vOut.parray);
    Last edited by NewGuy100; 07-29-2005 at 08:08 AM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Why do you keep passing 65535 as the amount of data to output?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Actually i changed it to the sizeof the array. I really want to test it out but I am waiting for the cable to hook up the PC to the microcontoller and I just wanted to know if i was in the right path.
    Last edited by NewGuy100; 07-29-2005 at 08:12 AM.

  11. #11
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Do you always want to write the entire array? You use up every element every time?

    ucArray=(unsigned char*)vOut.parray->pvData; // Cast the pointer to an array pointer

    I'm not sure what you think this accomplishes but unsigned char* is not an 'array pointer'. it is just a pointer. Casting is bad, if you have to do it you are most likely doing something wrong unless you know what you are doing.

  12. #12
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    i want to keep sending 128 bytes to different pages in the memory

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > WriteFile(hCom,vOut.parray,sizeof(vOut.parray),&iB ytesWritten,NULL);
    I would think here you'd want:
    Code:
              WriteFile(hCom,vOut.parray,134,&iBytesWritten,NULL);
    As sizeof(vOut.parry) is probably the size of a pointer, not the size of the array.

    As far as whether is will work, what you have looks reasonable, but I've never tried it, so I can't comment.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Thanks, I hope it works!

  15. #15
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    Got another question: Can anyone tell me where I can find a good example on serial communication for C. I executed one of the functions, Erase function, and it returned back a message saying that it erased succesfully. The weird thing is that I haven't turned on the power. It should have at least displayed "Cannot Erase." Help!!

    HERE is my erase code:

    Code:
     DWORD iBytesWritten;
     DWORD iBytesRead;
     BOOL bWriteRC;
     BOOL bReadRC;
     char *sBuffer;
    
     bWriteRC = WriteFile(a,"E\r",2,&iBytesWritten,NULL);
     if (bWriteRC){
        ;    //do nothing
     }
     else
      return "Cannot Erase the Flash";
     bReadRC = ReadFile(a,&sBuffer,20,&iBytesRead,NULL);
     if (bReadRC){
      return sBuffer;    //do nothing
     }
     else
      return "Cannot Read the Flash";
    Last edited by NewGuy100; 07-30-2005 at 05:33 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM