funciones para comunicación serie (win32)

por | 2011.10.12
HANDLE abrir_port(unsigned char port);
void cerrar_port(HANDLE);
int configurar_port(HANDLE ports,
                    DWORD, /* baud rate */
                    BYTE , /* bits dato */
                    BYTE , /* paridad: 2-EVENPARITY 1-ODDPARITY 0-NOPARITY */
                    BYTE , /* bits stop: 2-TWOSTOPBITS 1-ONE5STOPBITS 0-ONESTOPBIT */
                    unsigned int); /* time out - en ms */
int escribir_char(HANDLE,unsigned char);
int leer_char(HANDLE ports, unsigned char*);
int escribir_chars(HANDLE ports,unsigned char*, unsigned char);
int leer_chars(HANDLE ports, unsigned char*, unsigned char);
int escribir_string(HANDLE ports,unsigned char*);
int leer_string(HANDLE ports, unsigned char*);

seriewin32.c
seriewin32.h

y un ejemplo de uso (main.c) en el siguiente archivo:

zip-ch ejemplo de uso de las funciones para port serie (w32)

Otro ejemplo muy sencillo utilizando wxWidgets (proyecto code::blocks):

zip-ch wxserie

El siguiente ejemplo muestra en pantalla y guarda en un archivo («log.txt») el conjunto de caracteres (terminados en \n) que recibe por el port serie. (En la línea 36 cambiar si se necesario el número de puerto)

/*

 - Abre un port serie.
 - lo configura.
 - recibe mensaje ascii terminado en \n y lo muestra.

*/

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "seriewin32.h"


int main()
{
    FILE *f;

    /* HANDLE para el port serie */
    HANDLE port_serie;

    BYTE numport;
    DWORD baudios;
    BYTE parid;
    BYTE databits;
    BYTE stopbit;

    int timout;

    int numerr = 0;

    char buff[30];
    int pb = 0;
    unsigned char cr;

    numport = 23; /* <=====================================  Cambiar por el número de port que corresponda */
    baudios = 9600;
    databits = 7;
    parid = EVENPARITY; /* 2-EVENPARITY 1-ODDPARITY 0-NOPARITY */
    stopbit = ONESTOPBIT; /* 2-TWOSTOPBITS 1-ONE5STOPBITS 0-ONESTOPBIT */
    timout = 3000;

    port_serie = abrir_port(numport);

    if (port_serie == INVALID_HANDLE_VALUE) /* Verifica si se pudo abrir port */
    {
        printf("No se puede abrir puerto.\n\n");
        printf("Presione <ESC> para salir.\n");

        return(1);
    }

    /* configura port y verifica si hubo error */
	if (configurar_port(port_serie,baudios,databits,parid,stopbit,timout) == 0)
	{
	    printf("error configurando port\n\n");
        return(1);
	}

    f = fopen("log.txt", "w");

    do {
        if (leer_char(port_serie,&cr) == 0)
        {
            numerr++;
            printf("Err -> %d\n", numerr);
        } else {
            if (cr != '\n') {
                buff[pb++] = cr;
            } else {
                buff[pb] = '\0';
                pb = 0;
                printf("Rec -> %s\n", buff);
                fprintf(f, "Rec -> %s\n", buff);
            }
        }

    } while( numerr < 10 );

    fclose(f);

    /* -- cierra el port serie -- */
    cerrar_port(port_serie);

    return 0;
}

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *