|
Thank you very much. I find this finally.... 
|
|
|
|
|
You're welcome!
This is why I like message boards. An answer given on the board helps someone years after.
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
Hi guys,
I want to display the content of an array of type unsigned char which contains ascii caracters in a Windows form textbox. After hours of trying to do this, I am asking for some help. I am using Visual C++ Express 2008.
The array is filled by a function who reads data from the serial port.
I get a pointer to that array of unsigned char from a defined function.
something like this :
unsigned char *RxBuffer; // Filled by a function reading COM port.
I want to Display it in a Textbox, something like this :
textBox1->Text = RxBuffer.ToString();
When I do this, it diyplays the decimal value of my ascii code.
For example if the first ascii code in my array is 0x41, I would like "A" to be displayed in the textbox. But instead it displays "51" which is the decimal value.
Any advice? Thx in advance.
|
|
|
|
|
you may try the following code, i am not sure that it works , but i think this is one of the ways to solve ur problem , try it !
CString str;
str.Format("%s %x",str, RxBuffer);
now str contains converted data
i think now you can display str in your text box
|
|
|
|
|
kapardhi wrote: CString str;
str.Format("%s %x",str, RxBuffer);
now str contains converted data
Nope. Can you spot the problem in your code?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
My code looks like this :
cRxBuff1 is the array which I want to diplay in the textbox.
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
CUart ComPort;
ComPort.InitComPort(0x0F);
DWORD dwBytesRead1;
unsigned char cRxBuff1[EVAL_READ_BUFFER_LEN];
if(ReadFile(ComPort.MyHandleCOM1,cRxBuff1,EVAL_READ_BUFFER_LEN,&dwBytesRead1,NULL))
{
if(dwBytesRead1 > 0)
{
textBox1->Text= ???;
}
}
|
|
|
|
|
...
if(dwBytesRead1 > 0)
{
int i;
Byte b;
for (i=0; i<dwbytesread;> {
b = cRxBuff1[i];
textBox1->Text += b.ToString("x02");
}
}
...
Anyway, if you choose the right forum [^] then you'll have the chance of better answers.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Almost there, but it is still not diplaying text.
Like this it diplay the Hexadecimal value. 0x41 when I would like 'A' displayed.
If I change
textBox1->Text += b.ToString("x02");
to
textBox1->Text += b.ToString("s");
I get an unhandled exception error due to invalid format.
Sorry if it is not in the good forum, I thought this was related to Visual C++ problem, because my problem is I am not able to display text in a windows form textbox.
What could be the good forum in your opinion?
|
|
|
|
|
textBox1->Text += (Char) cRxBuff1[i];
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks a lot, it is working now.
I was sure to have tried this already.
In fact I tried the following just to test if I can display a caracter
textBox1->Text = (Char) cRxBuff1[0];
How come we can do
textBox1->Text += (Char) cRxBuff1[i];
but not
textBox1->Text = (Char) cRxBuff1[i];
??
|
|
|
|
|
Because a string is a sequence of characters.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
which header file do I need to include to use CString?
|
|
|
|
|
|
Nah it hasn't. That is why I was wondering how this can work. I saw some examples with stuff like this. At least the compiler accept it. I tried to do some thins like this too :
textBox1->Text = ToString(RxBuffer);
But its not working either.
I am not used to manipulate strings, I do mostly C code, and I have no clue how to do this.
|
|
|
|
|
Hi !!
My application is related with Smart card readers.
I actually use CreateFile() to create a handle to the port to which the reader is connected.
I use WriteFile () to send data to reader & receive response from reader using ReadFile ().
I compare the response sent by a reader, to detect whether it is a valid Reader or not
now my problem is if i donot connect a reader to the port, and use ReadFile (), it executes successfully, although there is no reader connected to the port.
So is there a function to detect whether a reader is connected to the port or not?
|
|
|
|
|
I will suggest you to send some basic command to reader.
If reader responds reader is there. Else you can time out and say no reader is connected.
Regards,
Sandip.
|
|
|
|
|
Presently i am doing that but the reader is responding at different time delays at different times at different computers.
but if i keep the maximum delay, the performance of the application decreases !!
|
|
|
|
|
What is the timeout you are using now??
Regards,
Sandip.
|
|
|
|
|
// Set the time outs for the COM port
COMMTIMEOUTS CommTimeoutsObj;
commTimeoutsObj.ReadIntervalTimeout = 0;
commTimeoutsObj.ReadTotalTimeoutMultiplier = 10;
commTimeoutsObj.ReadTotalTimeoutConstant = 2;
commTimeoutsObj.WriteTotalTimeoutMultiplier = 0;
commTimeoutsObj.WriteTotalTimeoutConstant = 0;
// Set the above settings to the selected COM port.
bStatus = SetCommTimeouts (handle, &commTimeoutsObj);
so the ReadFile () waits for ((No of bytes to be read * 10) + 2) milliseconds
this is the time i wait !!
|
|
|
|
|
kapardhi wrote: so the ReadFile () waits for ((No of bytes to be read * 10) + 2) milliseconds
How many bytes do you expect ??
If the number of bytes is less time out should not be much.
There must be some handshaking commands that reader must be supporting.
Each time you need to do some operation with reader you can send these commands which have less number of bytes in response.
does it help??
Regards,
Sandip.
|
|
|
|
|
Sorry !!
Not yet satisfied!
Presently i send commands and wait for response , but i need some better approach !
Is there any command that gets all the details of the device connected to the serial port ??
|
|
|
|
|
Sounds strange that you are able to open a resource that is not connected to the system...
What "filename" are you providing as argument in the call to ::CreateFile() ?
Is ::CreateFile() actually returning a valid handle, i.e. it doesn't return INVALID_HANDLE_VALUE which is defined as -1?
Is ::ReadFile() really returning TRUE ?
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Suppose i just connect a Serial port cable to computers Serial port, and at the other end a reader is not connected, now a handle is successfully created.
now if i use ReadFile() it waits infinitely to receive a response !
on the other hand if i set some Time outs using SetCommTimeouts ()
the Readfile () successfully executes as it times out, i.e it doesnot receive response within stipulated time period, but it doesnot give error if the reader is not connected, it reads Zero bytes
|
|
|
|
|
kapardhi wrote: Serial port cable to computers Serial port
Ahh, that explains it.
I assumed it was a USB device, sorry about that.
kapardhi wrote: now if i use ReadFile() it waits infinitely to receive a response !
Don't know if you by this mean that the call blocks, but if that's a problem you can use overlapped mode instead. I would recommend overlapped mode whether this is a problem or not. However, the read request to the driver will not be completed until the timeout set by ::SetCommTimeouts() expires.
If the card reader would set one of the signals, e.g. DSR or CTS, to an active state it would be possible to use ::GetCommModemStatus() to check if the device is present. Have you investigated this possibility?
"It's supposed to be hard, otherwise anybody could do it!" - selfquote "High speed never compensates for wrong direction!" - unknown
|
|
|
|
|
Sorry !!
Not yet satisfied!
Presently i send commands and wait for response , but i need some better approach !
|
|
|
|