C/Visual C - Puerto Serie

 
Vista:

Puerto Serie

Publicado por Tomas Gutierrez (3 intervenciones) el 28/10/2003 01:13:10
Hola que tal,
estoy haciendo un programita en C para Windows 2000 para leer del puerto serie, esto es lo que llevo, pero siempre me dice que no puede abrir el puerto:

#include <windows.h>
#include <stdio.h>
#include <process.h>
#include <conio.h>
#include <types.h>


int main()
{
HANDLE HdComm;
DWORD DwError;
DCB Dcb;
unsigned Data;
char Msg[128];
unsigned long RealNum;
int e;

// Open the COMM port.
HdComm = CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0,NULL, OPEN_EXISTING, 0, NULL );
while ( HdComm == (HANDLE)0xFFFFFFFF ) // If COMM port can't be opened, then ...
{
DwError = GetLastError(); // Display error code, and abort.
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, 0, DwError, 0, Msg, 124, 0 );
printf( "Cannot Open Port COM1 !\nError Code: %d, - %s.\n", DwError, Msg );

}

// Initialize COMM port.
SetupComm( HdComm, 1024, 1024 );
GetCommState( HdComm, &Dcb );
Dcb.BaudRate = BAUD_9600;
Dcb.Parity = NOPARITY;
Dcb.ByteSize = 8;
Dcb.StopBits = ONESTOPBIT;
SetCommState( HdComm, &Dcb );

for (e=0; e < 3000; e++){
ReadFile( HdComm, &Data, 1, &RealNum, NULL ); //Recieve the char from COM1
// WriteFile( HdComm, &Data, 1, &RealNum, NULL ); //Write the recieved data to COM1
printf( "%02x ", Data ); //Display the data
} //while( Data != 0x1b ); // Loop until the data is ESC

CloseHandle( HdComm );
getchar();
return 0;
}

Alguien me puede ayudar, me urge.

Saludos a todos
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Puerto Serie

Publicado por INT3 (7 intervenciones) el 28/10/2003 04:09:51
y cual es tu error ?
no lo he provado, pero creo que solo es COM1 y no \\\\.\\\COM1
escribo ya que cuando abres un puerto paralelo solo se pone LPT1
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Puerto Serie

Publicado por Tomas Gutierrez (3 intervenciones) el 28/10/2003 17:03:00
estoy probando con COM1 y en el debug me pone que HdComm que es la variable que se crea con CreateFile() tiene *bus error*

Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar