Código:
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <windows.h>
#include "rs232.h"
#include <string.h>
#include <string>
#include <sstream>
using namespace std;
ifstream fichero;
int main(){
int servo, posicion,syncbyte = 255;
char s,ser,pos,pos2,pos3, p, sbyte = (char) syncbyte;
string nombre, auxiliar;
cout << "Introduce el nombre del fichero: ";
getline(cin, nombre);
fichero.open(nombre, ios::in);
if (fichero.is_open()){
while (!fichero.eof()){
getline(fichero, auxiliar);
ser = auxiliar[2];
servo = ser - '0';
if (servo >4){
servo = 3;
}
if (servo <0){
servo = 0;
}
pos = auxiliar[4];
pos2 = auxiliar[5];
pos3= auxiliar[6];
posicion = (pos - '0')*100 +(pos2 - '0')*10 + (pos3 - '0');
if (posicion <0){
posicion= 0;
}
if (posicion >255){
posicion = 254;
}
cout <<servo << "," <<posicion<<endl;
s = (char) servo;
p = (char) posicion;
ostringstream suma;
suma << sbyte << s << p;
string todo = suma.str();
//cout << todo<<endl;
const char* lavihen = todo.c_str();
RS232_OpenComport(0, 9600, "8N1");
RS232_cputs(0, lavihen);
RS232_CloseComport(0);
}
}else{
cout << "ERROR";
}
return 0;
}