Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views3 pages

Programa Prueba

This document contains a C program that communicates with a serial port (COM4) to read and write data. It initializes the serial port settings, reads user input, sends it to the port, and displays the received data until the Enter key is pressed. The program also includes error handling for serial communication operations.

Uploaded by

bazurtomeir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Programa Prueba

This document contains a C program that communicates with a serial port (COM4) to read and write data. It initializes the serial port settings, reads user input, sends it to the port, and displays the received data until the Enter key is pressed. The program also includes error handling for serial communication operations.

Uploaded by

bazurtomeir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "stdafx.

h"
#include <stdio.h>
#include <conio.h>
#include <windows.h>
char escribir_leer(char transmitir);
void inicializar(void);
void cerrar(void);
HANDLE handlePort_;
WCHAR puerto[] = L"COM4";
DWORD length = 1, sizeBuffer = 1;

void main(void)
{

char a=0,b;
int x=0;

inicializar();
do
{

if(_kbhit())
{
a=_getch();
b = escribir_leer(a);
printf("\n El dato leido fue un = %x", b&0xff);

} while (a != 0xd);

cerrar();
printf("\nPrograma finalizado");
_getch();

}
char escribir_leer(char transmitir)
{

char outputData[2];
char inputData[2];
outputData[0] = transmitir;

if (WriteFile(handlePort_,
outputData,
sizeBuffer,
&length, NULL) == 0)
{
printf("Error Escritura Puerto Serial.");

}
//Sleep(1);

if (ReadFile(handlePort_,
inputData,
sizeBuffer,
&length,
NULL) == 0)
{
printf("Error Lectura Puerto Serial");

return(inputData[0]);

}
void inicializar(void)
{

handlePort_ = CreateFile(puerto,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);

DCB config_;

if (GetCommState(handlePort_, &config_) == 0)
{

printf("Puerto Serial Error");

}
config_.BaudRate = 115200;
config_.StopBits = 2;
config_.Parity = 0;
config_.ByteSize = 8;

if (SetCommState(handlePort_, &config_) == 0)
{
printf("Error Configuracion de Puerto Serial");
}

COMMTIMEOUTS comTimeOut;
comTimeOut.ReadIntervalTimeout = 3;
comTimeOut.ReadTotalTimeoutMultiplier = 3;
comTimeOut.ReadTotalTimeoutConstant = 2;
comTimeOut.WriteTotalTimeoutMultiplier = 3;
comTimeOut.WriteTotalTimeoutConstant = 2;
SetCommTimeouts(handlePort_, &comTimeOut);

}
void cerrar(void)
{

if (CloseHandle(handlePort_) == 0)
{
printf("Cerrar puerto error");
}
}

You might also like