Thread: Beginner!!! Please help

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The way I would do it would be to package up the entire frame into an array of bytes, tracking the length of it. Then send the package using a for-loop that calls vSendData for each byte.

    If you have no idea how to do that, then I suggest you get your C book out.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Nov 2008
    Location
    Leicester, UK
    Posts
    35
    I agree with you. Thats what I am trying to do.

    I will get back to you.

    Thanks a lot for your help.

  3. #18
    Registered User
    Join Date
    Nov 2008
    Location
    Leicester, UK
    Posts
    35
    Hello again everyone !!!

    Following is the frame which I am transmitting (please refer to previous posts for details). Can anyone tell me if it is correct?


    Code:
    char send[6] = {0x53,0x4E,0x32,0x46,0x48,0x50};
    	for (i=0;i<6;i++)
    			{
    				data = send[i];
    				ASC1_vSendData (data);
    			}
    I appreciate your help.

    Thank you.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would do:
    Code:
    char send[6] = {'S',0x4E,0x32,'G','H', 'P'};
    Versus your previous post, you are missing an 0xFE at the third byte, and I'm not sure you want an ASCII '2' or the value 0x02 in the length field - if it was my protocol, I would probably use a binary value, as that allows more than 1-9 bytes of message - but if the message is always very short, the ascii value is better, I suppose.

    I also wouldn't use an extra variable before passing it to send. But that is much elss of an issue.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Location
    Leicester, UK
    Posts
    35
    If I have to send two bytes, can i use '0x02' as the length value and still send
    '0x46' and '0x48 as the actual bytes?

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by reemaambekar View Post
    If I have to send two bytes, can i use '0x02' as the length value and still send
    '0x46' and '0x48 as the actual bytes?
    I have no idea - the length is part of the protocol, and I do not know what your protocol specifies. It just "feels wrong" in relation to other protocols that I've seen. But it's not unheard of to send the size as a printable character - it's just limiting the number of bytes you can send to 0..9 [for one digit].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Registered User
    Join Date
    Nov 2008
    Location
    Leicester, UK
    Posts
    35
    Please bear with me on this since I am fairly new to all this.

    Instead of using an array, can I use a structure which will allow me to define different data types the way i want?

  8. #23
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by reemaambekar View Post
    Please bear with me on this since I am fairly new to all this.

    Instead of using an array, can I use a structure which will allow me to define different data types the way i want?
    If you do that and you can only transmit one byte at a time, you won't be able to use the for loop because you will have to refer to the struct members individually, which they will all be (the same as) chars anyway (so what's the point?) You'll have gone from something appropriately simple to something with unnecessary complications.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #24
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Aside from MK27's answer, it is also difficult to have a variable payload, since the payload is in the middle of the packet, not at the end.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #25
    Registered User
    Join Date
    Nov 2008
    Location
    Leicester, UK
    Posts
    35
    Thanks both of you.

    As matsp said before about using a binary number in the length field, how can i declare a binary number in the array?

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by reemaambekar View Post
    Thanks both of you.

    As matsp said before about using a binary number in the length field, how can i declare a binary number in the array?
    All numbers in computers are BINARY. What I mean by that is the value 0x02 or 2 (whichever you prefer) - it's the same value, rather than '2' or 0x32 (again, you choose which you use - as long as we are dealing with ASCII/ANSI compliant character sets, they are identical).

    C does not support "binary" in source code as standard. A few compilers, particularly for embedded use have extensions, so you can write something like 0b0000010 to describe the value 2 - but it's still stored exactly the same way whether you write 2, 0x02 or any other format that the compiler supports.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    Registered User
    Join Date
    Nov 2008
    Location
    Leicester, UK
    Posts
    35
    Quote Originally Posted by matsp View Post
    All numbers in computers are BINARY. What I mean by that is the value 0x02 or 2 (whichever you prefer) - it's the same value, rather than '2' or 0x32 (again, you choose which you use - as long as we are dealing with ASCII/ANSI compliant character sets, they are identical).

    C does not support "binary" in source code as standard. A few compilers, particularly for embedded use have extensions, so you can write something like 0b0000010 to describe the value 2 - but it's still stored exactly the same way whether you write 2, 0x02 or any other format that the compiler supports.

    --
    Mats
    Yes, I know that. Thats the reason I got confused when u said u would have used a binary number.

  13. #28
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by reemaambekar View Post
    Yes, I know that. Thats the reason I got confused when u said u would have used a binary number.
    Ok, I meant "binary" as opposed to "text" or "ascii digits" - e.g. The number 12 would be binary 00001100, whilst "12" would be the digits 00110001, 00110010 ('1', '2').

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #29
    Registered User
    Join Date
    Nov 2008
    Location
    Leicester, UK
    Posts
    35
    Quote Originally Posted by matsp View Post
    Ok, I meant "binary" as opposed to "text" or "ascii digits" - e.g. The number 12 would be binary 00001100, whilst "12" would be the digits 00110001, 00110010 ('1', '2').

    --
    Mats
    So, in my frame how can I declare the number of bytes? for eg, say I want to send 2 bytes.

  15. #30
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would probably write a function that takes the payload and length, along with a buffer (and perhaps buffer-size so you can DETECT that your payload and framing doesn't fit, if the buffer isn't big enough).

    Then just fill in the start of the buffer, use a loop to copy the payload, and then mark the end with a 'P'. Something like this would be the calling code:
    Code:
       char buffer[256];
    
       const char *payload = "abcd";
       BuildFrame(payload, strlen(payload), buffer, sizeof(buffer));
    I'm leaving it to you to define the function itself.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM