This is probably trivial but I cannot get why I dont get the read back in my program. It seem to work fine for all the "complex stuff", and it says it has read 1 (character/byte), but I cannot get hold of it; it seems to be typ-matching problems (this is really weird the compiler g++ (i.e. gcc) btw ).
How I alter different variants of Buf (as pointer, char, char array, etc) I cannot get hold of the input.
SO below is now the stripped code and sync read version. Which also should compile.
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <windows.h>
using namespace std;
//**********************************
//******* M A I N ****************
//**********************************
int main()
{
HANDLE hComm;
int choice;
// non overlap test case (2nd last par = 0)
hComm = CreateFile( "COM4",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
0);
if (hComm == INVALID_HANDLE_VALUE){
// error opening port; abort
printf("open error COM4\n");
}
else
printf("COM4 open");
printf("\n Hi, this is a UART attempt:"); scanf("%d", &choice);
// build the control block
DCB dcb={0};
printf("******dcb******\n");
dcb.DCBlength = sizeof(dcb);
if ( !GetCommState(hComm, &dcb)) printf("Get DCB error"); // I dont think this should be needed ?
if ( ! BuildCommDCB( "4800,n,8,2" , &dcb ) ) {
// error
printf("COM4 buidDCB -- error\n");
return(1);
}
printf("****here*****\n");
// put the control block into action
if (! SetCommState( hComm, &dcb ) ){
// error
printf("COM4 setCommState -- error:%d \n",(int)GetLastError() );
return(1);
}
printf("seem successfull \n");
/*************************************READ non-ASYNC TESTING ************************************/
/********************************************************************************************/
char Buf[1];
/* initiate waiting for reading on UART */
DWORD dwRead;
// Issue rea
if ( ! ReadFile(hComm, Buf, 1 , &dwRead, 0)) {
// Error in communications; report it.
printf("ReadFile --- error:%d",(int)GetLastError());
}
else {
// read completed
printf("read <%d>---%o--- \n",(int)dwRead,Buf[0]);
}
return 0;
} //*** end main ************************************
You are going to kick yourself: The problem is that the final printf (to display the value) should be:
printf("--- immediate read <%d>---%o--- \n",(int)dwRead,Buf[0]);
Note the trailing [0] on Buf.
Alternatively, you could declare Buf as:
char Buf;
and then the call would be:
if ( ! ReadFile(hComm, &Buf, 1, &dwRead, &osReader)) {
(with a & on Buf.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With