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
strlen() is detecting the zero byte. In this case that isn't what you need, so don't use strlen. Just put the number of bytes that should be sent.
Thank you for the quick response.

I tryed with a fixed number of bytes and fails too :_(


WriteFile(handlerP,bufferPort,9,&bytesRW,NULL);
Last edited on
I tryed with a fixed number of bytes and fails too

Does it fail in precisely the same way, or is there some other error?

I haven't tried it with the serial port, I used the WriteFile() function with a disk file and it worked correctly, as far as I can tell.

This was my code:
1
2
3
4
5
    char text[] = { '\x81', '\x91', '\x00', '\xA1', '\xED' };
    
    DWORD count;
    
    WriteFile(hnd, text, 5, &count, NULL);

In a complete program it looked like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <fstream>
#include <windows.h>

void decode(const char * fname);

int main() 
{             
    const char * fname = "tempjunk.txt";

    HANDLE hnd = CreateFileA(fname, GENERIC_WRITE, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);        
        
        
    //           10000001 10010001 0000000 10100001 11101101.     
    char text[] = { '\x81', '\x91', '\x00', '\xA1', '\xED' };
    
    DWORD count;
    
    WriteFile(hnd, text, 5, &count, NULL);

    CloseHandle(hnd);
    
    
    std::cout << "count = " << count << '\n'; 
    std::cout << "text size = " << sizeof(text) / sizeof(text[0])  << '\n';  
    

    decode(fname);  // display file contents in binary format
}

void showBinary(char ch)
{
    for (int i=7; i>=0; --i)
        std::cout << bool(ch & (1 << i));
}

void decode(const char * fname)
{
    std::ifstream fin(fname, std::ios::binary);
    char ch;
    while (fin.get(ch))
    {
        showBinary(ch);
        std::cout << '\t';
    }
}


Output:
count = 5
text size = 5
10000001        10010001        00000000        10100001        11101101
Topic archived. No new replies allowed.