Thread: WriteFile question???

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

    Finally got it to communicate, but got a small problem.

    I finally got it to communicate but I got another problem. I am trying to do a strcmp function to match the two strings, but it keeps telling me that it doesn't match. I typed it word for word and it should work but it doesn't. I think its because of the size of the string that is being read in. The string read in from the serial port is 17 bytes whereas the exact same string I used to compare it to is 14 bytes. How can I fix this?

  2. #17
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    Well then load up the debugger, and inspect the two strings byte for byte yourself, to help you figure out where it's going wrong!.
    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.

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    One idea: maybe you forgot to terminate one or both of the strings.

    >bWriteRC = WriteFile(a,"E\r",2,&iBytesWritten,NULL);
    For example here you only write 2 bytes, so the string terminator isn't transferred. So you could either change the 2 to a 3, or add the terminator once it gets read on the other end.

    Or, and this might be best, you could use memcmp() instead of strcmp() to compare the two buffers.

  4. #19
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    I am trying to send 128 bytes of data at a time in different pages in flash, but I can only write in 96 bytes on the first page and thats it. After the 96 bytes I get some weird data on the first page and the rest of the other page is empty. I think everything up till the last WriteFile() works. I did a print statement right before the WriteFile () and it does go through each page but for some reason my WriteFile is not sending it. I am prolly not using the VARIANT statement right. Can anybody help me?

    This is what I used.

    WriteFile(hCom,"L\r",2,&iBytesWritten,NULL); // First send the command (ASCII)

    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;

    something = WriteFile(hCom,vOut.parray,134,&iBytesWritten,NULL );
    Last edited by NewGuy100; 08-11-2005 at 03:48 PM.

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >curr->len
    Is curr->len equal to 134?

    >something = WriteFile(hCom,vOut.parray,134,&iBytesWritten,NULL );
    Did you check iBytesWritten to see how many bytes were actually transferred? Also the return value of WriteFile() might tell you something.

    >curr=curr->next;
    I notice you are skipping the first node, which maybe you have a good reason for this.

  6. #21
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    I did print statements and the iBYTESWRITTEN in the WriteFile function say I am printing out 134 bytes out. I don't think that vOut.parray works the way i expect it to. I am not too familiar with the VARIANT statement.

    I am skipping the first node because it is an empty node. It is where I first initialized my link list.

  7. #22
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    What is the difference between using a regular array and using array from the VARIANT statement?

  8. #23
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    VARIANT can contain different things, depending on context. WriteFile has no in-built knowledge of what a VARIANT contains, so the caller needs to interpret what's in the VARIANT and use an output method that is suitable for whatever it contains.

    If you're using a "regular array", odds are you know what it contains and can therefore output it directly in a suitable way.

  9. #24
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    You can define the size of your array, sizeof(VARIANT) is not under your control.

  10. #25
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by orbitz
    You can define the size of your array, sizeof(VARIANT) is not under your control.
    I'm not sure what you intended to say, but what you've said is incorrect.

    The value of sizeof(anything) is implementation defined, so is beyond programmer control (unless that anything is a char or an array of char).

    sizeof(VARIANT) will also be independent of what that variant contains.

  11. #26
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    After much research, I have discovered that to access the data stored in a Variant array one would need to use the SafeArrayAccessData function to access the elements of the array. vOut.parray returns a pointer to the descriptor, which is what i don't want.

  12. #27
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by NewGuy100
    After much research, I have discovered that to access the data stored in a Variant array one would need to use the SafeArrayAccessData function to access the elements of the array.
    No particular surprises there. What that means is that, if you know the VARIANT contains an array, then you need to access that array data from the VARIANT in some sensible manner.
    Quote Originally Posted by NewGuy100
    vOut.parray returns a pointer to the descriptor, which is what i don't want.
    In C++ terms, IF the VARIANT contains a contiguous array, this means that vOut.pArray will be a pointer to the first element in that array.

    There is presumably some other member of the VARIANT, or a function that can be called, which allows working out the number of elements in the array.

  13. #28
    Registered User
    Join Date
    Jul 2005
    Posts
    56
    When I am reading from the port using the ReadFile , I don't know the length of the string. How do I make it automtically adjust the length of the string instead of guessing how long it is?

    ReadFile(hCom,&ch,20,&iBytesRead,NULL);

    As you can see, I guessed the number of bytes to read in is 20. How do I make it read until the end?
    Last edited by NewGuy100; 08-17-2005 at 06:55 AM.

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