How to send ASCII codes to a serial port and read as bytes or characters? - C ++

1

In the following code I can send characters and read the bytes of these. But what should I add, delete or modify so that only the codes are sent? ASCII and to read the bytes without having to add the line char bytes_to_send[] = "hello"; ?

#include <windows.h>
#include <stdio.h>



int main()
{

    char bytes_to_send[] = "hello";
    bytes_to_send[0] = 104;
    bytes_to_send[1] = 101;
    bytes_to_send[2] = 108;
    bytes_to_send[3] = 108;
    bytes_to_send[4] = 111;


    HANDLE hSerial;
    DCB dcbSerialParams = { 0 };
    COMMTIMEOUTS timeouts = { 0 };


    fprintf(stderr, "Opening serial port...");
    hSerial = CreateFile(
        "\\.\COM3", GENERIC_READ | GENERIC_WRITE, 0, NULL,
        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hSerial == INVALID_HANDLE_VALUE)
    {
        fprintf(stderr, "Error\n");
        return 1;
    }
    else fprintf(stderr, "OK\n");


    dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
    if (GetCommState(hSerial, &dcbSerialParams) == 0)
    {
        fprintf(stderr, "Error getting device state\n");
        CloseHandle(hSerial);
        return 1;
    }

    dcbSerialParams.BaudRate = CBR_38400;
    dcbSerialParams.ByteSize = 8;
    dcbSerialParams.StopBits = ONESTOPBIT;
    dcbSerialParams.Parity = NOPARITY;
    if (SetCommState(hSerial, &dcbSerialParams) == 0)
    {
        fprintf(stderr, "Error setting device parameters\n");
        CloseHandle(hSerial);
        return 1;
    }


    timeouts.ReadIntervalTimeout = 50;
    timeouts.ReadTotalTimeoutConstant = 50;
    timeouts.ReadTotalTimeoutMultiplier = 10;
    timeouts.WriteTotalTimeoutConstant = 50;
    timeouts.WriteTotalTimeoutMultiplier = 10;
    if (SetCommTimeouts(hSerial, &timeouts) == 0)
    {
        fprintf(stderr, "Error setting timeouts\n");
        CloseHandle(hSerial);
        return 1;
    }


    DWORD bytes_written, total_bytes_written = 0;
    fprintf(stderr, "Sending bytes...");
    if (!WriteFile(hSerial, bytes_to_send, 5, &bytes_written, NULL))
    {
        fprintf(stderr, "Error\n");
        CloseHandle(hSerial);
        return 1;
    }
    fprintf(stderr, "%d bytes written\n", bytes_written);


    fprintf(stderr, "Closing serial port...");
    if (CloseHandle(hSerial) == 0)
    {
        fprintf(stderr, "Error\n");
        return 1;
    }
    fprintf(stderr, "OK\n");


    return 0;
}
    
asked by Beta 17.07.2017 в 05:15
source

1 answer

3

In C ++ there are several ways to declare an array or, in your case, a buffer:

  • Defined size at compile time. The compiler analyzes the content to be stored in the buffer and calculates the size needed to store it:

    char array[] = "...";
    
  • Explicitly defined size: The compiler does not have to infer the buffer size, you are providing it explicitly:

    char array[200];
    
  • Dynamic memory. It allows you to set the buffer size according to the needs at each moment:

    char* buffer;
    buffer = new char[200];
    
    // ...
    
    delete[] buffer;
    
  • Containers. They add a level of encapsulation and isolation to the previous examples:

    std::array<char,200> buffer; // Tamaño fijo
    buffer.data(); // Acceso al buffer interno
    
    std::vector<char> buffer; // Tamaño variable
    buffer.data(); // Acceso al buffer interno
    

Your code is currently using the first example. The quickest thing is to replace it with the second one and, without touching anything, it is expected that everything works correctly:

char bytes_to_send[6] = {}; // Se inicializan los bytes a 0
bytes_to_send[0] = 104;
bytes_to_send[1] = 101;
bytes_to_send[2] = 108;
bytes_to_send[3] = 108;
bytes_to_send[4] = 111;

On the other hand, think that the assignment you make of the bytes is manual ... it would be more convenient to use copy functions to avoid silly mistakes:

char bytes_to_send[6];
strcpy(bytes_to_send,"hello");

// Esto ya sobra
// bytes_to_send[0] = 104;
// bytes_to_send[1] = 101;
// bytes_to_send[2] = 108;
// bytes_to_send[3] = 108;
// bytes_to_send[4] = 111;
    
answered by 17.07.2017 в 09:11