Click to See Complete Forum and Search --> : Sending Read Buffer contents to ListBox


gbrooks3
July 14th, 2006, 10:45 AM
Hi, i am attempting to communicate with a device via comport. I can:

Open
Read
Write
Setup DCB
etc...

All fine. However, i want to display the bytes i have read in a ListBox, i really have no idea how to do it :(

Can someone point out how i could implement it here...


if((iTransferred = ReadCOMBytes(com, lpBuf, 8, dwTimeout)) < 8)
{

HWND hWndList = GetDlgItem(hWnd, IDC_LIST2);
LPSTR lpStr10 = "Received (%u bytes): "; //Here i want to show itransferred, i.e. the bytes recieved.
LPSTR lpStr11 = "Unexpected base-band value!";

SendMessage(hWndList, LB_ADDSTRING, 0, ( LPARAM ) lpStr10 );
SendMessage(hWndList, LB_ADDSTRING, 0, ( LPARAM ) lpStr11 );

}



Many thanks :)

golanshahar
July 14th, 2006, 12:39 PM
You can try this:

TCHAR szTransfered[MAX_PATH]={0};
sprintf(szTransfered,"%d",iTransferred );
::SendMessage(hWndList, LB_ADDSTRING, 0, ( LPARAM ) szTransfered );


Cheers

gbrooks3
July 14th, 2006, 01:23 PM
Thanks for the reply, i am right in saying that that section of code will only send the number of bytes recieved?

I would like to actually display what those bytes are....Any ideas mate?

Cheers.

golanshahar
July 14th, 2006, 02:41 PM
Thanks for the reply, i am right in saying that that section of code will only send the number of bytes recieved?

I would like to actually display what those bytes are....Any ideas mate?

Cheers.

Well what is the variable that holds the bytes? And what type is it? If it lpBuf and it is declared as simple BYTE array you can simply pass it to SetDlgItemText().

Cheers

gbrooks3
July 14th, 2006, 02:57 PM
Hi, thanks for the help, i am really still new to this, if you could give me a quick "for instance" example, that would be great.

I Dont want to use SetDlgItemText, as i want to just show the bytes recieved on a new line in my listbox :)

I did try using lpBuf as in: ::SendMessage(hWndList, LB_ADDSTRING, 0, ( LPARAM ) lpBuf );

But all that was displayed in the listbox was the Euro (currency) sign.

Your help is greatly appreciated :)

golanshahar
July 14th, 2006, 03:03 PM
Hi, thanks for the help, i am really still new to this, if you could give me a quick "for instance" example, that would be great.

I Dont want to use SetDlgItemText, as i want to just show the bytes recieved on a new line in my listbox :)

I did try using lpBuf as in: ::SendMessage(hWndList, LB_ADDSTRING, 0, ( LPARAM ) lpBuf );

But all that was displayed in the listbox was the Euro (currency) sign.

Your help is greatly appreciated :)

Are you sure those are not the bytes that are sent? What do you expect to get?

Cheers

gbrooks3
July 14th, 2006, 03:19 PM
Yep the bytes are stored in lpBuf i get 8 bytes (using com logger to check)...

Any ideas?

golanshahar
July 14th, 2006, 03:34 PM
Yep the bytes are stored in lpBuf i get 8 bytes (using com logger to check)...

Any ideas?

That’s ok, my question was about the point you said that you add the lpBuf to the list box and saw a euro currency sign, so my question is why you think this is not the correct bytes that you are getting? What do you expect to get?

Cheers

gbrooks3
July 14th, 2006, 03:43 PM
ah, i see. Well, according to the Comport logger, i should get:

80 00 03 01 FF FF FF FF

Going back over an older piece of my code for command line, i used the routine:



void PrintBuffer(unsigned char lpBuf[], int iLength) {
for(int i=0;i<iLength;i++) printf("%02X ", lpBuf[i]);
}



I cant quite see how to relate that to my listbox though :( Sorry :(

golanshahar
July 15th, 2006, 01:15 AM
in that case you can try something like that:

char szOutPut[MAX_PATH]={0};

for(int i=0;i<iLength;i++)
{
char szTmp[64]={0};
sprintf(szTmp,"%02X ", lpBuf[i]);
strcat(szOutPut,szTmp);
}

::SendMessage(hWndList, LB_ADDSTRING, 0, ( LPARAM ) szOutPut);


Cheers

gbrooks3
July 15th, 2006, 04:59 AM
Perfect! Thats just what i wanted, i wish someday i can be as knowledgeable at this as you :)

Thanks!

Just to clarify:



void PrintBuffer(HWND hWnd, unsigned char lpBuf[], int iLength)
{
char szOutPut[MAX_PATH]={0};

for(int i=0;i<iLength;i++)
{
char szTmp[64]={0};
sprintf(szTmp,"%02X ", lpBuf[i]);
strcat(szOutPut,szTmp);
}

HWND hWndList = GetDlgItem(hWnd, IDC_LIST2);
::SendMessage(hWndList, LB_ADDSTRING, 0, ( LPARAM ) szOutPut);
}


Then whenever you need to display the bytes in lpBuf just call: PrintBuffer(hWnd, lpBuf, iTransferred);

Thanks again, i will have a play around with this some more, i am sure i will have some more questions :)

golanshahar
July 15th, 2006, 05:52 AM
You are welcome :wave:

Cheers