Hallo esrtmal,
ich bin anfänger im Gebiete WinApi und brauche ihre Unterstützung, um mein C++-Code zu beenden.
Ich arbeite unter Windows XP und möchte die Positionen x und y von meiner Textdatei zu einem Mikrocontroller atmega644p über der serielle Port RS232 senden. Die Textdatei enthält die positionen x, y und andere Symbole. Für meine Arbeit beschäftige ich mich nur mit jeder Position x und y. Die andere Symbole sind unwichtig. In der Textdatei befinden sich die Positionen x und y in Ascii-Form.
Ich habe versucht, ein c++-Code zu schreiben, damit man die Textdatei Zeile für zeile ausliest. Dann ich habe mit Hilfe der Funktion CreateFile versucht, der serielle Port RS232 zu oeffnen, um später jede Position x und y in Byte-Form ueber die serielle Schnittstelle zu uebertragen.
Ich habe mein C++-Code debuggt und habe folgender Fehlermeldung auf mein Consol bekommen : " "Fehler: Die Oeffnung der serielle Port RS232 ist unmöglich" " .
Warum bekomme ich diese Fehlermeldung ?
Bitte, was soll ich noch in meinem programm ändert, damit ich mit Erfolg jede Position x und y von der Textdatei auf dem seriellen Port RS232 uebertrage. Im Anhang liegt die Textdatei.
Danke im voraus.
mfg arthurdubois
Mein C++-Code lautet:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
using namespace std;
int main(void)
{
ifstream file("koordinaten.txt", ios::in | ios::binary);
string line;
if (!file)
{
cout << "Fehler: Die Oeffnung der Textdatei in Modus Auslesen ist unmöglich" << endl;
return 1;
}
// Oeffung der Port COM1
HANDLE h = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (h == INVALID_HANDLE_VALUE)
{
cout << "Fehler: Die Oeffnung der serielle Port RS232 ist unmöglich" << endl;
return 1;
}
DCB dcb = { 0 };
BOOL dcbOk = GetCommState(h, &dcb);
dcb.BaudRate = CBR_115200;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = TWOSTOPBITS;
dcb.fOutxCtsFlow = FALSE; // Warum das und dann ein manueller Test von CTS ?
dcbOk = SetCommState(h, &dcb);
// Auslesen Zeile pro Zeile
while (getline(file, line))
{
int x ; // x, y von Textdatei
int y ;
int x_steps; // x, y in Motorschritt
int y_steps;
stringstream input; // Eingangsfluss (Eine Zeile von Text)
stringstream output_x; // Ausgangsfluss (Ein Paar von Koordinaten)
stringstream output_y;
// Eine Zeile wird ein Eingangsfluss
input.str(line);
// Extraktion von X und von Y.
if (input.get() != 'X')
continue;
input >> x;
//cout << x << endl;
if (input.get() != 'Y')
continue;
input >> y;
//cout << y << endl ;
// cout << x << ", " << y << endl;
// Konversion der Positionen x und y in Motorschritt
x_steps = (x * 127) / 500;
//cout << x_steps << endl ;
y_steps = (y * 127) / 500;
//cout << x_steps << ", " << y_steps << endl;
// Uebertragung von Koordinaten (x,y) ueber der serielle Port RS232
DWORD written = 0;
output_x << x_steps; // TODO: Ne manque-t-il pas un terminateur ou un cadrage ?
WriteFile(h, output_x.str().c_str(), output_x.str().size(), &written, 0);
output_y << y_steps; // TODO: Ne manque-t-il pas un terminateur ou un cadrage ?
WriteFile(h, output_y.str().c_str(), output_y.str().size(), &written, 0);
// TODO: Et là, il ne faut pas attendre un retour ?
}
CloseHandle(h);
return 0;
}
|