Forum: PC-Programmierung Nochmal Winapi und RS232


von soso (Gast)


Lesenswert?

Ich habe jetzt nen funktionierenden code, mit welchem ich senden und 
empfangen kann, jedoch funktioniert das empfangen nicht gebuffert ist. 
ich würde gerne aus dem buffer immer das letzte empfangene zeichen 
auslesen. der rest soll solange im buffer warten. momentan empfange ich 
nicht jedes zeichen, wenn ich zwischendurch was anderes mache gehen mir 
zeichen verlohren.

Kann da jemand mal bitte über den code gucken?

Danke Gruß
1
// Handel fuer den comport
2
DCB           dcb;
3
DWORD         iBytesWritten, dwRead = 0;
4
DWORD         dwEvtMask, dwSetMask = EV_RXCHAR | EV_ERR;
5
HANDLE        hCom;
6
BOOL          bRet = TRUE;
7
OVERLAPPED    o;
8
COMMTIMEOUTS  ct;
9
10
11
#define RECVBYTES       5      // max. recv. bytes per transmission
12
#define STX             0x02
13
#define ETX             0x03
14
#define Pointer         0x00   // ?? Pointer sind 4 Byte lang?
15
#define CHK             0x3E
16
#define ENQ             0x05
17
18
19
20
21
/**
22
 * Diese Funktion initialisiert die RS232 Schnittstelle
23
 * und ließt / schreibt in die Buffer
24
 *
25
 *
26
 */
27
DWORD WINAPI rs232_listener(LPVOID lpParam){
28
29
    char          flag[] = {ENQ};
30
    unsigned char          output[] = {STX, 0x0E, Pointer, '1', ETX, CHK};
31
32
    int           laenge = sizeof(output);
33
    char          *Buf;
34
35
    Buf = (char*)malloc(laenge * 5 + 1);
36
37
    memset(&o, 0, sizeof(OVERLAPPED));
38
    o.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
39
40
    if(!(hCom = CreateFile("COM3", GENERIC_WRITE | GENERIC_READ, 0, NULL,
41
                          OPEN_EXISTING, 0, NULL)))
42
    {
43
      LPVOID lpMsgBuf;
44
      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
45
                      FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
46
                      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
47
      MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error: CreateFile", MB_OK | MB_ICONINFORMATION);
48
      LocalFree(lpMsgBuf);
49
    return 0; // no action
50
    }
51
52
    dcb.DCBlength = sizeof(DCB);
53
    GetCommState(hCom, &dcb);
54
    dcb.BaudRate  = 9600;
55
    dcb.ByteSize  = 8;
56
    dcb.Parity    = NOPARITY;
57
    dcb.StopBits  = TWOSTOPBITS;
58
    SetCommState(hCom, &dcb);
59
60
    GetCommTimeouts(hCom, &ct);
61
    //ct.ReadIntervalTimeout         = 50;
62
    ct.ReadIntervalTimeout         = 100000 / 9600 * (dcb.ByteSize +
63
                                                   (dcb.Parity == NOPARITY ? 0 : 1) +
64
                                                   (dcb.StopBits == ONESTOPBIT ? 1 : 2)) * RECVBYTES;
65
    ct.ReadTotalTimeoutMultiplier  = 0;
66
    ct.ReadTotalTimeoutConstant    = 50;
67
    ct.WriteTotalTimeoutMultiplier = 0;
68
    ct.WriteTotalTimeoutConstant   = 0;
69
    SetCommTimeouts(hCom, &ct);
70
71
    SetupComm(hCom, COM_BUFFER_SIZE, COM_BUFFER_SIZE);
72
    SetCommMask(hCom, dwSetMask);
73
74
    if(!WriteFile(hCom, &flag, 1, &iBytesWritten, NULL)){
75
      LPVOID lpMsgBuf;
76
      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
77
                    FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
78
                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
79
      MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error: WriteFile", MB_OK | MB_ICONINFORMATION);
80
      LocalFree(lpMsgBuf);
81
    }
82
83
84
85
86
    return 0;
87
88
89
90
91
}
92
93
94
95
96
97
98
/**
99
 * Diese Funktion sendet ein Zeichen über den Sendebuffer
100
 */
