Soy nuevo en la codificación en C ++, nuevo en la comunicación inalámbrica y soy autodidacta, por lo que si observa algún error no relacionado con el problema, indíquelo también.
Tengo dos XBEE serie 1 conectadas a las tablas de acceso del explorador Sparkfun XBEE. Uno está conectado a mi PC a través de mini-b USB, el otro es remoto y está conectado a la placa de arranque Sparkfun ADXL335, alimentado con un enchufe de salida al adaptador mini-b. He configurado los XBEE utilizando XCTU y se comunican bien tanto conectados directamente a la PC como con el remoto.
He visto las muestras que se reciben en Realterm y XCTU y las muestras coinciden con el marco de API correcto.
Aquí hay un marco de ejemplo para referencia:
7E 00 0E 83 00 02 32 00 01 0E 00 03 FF 03 FF 03 FF 33
Estoy utilizando el código C ++ en Visual Studio Community 2017 para acceder y leer el puerto.
El problema que tengo es que los datos que estoy recibiendo son No es coherente y no tiene sentido para mí. Si pudiera recibir los datos en Hex de la forma en que se muestran en XCTU y Realterm, sería ideal.
Lo siento por la gran cantidad de texto, ¡quiero que se presente la mayor cantidad de información posible para obtener la mejor respuesta posible!
CODE:
{
//working method of setting access to serial/usb port "COM3"
myPort = CreateFile(_T("COM3"),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0
);
if (myPort == INVALID_HANDLE_VALUE)
{
cout << "Failed to connect to serial port" << endl << GetLastError() << endl;
CloseHandle(myPort);
return E_FAIL;
}
SetFilePointer(myPort, 0, 0, FILE_BEGIN);
DCB dcb = { 0 };
dcb.DCBlength = sizeof(DCB);
if (GetCommState(myPort, &dcb) == 0)
{
cout << "Connection with serial port failed" << endl << GetLastError() << endl;
CloseHandle(myPort);
return E_FAIL;
}
//....
dcb.fBinary = true;
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.fParity = false;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fTXContinueOnXoff = true;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.XoffChar = 0x13;
dcb.XonChar = 0x11;
dcb.XoffLim = 10;
dcb.XonLim = 10;
if (SetCommState(myPort, &dcb) == 0)
{
cout << "Failed to set dcb information of selected port" << endl << GetLastError() << endl;
CloseHandle(myPort);
return E_FAIL;
}
//....
//Create the overlapped event. Must be closed,
//before exiting to avoid a handle leak.
overlapStruct.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
//Error creating overlapped event; abort.
if (overlapStruct.hEvent == NULL)
{
cout << "Failed creating overlapped event" << endl;
return E_FAIL;
}
/*Loop while the
accelerometers are active and transmitting data*/
//while(GetCommState(myPort, &dcb) != 0)
//For loop imitates above loop for testing purposes
for (int i = 0; i <= 100; i++)
{
//Method of reading information in
//from the described usb port
//Issue read operation of accelerometer
if (!fWaitingOnRead)
{
if (!ReadFile(myPort, commBuffer, bufferSize, NULL, &overlapStruct))
{
if (GetLastError() != ERROR_IO_PENDING)
cout << "Something went wrong during ReadFile() function" << endl;
else
fWaitingOnRead = TRUE;
}
//Read completed immediately
else
{
//method of printing what was read (what is inside commBuffer)
for (int j = 0; j <= (bufferSize+1); j++)
{
//if (!(commBuffer[j] == NULL))
cout <<"commBuffer at position: "<<"("<<j<<") "<<commBuffer[j]<<endl;
}
}
}
Este es un ejemplo de lo que se devuelve de ~ 100 ejecuciones del bucle for (los conjuntos en negrita representan nuevos / cambios en los datos):
**commBuffer at position: (0) 768**
**commBuffer at position: (1) 1023**
**commBuffer at position: (2) 1023**
**commBuffer at position: (3) 13823**
**commBuffer at position: (4) 3585**
**commBuffer at position: (5) 0**
**commBuffer at position: (6) 0**
**commBuffer at position: (7) 0**
**commBuffer at position: (8) 0**
**commBuffer at position: (9) 0**
**commBuffer at position: (10) 0**
**commBuffer at position: (11) 0**
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
**commBuffer at position: (0) 126**
**commBuffer at position: (1) 33550**
**commBuffer at position: (2) 512**
**commBuffer at position: (3) 47**
**commBuffer at position: (4) 3585**
**commBuffer at position: (5) 0**
**commBuffer at position: (6) 0**
**commBuffer at position: (7) 0**
**commBuffer at position: (8) 0**
**commBuffer at position: (9) 0**
**commBuffer at position: (10) 0**
**commBuffer at position: (11) 0**
---
**commBuffer at position: (0) 768**
**commBuffer at position: (1) 1023**
**commBuffer at position: (2) 1023**
**commBuffer at position: (3) 14079**
**commBuffer at position: (4) 3585**
**commBuffer at position: (5) 0**
**commBuffer at position: (6) 0**
**commBuffer at position: (7) 0**
**commBuffer at position: (8) 0**
**commBuffer at position: (9) 0**
**commBuffer at position: (10) 0**
**commBuffer at position: (11) 0**
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 14079
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 14079
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 14079
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 14079
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
**commBuffer at position: (0) 126**
**commBuffer at position: (1) 33550**
**commBuffer at position: (2) 512**
**commBuffer at position: (3) 48**
**commBuffer at position: (4) 3585**
**commBuffer at position: (5) 0**
**commBuffer at position: (6) 0**
**commBuffer at position: (7) 0**
**commBuffer at position: (8) 0**
**commBuffer at position: (9) 0**
**commBuffer at position: (10) 0**
**commBuffer at position: (11) 0**
---
**commBuffer at position: (0) 768**
**commBuffer at position: (1) 1023**
**commBuffer at position: (2) 1023**
**commBuffer at position: (3) 13823**
**commBuffer at position: (4) 3585**
**commBuffer at position: (5) 0**
**commBuffer at position: (6) 0**
**commBuffer at position: (7) 0**
**commBuffer at position: (8) 0**
**commBuffer at position: (9) 0**
**comBuffer at position: (10) 0**
**commBuffer at position: (11) 0**
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
---
commBuffer at position: (0) 768
commBuffer at position: (1) 1023
commBuffer at position: (2) 1023
commBuffer at position: (3) 13823
commBuffer at position: (4) 3585
commBuffer at position: (5) 0
commBuffer at position: (6) 0
commBuffer at position: (7) 0
commBuffer at position: (8) 0
commBuffer at position: (9) 0
commBuffer at position: (10) 0
commBuffer at position: (11) 0
Logré que los datos en commBuffer se imprimieran como valores hexadecimales, cambiando la línea: cout <<"commBuffer at position: "<<"("<<j<<") "<<commBuffer[j]<<endl;
a: cout <<"commBuffer at position: "<<"("<<j<<") "<< hex <<int(commBuffer[j])<<endl;
(con commBuffer [] como tipo: unsigned char)
Gracias a la información sobre esta pregunta formulada en @ StackOverflow y gracias a brhans por contactarme para buscar la información correcta:
¿Cómo imprima un carácter sin signo como hexadecimal en c ++ utilizando ostream
También aumenté el tamaño de commBuffer (ahora con que obviamente es demasiado pequeño) lo que ayudó significativamente.
Sin embargo, todavía tengo el problema de recibir repeticiones incorrectas de los marcos. por ejemplo
Varios cuadros en XCTU: (7E 00 0E 83 00 02 30 00 01 0E 00 03 FF 03 FF 03 FF 35) (7E 00 0E 83 00 02 30 00 01 0E 00 03 FF 03 FF 03 FF 35) (7E 00 0E 83 00 02 30 00 01 0E 00 03 FF 03 FF 03 FF 35) ...
Varios marcos en la aplicación: (00 00 00 00 00 3A 31 00 01 0E 00 00 00 00 FF 03 FF 33) (7E 00 0E 83 00 02 31 00 01 0E 00 00 00 00 00 00 00 00 3A) (7E 00 0E 83 00 02 31 00 01 0E 00 03 FF 03 FF 03 FF 34)
¿Todavía hay algo incorrecto con mi código? ¿O hay una explicación única para estos resultados diferentes entre el terminal y la aplicación?
ACTUALIZACIÓN 08/23/18
He cambiado la función de impresión así:
for (int i = 0; i <= 76; i++)
{
repositionFrame:
//Method of reading information in
//from the described usb port
//Issue read operation of accelerometer
if (!fWaitingOnRead)
{
if (!ReadFile(myPort, commBuffer, (bufferSize +1), NULL, &overlapStruct))
{
if (GetLastError() != ERROR_IO_PENDING) //Error is not due to IO pending on a result
cout << "Something went wrong during ReadFile() function" << endl;
else
fWaitingOnRead = TRUE;
}
//Read completed immediately
else
{
//Method of printing what was read (what is inside commBuffer)
for (int j = 0; j <= (bufferSize); j++)
{
if (commBuffer[0] == 0x7e)
cout << "commBuffer at position: " << "(" << j << ") " << hex << setfill('0') << setw(2) << unsigned int(commBuffer[j]) << endl;
else
{
//jump to "repositionFrame:", the recieved frame was incorrect and therefore must be discarded
goto repositionFrame;
}
}
}
}
Para filtrar los marcos que comienzan con "7e", que funciona.
Por supuesto, todavía estoy obteniendo datos incorrectos / confusos, curiosamente, las primeras 4 posiciones de la matriz commBuffer son siempre correctas (7E 00 0E 83), los datos incorrectos solo aparecen después de esa secuencia, y también los datos incorrectos parecen ser , datos que existen en una posición diferente en el marco correcto, ( 33 33 00 en posiciones [15] [16] [17] en lugar de lo que debería ser: 03 FF 33 en [15] [16] [17]) en lugar de valores aleatorios que reemplazan los valores correctos en la matriz.
¿Esperamos que alguien pueda explicar los datos incorrectos?