Thread: Serial Port - Receive Packet

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, you need to learn how to write code for yourself.

    You are comparing a local array with NULL - when do you expect your local array to be located at address ZERO? I'd expect your code to not work at all if your stack gives you address zero!!!

    d_len is never set to a value, so it could contain exactly any value (it will most likely be the same value until you change the code in some way, but it's not specified anywhere). Again you are using NULL - d_len is not a pointer. NULL is the value defined as "invalid pointer value" - it is not the same as zero or empty.

    --
    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
    Apr 2009
    Posts
    37
    Ok, I have tried a slightly different approach. This way it says that I am receiving 1 byte from the port. But I am having trouble with casting the receive buffer as a COMMAND_PACKET. Error: Conversion to non-scalar type requested. Highlighted in Yellow

    Here is my latest attempt
    Code:
    int receive_packet(COMMAND_PACKET *packet)
    {
         DWORD dwRetVal;
         DWORD buflen;
         LPVOID ReplyBuffer;
         unsigned char buf[20];
         buflen = sizeof(incoming_command);
         ReplyBuffer = (VOID*) malloc(buflen);
         if(ReplyBuffer == NULL) {
           printf("ReplyBuffer: Error");
           return 1;
           }
         memset(ReplyBuffer, 0, buflen);
         incoming_command = (COMMAND_PACKET *)ReplyBuffer;
         dwRetVal = ReadFile(handle,
                        buf,
                        1,
                        &buflen,
                        NULL);
         if(dwRetVal != 0) {
                     printf("Received %ld bytes\n", dwRetVal);
                     printf("Received Packet: %s\n", buf[1]);
                     }
    
    }
    in the code incoming_command is defined as a COMMAND_PACKET

    Do you see why I want to cast ReplyBuffer as a COMMAND_PACKET, so that I can read it?
    How might this be acomplished?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What type is incomming_command?

    Why are you using malloc and LPVOID?

    Again, I get the feeling your are only guessing what you are doing, not actually UNDERSTANDING what the code is doing.

    --
    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.

  4. #19
    Registered User
    Join Date
    Apr 2009
    Posts
    37
    As I stated incoming_comand is of type COMMAND_PACKET

    Code:
    typedef struct
      {
      ubyte
        command;
      ubyte
        data_length;
      ubyte
        data[MAX_DATA_LENGTH];
      WORD_UNION
        CRC;
      }COMMAND_PACKET;
    I just want to know how I would over come the Error: Conversion to non-scalar type requested. And at the same time be able to print to screen what is stored in the received buffer.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you are casting a pointer to void to a pointer to COMMAND_PACKET, which you then try to assign to a structure - that's not going to work.

    --
    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.

  6. #21
    Registered User
    Join Date
    Apr 2009
    Posts
    37
    Is it essential to have a process running that is constantly listening for bytes to read in the port?

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by samuelmoneill View Post
    Is it essential to have a process running that is constantly listening for bytes to read in the port?
    No, the serial port (in Windows at least) will be buffered, so you can leave it for a little bit of time (half a second or more, I'd say, even if there's a fair bit of data flowing in). Obviously if something is sending data like crazy to the serial port, it will eventually overflow and loose data. But as long as you don't leave it for too long, it should be fine.

    --
    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. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Send and receive through serial Port
    By overspray in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2004, 04:15 PM
  5. DOS, Serial, and Touch Screen
    By jon_nc17 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-08-2003, 04:59 PM