101
void send_RS232(char *data, int length){
102
103
104
     OVERLAPPED osWrite = {0};
105
     DWORD dwWritten;
106
     DWORD dwRes;
107
     BOOL fRes;
108
109
110
     // Create this write operation's OVERLAPPED structure's hEvent.
111
     osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
112
     if (osWrite.hEvent == NULL)
113
        // error creating overlapped event handle
114
        printf("error\r\n");
115
116
     //printf("wait for signal");
117
118
     // Issue write.
119
     if (!WriteFile(hCom, data, length, &dwWritten, &osWrite)) {
120
        if (GetLastError() != ERROR_IO_PENDING) {
121
           // WriteFile failed, but isn't delayed. Report error and abort.
122
           fRes = FALSE;
123
        }else{
124
           // Write is pending.
125
           dwRes = WaitForSingleObject(osWrite.hEvent, INFINITE);
126
           switch(dwRes)
127
           {
128
              // OVERLAPPED structure's event has been signaled.
129
              case WAIT_OBJECT_0:
130
                   if (!GetOverlappedResult(hCom, &osWrite, &dwWritten, FALSE)){
131
               printf("Fehler 1");
132
                 fRes = FALSE;
133
                   }else{
134
                    // Write operation completed successfully.
135
                     printf("Ok");
136
                    fRes = TRUE;
137
                   }
138
                   break;
139
140
              default:
141
                   // An error has occurred in WaitForSingleObject.
142
                   // This usually indicates a problem with the
143
                  // OVERLAPPED structure's event handle.
144
                printf("Fehler 1");
145
                   fRes = FALSE;
146
                   break;
147
           }
148
        }
149
     }else{
150
        // WriteFile completed immediately.
151
        fRes = TRUE;
152
        //printf("Fehler");
153
     }
154
     CloseHandle(osWrite.hEvent);
155
     return;
156
157
158
}
159
160
161
/**
162
 * Diese Funktion empfaengt ein zeichen ueber den RS232 Buffer
163
 */
164
char receive_rs232(){
165
  char ret = 0;
166
  char InString[COM_BUFFER_SIZE + 1];
167
168
    WaitCommEvent(hCom, &dwEvtMask, &o);
169
    if(WAIT_OBJECT_0 == WaitForSingleObject(o.hEvent, INFINITE)) //warten bis Event
170
    {
171
      if(dwEvtMask & EV_RXCHAR)
172
      {
173
        bRet = ReadFile(hCom, &InString, sizeof(InString), &dwRead, NULL);
174
        if(!bRet){
175
          LPVOID lpMsgBuf;
176
          FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
177
                        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(),
178
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
179
          MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error: ReadFile", MB_OK | MB_ICONINFORMATION);
180
          LocalFree(lpMsgBuf);
181
        }
182
        else
183
        {
184
          InString[dwRead] = '\0';
185
          // Add to buffer
186
          rxBuffer.buffer[(rxBuffer.iWrite++)%BUFFERSIZE] = InString[0];
187
          rxBuffer.anzElemente = rxBuffer.anzElemente + 1;
188
          //printf(TEXT("%s"), InString);
189
        }
190
      }
191
      if(dwEvtMask & EV_ERR)
192
      {
193
        MessageBox(NULL, "Error empfangen", "Error: ReadFile", MB_OK);
194
      }
195
    }
196
197
198
199
  return ret;
200
}

von Justus S. (jussa)


Lesenswert?

soso schrieb:
> ich würde gerne aus dem buffer immer das letzte empfangene zeichen
> auslesen. der rest soll solange im buffer warten.

mit Verlaub, das ist doch total unsinnig, die Reihenfolge der Zeichen 
wird wohl auch eine Bedeutung haben...und afaik geht das auch gar nicht, 
die Buffer von den üblichen RS232-Schnittstellen sind FIFO, da kann man 
sich nicht aussuchen, welche man jetzt haben möchte....

von soso (Gast)


Lesenswert?

Naja das meine ich doch, nur mir gehen momentan Zeichen verlohren das 
will ich nicht

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.