CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    31

    Unhappy trying to send hex data over serial port.

    Hi,
    I am using the following code to send hex data over the serial port:

    int str[4] = {0xFF, 0xFF, 0xFF, 0xFF};
    BSTR bstr = SysAllocString((OLECHAR*)str);
    VARIANT var;
    ZeroMemory(&var, sizeof(VARIANT));
    var.vt = VT_BSTR;
    var.bstrVal = bstr;

    Then the WriteFile is: BOOL Write(VARIANT* byteval) { WriteFile(hComm, byteval, sizeof(byteval), &iBytesWritten, NULL); }

    Now, I am using a sniffer to check the data I am sending. For some reason data (08 00 00 00) in hex is getting sent. I have chaged the values in str. But whatever the value is in str, I am getting the same data, 0x08 being sent over the serial port.

    Please help me to solve this problem. I am stuck here for hours!
    Thanks in advance!

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: trying to send hex data over serial port.

    Why all the overhead with VARIANT and BSTR (and even int[4] for that matter).

    Just send the data:

    Code:
    unsigned char str[4]={0xFF,0xFF,0xFF,0xFF};
    DWORD iBytesWritten;
    
    WriteFile(hComm, str, 4, &iBytesWritten, NULL);

  3. #3
    Join Date
    Jun 2009
    Posts
    31

    Re: trying to send hex data over serial port.

    Hi hoxsiew,
    you are so correct!! Why didn't I think of this simple solution!!!
    thanks a lot for your help! The program is now working correctly.
    kajarigd

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured