32-bit Windows functions

1

I have the following code, which compiles me as is. The problem is that I do not understand why he does not receive me or send data from one computer to another. I would also like to know why I can not configure the speed of the data, I have tried but I do not understand what the error is.

#include "stdafx.h"
#include "windows.h"
#include "conio.h"

int main()
{
    char envio[100] = {};
    char recibe[100] = {};
    char tamaño;
    DWORD Bescrito = 1, Brecibido=1;

    HANDLE dato = CreateFile(L"COM3",
        GENERIC_READ | GENERIC_WRITE,
        0,
        NULL,
        OPEN_EXISTING,
        0,
        NULL);
    //DCB config_;

    //if (GetCommState(dato, &config_) == 0)
    //{
    //  //AfxMessageBox("Get configuration port has problem.");
    //  printf("Puerto Error");
    //  return Bescrito;
    //}

    //// Assign user parameter.
    //config_.BaudRate = 115200;  // Specify buad rate of communicaiton.
    //config_.StopBits = 1;  // Specify stopbit of communication.
    //config_.Parity = 0;      // Specify parity of communication.
    //config_.ByteSize = 8;  // Specify  byte of size of communication.

    //if (SetCommState(dato, &config_) == 0)
    //{
    //  printf("Configuracion Puerto frito");

    //}
    if (dato != 0) {

        int op;

        printf("puerto abierto");

        op = _getch();
        do
        {
            //escriura

            printf("\nescriba: ");
            scanf_s("%s", envio, 100);
            tamaño = strlen(envio);
            WriteFile(dato, envio, tamaño, &Bescrito, NULL);

            Sleep(2);

            //lectura

            printf("\nleyendo: \n");
            do {

                ReadFile(dato, recibe, 1, &Brecibido, NULL);
                if (Brecibido == 1)
                {

                    printf("%s", recibe[0]);
                }
            } while (!_kbhit());
            _getch();

        }while (op != 27);

    }
    else if (dato==0)
    {
        printf("error de apertura.");
        _getch();
    }

    CloseHandle;

    return 0;
}
    
asked by Edwin 15.11.2016 в 17:29
source

1 answer

0

I do not know if it will be the problem, since I can not test your code, but it does not seem too correct to define the configuration using literals with constants for this purpose that avoid introducing silly errors:

config_.BaudRate = CBR_115200; // equivalente a 115200;
config_.StopBits = ONE5STOPBITS; // equivalente a 1 -> nota que la parada es 1.5
config_.Parity = NOPARITY; // equivalente a 0
config_.ByteSize = 8;  // Specify  byte of size of communication.

On the other hand, the Microsoft examples recommend initializing the DBC structure before reading the port configuration, which you are ignoring:

SecureZeroMemory(&config_, sizeof(DCB));
config_.DCBlength = sizeof(DCB);

if (GetCommState(dato, &config_) == 0)
// ...

Otherwise I do not see anything strange but, as I say, I can not test the code.

    
answered by 15.11.2016 в 17:47