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

    Windows (COM), Send bytes to serial port

    Hello everyone, I'm trying to make a Windows program that sends data to a microcontroller, through serial port (usb emulating com).

    Till now, I made it with ASCII, but I have to do a job with a classmate, that said me, that I don't have to do that; That I have to send directly to the serial port, the bytes that He needs to use (He program the microcontroller, I, the Windows interface).

    I always used writefile with ASCII, in the form:

    WriteFile(handlePort,bufferPort,(strlen(buffer_puerto)),&nBytes,NULL);

    I have to send a byte chain, like 10000001 10010001 0000000 10100001 11101101.

    The problem is, that when Writefile detects the third byte 00000000, interprets it like a null character '\0' and do not send more bytes.

    Please, could anyone help me. Is there any way to send all bytes (after the third 00000000) without lost information?

    Is There another function apart writefile that can it make that?
    How should I do it?

    Thanks in advance.
    Greetings

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Windows (COM), Send bytes to serial port

    Is there any way to send all bytes (after the third 00000000) without lost information?
    Yes, just set the third parameter to the number of bytes to write. The issue is that strlen() determines the number of chars upto the null char - so if the data to write includes a null char(s) then strlen() can't be used to determine the number of bytes.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Windows (COM), Send bytes to serial port

    Your problem is the strlen function which treats the NULL as the end_of_string.
    You have to use something other to calculate the byte length of the buffer to send.
    Victor Nijegorodov

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