Click here to Skip to main content
15,741,947 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

I have a program that communicates to a device through the serial port. I am having difficulties with one section however. I am getting what looks like a right angle symbol when I run through the calculated hexadecimal commands. Here are the two sections of code (main section and function).

Main Section:
private: System::Void poll_Click(System::Object^  sender, System::EventArgs^  e)
                int i, end;
                double a = 1.58730159;
                String^ portscan = "port";
                String^ translate;
                std::string portresponse [65];
                std::fill_n(portresponse, 65, "Z");

                for (i=0;i<63;i++)
                        // Command 0 generator
                        int y = 2;
                        y += i;
                        std::string command0[10] = {"0xFF", "0xFF", "0xFF", "0xFF", "0xFF", "0x02", dectohex(i), "0x00", "0x00", dectohex(y)};
// The two "dectohex" values above is where I get the odd symbol
                        for (end=0;end<10;end++)
                            portscan = marshal_as<String^>( command0[end] );

                        translate = (this->_serialPort->ReadLine());
                        MarshalString(translate, portresponse [i]);
                        if(portresponse [i] != "Z")
                        a += 1.58730159;


string dectohex(int i)
            string hexidecimal = "";
            char hex_string[10] = "";
            hexidecimal = sprintf (hex_string, "0x%02X", i);
            return hexidecimal;

I am assuming that there is something fairly simple that I am missing. Any and all help is appreciated.
Sergey Alexandrovich Kryukov 2-Aug-13 14:03pm    
First of all, why would you use C++ library things like sprintf with C++/CLI? Use .NET BCL: object.ToString, int.Parse, etc... Why are you mixing std::string with System::String, etc.?
Richard MacCutchan 6-Aug-13 10:36am    
I get the feeling that you really don't understand how this device works. What you probably should be sending is an actual hexadecimal byte value rather than a hex string representation of that value.
Bandzerf 6-Aug-13 10:57am    
I was told that the device would recognize hex strings but in the case that the information is incorrect, where should I look for instructions on how to send hexadecimal byte values? Or do you have additional advice based on the code that I have so far?
Richard MacCutchan 6-Aug-13 11:07am    
See Solution 4 below.
Richard MacCutchan 6-Aug-13 11:11am    
Take a look at this paper on serial I/O which may also help to explain things better.

Well I am making an assumption, and without seeing the documentation for the device I cannot be 100% certain, but I would expect it to be (in normal C/C++) something like:
int command0[10] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, i, 0x00, 0x00, y };

for (end=0; end<10; end++)

Incidentally, is there a good reason why you are using C++/CLI rather than unmanaged C++?
Share this answer
Bandzerf 6-Aug-13 11:22am    
I was essentially handed a serial communication program and told to make it work for the new device (the documentation didn't help, it was poorly written), it was not my choice. Although, to be honest I am new enough that I don't recognize the difference between the two (granted I haven't looked yet).

When I try and use this code I hit two errors. The first is in the array (command0 taking the dectohex function) itself with the string to int error C2440, which I can figure out. The second one is one I have hit before, an error C2664 because I'm trying to send an int instead of a System String^. Any pointers since I'm assuming we don't want to convert it to a string and have the string representation sent again?
Richard MacCutchan 6-Aug-13 11:36am    
I am not very familiar with C++/CLI so cannot offer any particularly useful suggestions as to how it should be modified. I would only go back to my original suggestion that you check the documentation for the device, to see exactly what data it expects in its command strings. Only then can you be sure that any changes to your code are the right ones.
Bandzerf 6-Aug-13 11:40am    
Okay, thank you for the suggestions and the code segment. I will keep it in mind if the string representations don't function as expected.
Please see my comment to the question. What you are missing is really fairly simple, but you make it much less simple if you mix up libraries for C++ with that of .NET BCL, for C++/CLI. Why?

Now, the functions like "decimal to hexadecimal" or visa versa cannot be found in typical libraries, just because they would make no practical sense. Do you really understand that numeric types cannot be "decimal" or "hexadecimal"? They are all binary. "Decimal" or "hexadecimal" only should appear when you want to represent data the way you can show them on screen. You should work with data itself, not with strings representing data. This simple idea should give you the understanding on what should you do when string are involved. You can interpret a string as the one carrying numeric data, and this process is referred as parsing and may or may not be successful. And creation a string from a numeric type is just a matter of string formatting. There are no "conversions", and it should not be.

Let's consider your commands. Let's assume they are of the type int (could be byte or any other integer type). Then you have the methods System::Int32::Parse and System::Int32.ToString:[^],[^].

See also:[^],[^].

Share this answer
Bandzerf 6-Aug-13 9:14am    
Thank you for your input
Sergey Alexandrovich Kryukov 6-Aug-13 11:13am    
Sure. Are you accepting the answer formally?
Bandzerf 6-Aug-13 12:02pm    
I am not accepting it formally. I was looking for an answer that solves the question given what I provided. You acknowledged the existence of a solution but then sidestepped it to tell me how I shouldn't be doing it. I do appreciate your input, but it is not what answered my question within the context of my program.
Sergey Alexandrovich Kryukov 6-Aug-13 12:33pm    
Well, this is because some activity on your side is required. What, no follow-up questions?
string dectohex(int i)
        char hex_array[10];
        sprintf (hex_array, "0x%02X", i);
        string hex_string(hex_array);
        return string(hex_string);

I needed to convert the char array to a string. David Yaw alerted me to this here:[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900