Mi problema es el siguiente estoy intentando leer los datos que recibo de un indicador digital de peso vía serial a mi pc. Los datos son interpretados por el dato tipo BYTE. El codigo que estoy utilizando es uno que encontre en la web es el siguiente:
Código
#ifndef serie_h #define serie_h #include <windows.h> // Open Serial port HANDLE OpenSerialPort( char *psPort, // "COM1","COM2" DWORD bBaudRate, // CBR_9600, CBR_19200. CBR_56000 BYTE bByteSize, // 7,8 BYTE bParity, // NOPARITY, EVENPARITY, ODDPARITY BYTE bStopBits, // ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS DWORD ReadTimeout // Programmable Read Timeout ); // Send a byte BOOL SerialSendByte(HANDLE hPort, BYTE byte); // return TRUE: Send OK // Receive a byte BOOL SerialReceiveByte(HANDLE hPort, BYTE *pbyte, BOOL *pTimeout); // return TRUE & *pTimeout TRUE Send OK // return TRUE & *pTimeout FALSE Timeout // return FALSE Receive Error // Close Serial Port BOOL CloseSerialPort(HANDLE hPort); // Write&Read strings from serial port using standard file I/O functions /*BOOL ReadFile( HANDLE hFile, // handle of file or serial port to read LPVOID lpBuffer, // address of buffer that receives data DWORD nNumberOfBytesToRead, // number of bytes to read LPDWORD lpNumberOfBytesRead, // address of number of bytes read LPOVERLAPPED lpOverlapped // NULL ); */ /* BOOL WriteFile( HANDLE hFile, // handle to file or serial port to write to LPCVOID lpBuffer, // pointer to data to write to file DWORD nNumberOfBytesToWrite, // number of bytes to write LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written LPOVERLAPPED lpOverlapped // NULL ); */ #endif ///Archivo cpp #include "serie.h" HANDLE OpenSerialPort( char *psPort, // "COM1","COM2" DWORD dwBaudRate, // CBR_9600, CBR_19200. CBR_56000 BYTE bByteSize, // 7,8 BYTE bParity, // NOPARITY, EVENPARITY, ODDPARITY BYTE bStopBits, // ONESTOPBIT, ONE5STOPBITS, TWOSTOPBITS DWORD Timeout // Timeout ) { HANDLE hPort; // port handler DCB dcbPort; // Port configuration COMMTIMEOUTS commTimeouts; // Port Timeouts DWORD dwError; // Error code // Open Serial Port hPort=CreateFile( psPort, // pointer to name of the file GENERIC_READ | GENERIC_WRITE, // access (read-write) mode 0, // share mode: 0 the object cannot be share NULL, // pointer to security attributes: NULL the handle cannot be inherited OPEN_EXISTING, // how to create: Comx exist 0, // file/port attributes NULL); // handle to file/port with attributes to copy // If the function fails, the return value //is INVALID_HANDLE_VALUE if ( hPort == INVALID_HANDLE_VALUE ) { dwError = GetLastError (); // Flush error code return hPort; } // Set Port Configuration // Delete DCB configuration FillMemory(&dcbPort, sizeof(dcbPort), 0); dcbPort.DCBlength = sizeof(dcbPort); // Current DCB in use for the communications port GetCommState (hPort, &dcbPort); // Update DCB with new parameters dcbPort.BaudRate = dwBaudRate; dcbPort.ByteSize = bByteSize; dcbPort.Parity = bParity; dcbPort.StopBits = bStopBits; // Fixed parameters (Disable XON-XOFF and modem handshake) dcbPort.fBinary = TRUE; // Binary mode; no EOF check dcbPort.fParity = TRUE; // Enable parity checking dcbPort.fOutxCtsFlow = FALSE; // No CTS output flow control dcbPort.fOutxDsrFlow = FALSE; // No DSR output flow control dcbPort.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type // Raises the DTR line when the device is opened dcbPort.fDsrSensitivity = FALSE; // DSR sensitivity dcbPort.fTXContinueOnXoff = TRUE; // XOFF continues Tx dcbPort.fOutX = FALSE; // No XON/XOFF out flow control dcbPort.fInX = FALSE; // No XON/XOFF in flow control dcbPort.fErrorChar = FALSE; // Disable error replacement dcbPort.fNull = FALSE; // Disable null stripping dcbPort.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control // Raises the RTS line when the device is opened dcbPort.fAbortOnError = FALSE; // Do not abort reads/writes on // error // Set new configuration if (!SetCommState (hPort, &dcbPort)) { dwError = GetLastError (); // Flush error code CloseSerialPort(hPort); hPort = INVALID_HANDLE_VALUE; return hPort; } // Set Port Timeouts // Timeouts preparation MORE INFORMATION IN WIN32 API: COMMTIMEOUTS commTimeouts.ReadIntervalTimeout = 0; // Specifies the maximum time, in milliseconds, allowed to elapse between the arrival // of two characters on the communications line // A value of zero indicates that interval time-outs are not used. commTimeouts.ReadTotalTimeoutMultiplier = 50; // Specifies the multiplier, in milliseconds, used to calculate // the total time-out period for read operations. // For each read operation, this value is multiplied // by the requested number of bytes to be read. commTimeouts.ReadTotalTimeoutConstant = Timeout;// Specifies the constant, in milliseconds, used to calculate the total // time-out period for read operations // commTimeouts.WriteTotalTimeoutMultiplier = 10; // Specifies the multiplier, in milliseconds, used to calculate the // total time-out period for write operations. // For each write operation, this value is multiplied // by the number of bytes to be written. commTimeouts.WriteTotalTimeoutConstant = 1000; // Specifies the constant, in milliseconds, used to calculate the total time-out period // for write operations // See de win32 api for more information // Set Timeouts if (!SetCommTimeouts (hPort, &commTimeouts)) { dwError = GetLastError (); // Flush error code CloseSerialPort(hPort); hPort = INVALID_HANDLE_VALUE; return hPort; } return hPort; } BOOL SerialSendByte(HANDLE hPort, BYTE byte){ BOOL bRes; DWORD dwError, dwNumBytesWritten=0; bRes=WriteFile( hPort, // handle to file or serial port to write to &byte, // pointer to data to write to file 1, // number of bytes to write &dwNumBytesWritten, // pointer to number of bytes written NULL // NULL ); if ((!bRes)||(dwNumBytesWritten!=1)){ dwError = GetLastError (); // Flush error code } return bRes; } BOOL SerialReceiveByte(HANDLE hPort, BYTE *pbyte, BOOL *pTimeout){ BOOL bRes; DWORD dwError, lpNumberOfBytesRead=0; *pTimeout=FALSE; bRes=ReadFile( hPort, // handle of file or serial port to read pbyte, // address of buffer that receives data 1, // number of bytes to read &lpNumberOfBytesRead, // address of number of bytes read NULL // NULL ); if (!bRes) { dwError = GetLastError (); // Flush error code } if ((bRes)&&(lpNumberOfBytesRead==0)){ *pTimeout = TRUE; } return bRes; } BOOL CloseSerialPort(HANDLE hPort){ BOOL bRes; DWORD dwError; bRes=CloseHandle(hPort); if (!bRes) { dwError = GetLastError (); // Flush error code } return bRes; } /////////////////////////////////////////////////////////////////////////////////////////////////// #include "serie.h" #include "windows.h" #include "stdio.h" #include <stdint.h> int main(){ HANDLE hPort; BOOL bRes; BYTE byte; BOOL timeout; hPort=OpenSerialPort("COM1",CBR_9600,8,NOPARITY,TWOSTOPBITS,5000); if (hPort==INVALID_HANDLE_VALUE){ return 1; } while(1){ bRes=SerialReceiveByte(hPort,&byte,&timeout); if (!bRes) { break; } if (timeout){ } else { } } if (!bRes) { return 1; } CloseSerialPort(hPort); return 0;
bien lo que obtengo de salida en la pantalla de la pc es lo siguiente:C°xxCx° Cx°Cx xxx° xCx x°xC C C C C C C C°xxCx° y este patron se repite constantemente. En este caso esos valores simbolizan el numero 0 que muestra el indicador digital de peso quisiera saber como puedo convertir esos valores del tipo BYTE al valor 0 Kg que muestra el indicador digital de peso pero en la pc.
Gracias de antemano por la ayuda