From c9f7ba2ab086edfbe5a2ccf4ac2bf905fe6e86a1 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Thu, 21 Dec 2017 10:16:21 +0100 Subject: [PATCH] ADD: increased debug messages in order to see when commnunications break down between device and client. Before it silently just broke and you just didn't know what happend.. --- uart/uart.h | 4 ++-- uart/uart_posix.c | 12 ++++++------ uart/uart_win32.c | 25 +++++++++++++++++++------ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/uart/uart.h b/uart/uart.h index d451c09c0..ec09149fe 100644 --- a/uart/uart.h +++ b/uart/uart.h @@ -82,9 +82,9 @@ bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_ /* Sends a buffer to a given serial port. * pbtTx: A pointer to a buffer containing the data to send. - * szTxLen: The amount of data to be sent. + * len: The amount of data to be sent. */ -bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen); +bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t len); /* Sets the current speed of the serial port, in baud. */ diff --git a/uart/uart_posix.c b/uart/uart_posix.c index 3d671102d..33bdc525f 100644 --- a/uart/uart_posix.c +++ b/uart/uart_posix.c @@ -203,16 +203,16 @@ bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t pszMaxRxLen, size_ return true; } -bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) { +bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t len) { int32_t res; - size_t szPos = 0; + size_t pos = 0; fd_set rfds; struct timeval tv; - while (szPos < szTxLen) { + while (pos < len) { // Reset file descriptor FD_ZERO(&rfds); - FD_SET(((serial_port_unix*)sp)->fd,&rfds); + FD_SET(((serial_port_unix*)sp)->fd, &rfds); tv = timeout; res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv); @@ -229,7 +229,7 @@ bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) } // Send away the bytes - res = write(((serial_port_unix*)sp)->fd,pbtTx+szPos,szTxLen-szPos); + res = write(((serial_port_unix*)sp)->fd, pbtTx+pos, len-pos); // Stop if the OS has some troubles sending the data if (res <= 0) { @@ -237,7 +237,7 @@ bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) return false; } - szPos += res; + pos += res; } return true; } diff --git a/uart/uart_win32.c b/uart/uart_win32.c index e14f31a45..16b21759b 100644 --- a/uart/uart_win32.c +++ b/uart/uart_win32.c @@ -69,6 +69,7 @@ serial_port uart_open(const char* pcPortName) { upcase(acPortName); // Try to open the serial port + // r/w, none-share comport, no security, existing, no overlapping, no templates sp->hPort = CreateFileA(acPortName, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (sp->hPort == INVALID_HANDLE_VALUE) { uart_close(sp); @@ -79,7 +80,7 @@ serial_port uart_open(const char* pcPortName) { // doesn't matter since PM3 device ignors this CDC command: set_line_coding in usb_cdc.c memset(&sp->dcb, 0, sizeof(DCB)); sp->dcb.DCBlength = sizeof(DCB); - if (!BuildCommDCBA("baud=115200 parity=N data=8 stop=1",&sp->dcb)) { + if (!BuildCommDCBA("baud=115200 parity=N data=8 stop=1", &sp->dcb)) { uart_close(sp); return INVALID_SERIAL_PORT; } @@ -119,18 +120,30 @@ void uart_close(const serial_port sp) { bool uart_receive(const serial_port sp, byte_t* p_rx, size_t pszMaxRxLen, size_t* p_rxlen) { int res = ReadFile(((serial_port_windows*)sp)->hPort, p_rx, pszMaxRxLen, (LPDWORD)p_rxlen, NULL); - if ( res == 0 ) + if ( res == 0 ) { + printf("UART:: reading from port error\n"); return false; - return ( pszMaxRxLen == *p_rxlen ); + } + bool read_test = ( pszMaxRxLen == *p_rxlen ); + if ( !read_test && *p_rxlen > 0 ) { + printf("UART:: not all data read from port len %u | read %u\n", pszMaxRxLen, *p_rxlen); + } + return read_test; } bool uart_send(const serial_port sp, const byte_t* p_tx, const size_t len) { - DWORD txlen = 0; + DWORD txlen = 0; int res = WriteFile(((serial_port_windows*)sp)->hPort, p_tx, len, &txlen, NULL); - if ( res == 0) + if ( res == 0) { + printf("UART:: writing to port error\n"); return false; - return ( len == txlen ); + } + bool write_test = ( len == txlen ); + if ( !write_test ) { + printf("UART:: not all data written to port len %u | sent %lu\n", len, txlen); + } + return write_test; } bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {