Bluetooth Modul am PC

OP #3244425
Lesenswert?

Ich habe das HC-05 Bluetooth Modul gekauft, und es über Bluetooth mit 
meinem PC verbunden. Das Modul scheint im Gerätemanager als COM10 auf.
Wenn ich den COM10 mit HTERM öffne, dann funktioniert alles wie es soll. 
Wenn ich mein eigenes Programm verwenden will, bekomme ich die Meldung, 
dass die Schnittstelle nicht geöffnet werden konnte.

Mit einem USB-UART Adapter (FT232) funktionier der Code.

Mein Code (CodeBlocks):
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <windows.h>
4
#include <conio.h>
5

6
HANDLE hCom;
7
DCB dcb;
8
COMMTIMEOUTS timeouts;
9
DWORD dwBytesWritten, dwBytesRead;
10
unsigned char rbuf, wbuf[20],rx_str[100];
11

12
void RS232_Init();
13
int main()
14
{
15

16
    RS232_Init();
17

18
    if(hCom == INVALID_HANDLE_VALUE)
19
    {
20
        printf("Schnittstelle konnte nicht geöffnet werden");
21
        return -1;
22
    }
23

24
    while(1)
25
    {
26
        if(kbhit())
27
        {
28
            //scanf("%s",wbuf);
29
            //WriteFile(hCom, wbuf, strlen(wbuf), &dwBytesWritten, 0);
30
            wbuf[0]=getch();
31
            WriteFile(hCom, wbuf, 1, &dwBytesWritten, 0);
32

33
            if(wbuf[0]==27)
34
                return 0;
35
        }
36

37
        ReadFile(hCom, &rbuf, 1, &dwBytesRead, 0);
38

39
        if(dwBytesRead == 1)
40
        {
41
            printf("%c",rbuf);
42
        }
43
    }
44

45
    return 0;
46
}
47

48
void RS232_Init()
49
{
50

51
    hCom = CreateFile ("COM10",
52
                       GENERIC_READ|GENERIC_WRITE,
53
                       0,
54
                       NULL,
55
                       OPEN_EXISTING,
56
                       FILE_ATTRIBUTE_NORMAL,
57
                       NULL);
58

59
    GetCommState(hCom, &dcb);
60
    dcb.BaudRate = 9600;
61
    //dcb.BaudRate = 4800;
62
    dcb.ByteSize = 8;
63
    dcb.Parity = NOPARITY;
64
    dcb.StopBits = ONESTOPBIT;
65
    SetCommState(hCom, &dcb);
66

67

68
    GetCommTimeouts(hCom, &timeouts);
69
    timeouts.ReadIntervalTimeout = MAXDWORD;
70
    timeouts.ReadTotalTimeoutMultiplier = 0;
71
    timeouts.ReadTotalTimeoutConstant = 0;
72
    SetCommTimeouts(hCom, &timeouts);
73

74
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren