Foro de elhacker.net

Comunicaciones => Hacking Mobile => Mensaje iniciado por: hoofmen en 2 Mayo 2006, 05:08 am



Título: RAW C/C++ para programar Puertos Seriales
Publicado por: hoofmen en 2 Mayo 2006, 05:08 am
Saludos,
  Despues de varios intentos fallidos de hacer aplicaciones managed que ocupen puertos seriales (para usar Bluetooth), me meti de lleno en C++( que es un lenguaje que me cuesta).
Logre hacer esta aplicacion consola:

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

HANDLE fileHandle;
HWND wForm = NULL;

int OpenSerialPort();
int CloseSerialPort();
int SendMsg();

int OpenSerialPort()
{
    WCHAR comPort[30];
   
    wsprintf(comPort,L"COM%d",7);

    if (fileHandle == NULL){                                     
        fileHandle = CreateFile(comPort, GENERIC_WRITE ,0,NULL,OPEN_EXISTING,0,0);
        if (fileHandle == INVALID_HANDLE_VALUE){
            MessageBox(NULL,L"No se pudo crear el COM 7",L"Aviso",MB_OK);
            fileHandle = NULL;
            return 0;
        }
    }
    return 1;
}

int CloseSerialPort()
{
    if (fileHandle != NULL){
        CloseHandle(fileHandle);
        fileHandle = NULL;
      MessageBox(NULL,L"COM cerrado",L"Aviso",MB_OK);
      return 1;
    }
   return 0;
}

int SendMsg()
{   
    DWORD dwSize = 0, dwWritten = 0;   
    const int key = 13;

    WriteFile(fileHandle, &key, sizeof(key), &dwWritten, NULL);
    printf("Mensaje enviado\n");   
    MessageBox(NULL,L"Mensaje Enviado",L"Aviso",MB_OK);
   return 1;
}

int main (int argc, char** argv)
{
    if (!OpenSerialPort())
        return -1;

    SendMsg();
   
    if (!CloseSerialPort())
        return -1;

    system("pause");
    return 1;
}


lo unico que me interesa es mandar el numero 13 por el serial port, esta aplicacion desde el PC me funciona sin problemas, pero cuando la trato en la PPC, no me deja, en la PPC y PC tengo com 8 entrada, com 7 salida, estoy haciendo algo mal? todo mal??

saludos y gracias