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

Thread: Sending Byte to serial port

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

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    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.
    Wrong answer!
    Your problem is you have no idea how to call some very simple function with very good described parameter list!
    so why do you have such a problem?
    Victor Nijegorodov

  2. #32
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,843

    Re: Sending Byte to serial port

    This is an example of using Stuffdata.

    Code:
    const char text[] = "abcdefghijklmnopqrstuvwxyz";
    char codify[200];
    
    StuffData(text, (sizeof(text)/sizeof(text[0])) - 1, codify);
    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...
    You really, really cannot pick this up as you go along without having a very good grounding in the basics of the c++ language, which is obvious you do not have. You need to start with the very basics and progress one step at a time. You are trying to undertake the triple jump without knowing how to run.

  3. #33
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    OK thank you very much! Also if the problem is to send this packet: 0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00. Not an Array of char.

  4. #34
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,843

    Re: Sending Byte to serial port

    The definiton for StuffData is

    Code:
    void StuffData(const unsigned char *ptr, unsigned long length, unsigned char *dst)
    This says that ptr is a pointer to memory containing unsigned characters and that the characters pointed to cannot be changed. An area of memory containing unsigned characters is a character array (or an array of chars). So to use StuffData the first parameter has to be a pointer to an array of chars. So before you call the function StuffData, your program has to put into a char array the characters for StuffData. You cannot get away from not using a character array! This can be either a fixed size array (as in my previous example) or it can be a dynamically allocated memory (use new). However the character array is created, once it has been you need to populate it with the characters for StuffData.

    So from your example, you want to use 11 characters. So you need a character array that contains these characters set up before you call StuffData.

    Code:
    const unsigned char packet[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
    unsigned char codify[200];
    StuffData(packet, sizeof(packet) / sizeof(packet[0]), codify);
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #35
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Ok I thank you very much.

    the follow code ran with success, but I'd like to see the encoding packet. I have thought to use printf, is it correct?

    Code:
    	 int main()
    	{
    		const unsigned char text[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
    	unsigned char codify[200];
    	
    	StuffData(text, (sizeof(text)/sizeof(text[0])), codify);
    	
    	//printf(...);
    	return 0;
    	 }

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

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    Ok I thank you very much.

    the follow code ran with success, but I'd like to see the encoding packet. I have thought to use printf, is it correct?
    I guess it would be, at least, much faster to try rather than write this post and then wait for replies!
    Victor Nijegorodov

  7. #37
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    I tried before to write!!! but it doesn't work (I have errors when it compiles or i read very strange characters) and for this reason I want to know if it is correct...

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

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    I tried before to write!!! but it doesn't work (I have errors when it compiles or i read very strange characters)
    Please, show your code together with the errors.
    Victor Nijegorodov

  9. #39
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    This code show strange characters without errors:

    int main()
    {
    const unsigned char text[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
    const int p= sizeof(text);
    unsigned char codify[p];

    StuffData(text, (sizeof(text)/sizeof(text[0])), codify);

    printf("%s", codify);
    return 0;
    }

  10. #40
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    Pardon...this is the code:

    Code:
    int main()
    	{
    		const unsigned char text[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
    		unsigned char codify[200];
    	
    	StuffData(text, (sizeof(text)/sizeof(text[0])), codify);
    	
    	printf("%s", codify);
    	return 0;
    	 }

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

    Re: Sending Byte to serial port

    Again:
    Quote Originally Posted by VictorN View Post
    Please, show your code together with the errors.
    BTW, why do you use format specification "%s" while your buffer contains a byte sequence, not a text?
    Victor Nijegorodov

  12. #42
    Join Date
    Feb 2013
    Posts
    34

    Re: Sending Byte to serial port

    there aren't any errors with this code.

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

    Re: Sending Byte to serial port

    Quote Originally Posted by Bfrancesco View Post
    there aren't any errors with this code.

    Then what did your previous post mean:
    Quote Originally Posted by Bfrancesco View Post
    I tried before to write!!! but it doesn't work (I have errors when it compiles or i read very strange characters) and for this reason I want to know if it is correct...


    So do you have some errors (compile, link, run-time) or not?
    Victor Nijegorodov

  14. #44
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,843

    Re: Sending Byte to serial port

    The destination characters returned by StuffData in dst are NOT a c-style string and are NOT guaranteed to be NULL terminated. So using printf (or cout) to try to display the contents of dst is a non-starter. Have you actually looked at what StuffData does? It takes an array of char and a length and returns a codified char array where the first char is the number of bytes (including itself) followed by the chars. If the length of the bytes exceeds 255 then a new chuck is started again with the first byte as the length byte. So "qwerty" gets codified as 0x07 'q' 'w' 'e' 'r' 't' 'y'. 0x00 is treated specially and just gets translated as a new chuck of length 1. So after using StuffData, you pass dst to whatever function you use to actually send the data.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    Re: Sending Byte to serial port

    Quote Originally Posted by 2kaud View Post
    The destination characters returned by StuffData in dst are NOT a c-style string and are NOT guaranteed to be NULL terminated. So using printf (or cout) to try to display the contents of dst is a non-starter.
    I'd say neither the initial buffer nor the destination one is "a c-style string". Both are just byte sequences with some predefined length.
    And there is nothing wrong to use printf (or cout) to output their contents. Just use some proper format specification (like %X, "%08X", "%d" or similar) and do output in a loop for each element in the buffer!
    Victor Nijegorodov

Page 3 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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center