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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <tchar.h>
#include <Windows.h>
#include <stdio.h>
using namespace std;
void PrintCommState(DCB dcb)
{
// Imprimez certaines des valeurs de structure DCB
_tprintf( TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"),
dcb.BaudRate,
dcb.ByteSize,
dcb.Parity,
dcb.StopBits );
}
int main(void)
{
ifstream file("koordinaten.txt", ios::in | ios::binary);
string line;
if (!file)
{
cout << "Erreur: Impossible d´ouvrir le fichier en mode lecture" << endl;
return 1;
}
//////////////////////////////////////////////////////////////////////////////////////
///////////// Configuring a communications Resource//////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
HANDLE h ;
char *fmsg = NULL;
int error = 0;
DCB dcb = { 0 };
BOOL dcbOk ;
TCHAR *pcCommPort = TEXT("COM1"); // La majorite des systemes ont un port serie COM1
// Ouverture du port COM1 (c'est votre code tel quel)
h = CreateFile(pcCommPort, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE, // Access read-Write mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
FILE_ATTRIBUTE_NORMAL, // normal file
0); // Port attributes)
if (h == INVALID_HANDLE_VALUE) // Echec de l´ouverture du port serie
{
// Description de l´echec d´ouverture du port de serie
error = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &fmsg,
0, NULL );
cout << "error: " << GetLastError() << endl << fmsg << endl; // Affichage de l´erreur
// cout << "Error: Impossible d´ouvrir le port série" << endl;
return 1;
}
// Tirez parti de la configuration actuelle en recuperant d´abord
// tous les parametres actuels.
dcbOk = GetCommState(h, &dcb);
if (!dcbOk) {
printf ("GetCommState failed with error %d.\n", GetLastError()); // Manipulez l´erreur
return (2);
}
PrintCommState(dcb); // Output to console
// Fill in some DCB values and set the COM1 state:
// 115200 bps, 8 data bits, no parity, and two stop bit.
dcb.BaudRate = CBR_115200; // baud rate
dcb.ByteSize = 8; // data size, transmit and receive
dcb.Parity = NOPARITY; // parity bit
dcb.StopBits = TWOSTOPBITS; // stop bit
// dcb.fOutxCtsFlow = FALSE; // Pourquoi ça et puis ensuite un test manuel du CTS ?
dcbOk = SetCommState(h, &dcb);
if (!dcbOk) {
printf ("SetCommState failed with error %d.\n", GetLastError()); // Manipulez l´erreur
return (3);
}
// Recevez la configuration de pcCommPort("COM1") a nouveau
dcbOk = GetCommState(h, &dcb);
if (!dcbOk) {
printf ("GetCommState failed with error %d.\n", GetLastError()); // Manipulez l´erreur
return (2);
}
PrintCommState(dcb); // Output to console
_tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
return (0);
///////////////////////////////////////////////////////////////////////////
//////////////// Configuring Timeouts//////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Retrieve the timeout parameters for all read and write operations
// on the port.
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts (h, &CommTimeouts);
// Changer les parametres de structure COMMTIMEOUTS.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
// Set the timeout parameters for all read and write operations on the port
//if (!SetCommTimeouts (h, &CommTimeouts))
//{
//
// char hMainWnd ;
// int dwError = 0;
// // Could not set the timeout parameters.
// MessageBox (hMainWnd, TEXT("Unable to set the timeout parameters"),
// TEXT("Error"), MB_OK);
// dwError = GetLastError ();
// return FALSE;
//}
///////////////////////////////////////////////////////////////////////////
////////// Extraction et Conversion des positions X et Y //////////////////
///////////////////////////////////////////////////////////////////////////
// Lecture ligne par ligne
while (getline(file, line))
{
int x; // x, y du texte
int y;
int x_steps; // x, y en pas moteur
int y_steps;
stringstream input; // flux d'entrée (une ligne du texte)
stringstream output_x; // flux de sortie (une paire de coordonnées)
stringstream output_y;
char input_x[5]; // une paire de coordonnées
char input_y[5];
int x1;
int y1;
// Une ligne devient un flux d'entrée
input.str(line);
// Extraction du X et du Y.
if (input.get() != 'X')
continue;
input >> x;
if (input.get() != 'Y')
continue;
input >> y;
// Conversion de la position en pas moteur
// J'ai rendu le calcul compatible avec le type int. (et je le trouve plus lisible)
x_steps = x * 127 / 500;
y_steps = y * 127 / 500;
///////////////////////////////////////////////////////////////////////////
/////////////// Writing to the serial port//////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Envoi des coordonnées par le port série, à compléter car votre code ne permet pas de savoir ce que vous deviez réellement faire.
DWORD written = 0;
output_x << x_steps; // TODO: Ne manque-t-il pas un terminateur ou un cadrage ?
WriteFile(h, // Manipulez le port serie "COM1" (Port "COM1" handle)
output_x.str().c_str(), // Pointer to the data to write
output_x.str().size(), // Number of bytes to write
&written, // Pointer to the number of bytes written
0); // Must be zero for windows CE
output_y << y_steps; // TODO: Ne manque-t-il pas un terminateur ou un cadrage ?
WriteFile(h, // Port "COM1" handle
output_y.str().c_str(), // Pointer to the data to write
output_y.str().size(), // Number of bytes to write
&written, // Pointer to the number of bytes written
0); // // Must be zero for windows CE
///////////////////////////////////////////////////////////////////////////////////////////////
//////////////////// Reading from the serial port ///////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
DWORD dwBytesTransferred = 0;
output_x >> x1;
itoa(x1, input_x, 10);
ReadFile(h, // Manipulez le port serie "COM1" (Port "COM1" handle)
input_x.str().c_str(), // Pointer to the data to write
input_x.str().size(), // Number of bytes to write
&dwBytesTransferred, // Pointer to the number of bytes written
0); // Must be zero for windows CE
output_y >> y1 ;
itoa(y1, input_y, 10);
ReadFile(h, // Manipulez le port serie "COM1" (Port "COM1" handle)
input_y.str().c_str(), // Pointer to the data to write
input_y.str().size(), // Number of bytes to write
&dwBytesTransferred, // Pointer to the number of bytes written
0); // Must be zero for windows CE
// TODO: Et là, il ne faut pas attendre un retour ?
//////////////////////////////////////////////////////////////////////////////
///////////////// Using Communication Events//////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
}
CloseHandle(h);
return 0;
} |
Partager