Send Hex Bytes To A Serial Port

11,723

You cannot sent an integer over a serial line.... you can only sent BYTES (7-8 bit)!

You need to choose what you want to do:

  • Sent characters: So the "number" 12 will be converted into the bytes

    _serialPort->Write(12.ToString());
    // => 0x49, 0x50
    
  • Sent the integer (4 bytes) as little endian

    auto data = System::BitConverter::GetBytes(12);
    _serialPort->Write(data, 0, data->Length);
    // => 0x0c, 0x00, 0x00, 0x00
    
  • Or you write just a single byte:

    auto data = gcnew array<System::Byte> { 12 };
    _serialPort->Write(data, 0, data->Length);
    // => 0x0c
    
  • Or write an byte array:

    auto data = gcnew array<System::Byte> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0xBF, 0x00, 0x00, 0xBD };
    _serialPort->Write(data, 0, data->Length);
    // => 0xFF 0xFF 0xFF 0xFF 0xFF 0x02 0xBF 0x00 0x00 0xBD
    
Share:
11,723
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I am attempting to send hexadecimal bytes to a serial com port. The issue is that the segment that sends the command apparently wants a system string instead of an integer (error C2664 "cannot convert parameter 1 from 'int' to 'System::String ^'). I have looked for a way to send an integer instead but have had no luck. (I have tried sending string representations of the hexadecimal values, but the device did not recognize the commands)

    Main part of Code

    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=1;i<64;i++)
                {
                    if(this->_serialPort->IsOpen)
                    {
                        // Command 0 generator
                        int y = 2;
                        y += i;
                        int command0[10] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, dectohex(i), 0x00, 0x00, dectohex(y)};
    
                        for (end=0;end<10;end++)
                        {
                            this->_serialPort->WriteLine(command0[end]);
                        }
    
                        translate = (this->_serialPort->ReadLine());
                        MarshalString(translate, portresponse [i]);
                        if(portresponse [i] != "Z")
                        {
                            comboBox7->Items->Add(i);
                        }
                        this->progressBar1->Value=a;
                        a += 1.58730159;
                    }
                }
    
    
         }
    

    Here is the function dectohex:

        int dectohex(int i)
             {
                int x = 0;
                char hex_array[10];
                sprintf (hex_array, "0x%02X", i);
                string hex_string(hex_array);
                x = atoi(hex_string.c_str());
                return x;
             }
    

    This is what solved my problem, courtesy of Jochen Kalmbach

    auto data = gcnew array<System::Byte> { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0xBF, 0x00, 0x00, 0xBD };
    _serialPort->Write(data, 0, data->Length);
    

    Replaced this

    this->_serialPort->WriteLine(command0[end]);
    
  • Admin
    Admin over 10 years
    So no matter what approach I take I can't send, for example, 0xFF 0xFF 0xFF 0xFF 0xFF 0x02 0xBF 0x00 0x00 0xBD as it appears here? I am trying to send these values to an external device to give me information about that same device. Is this additional information helpful?
  • Jochen Kalmbach
    Jochen Kalmbach over 10 years
    What is the problem with : `auto data = gcnew array<System::Byte>(10) { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0xBF, 0x00, 0x00, 0xBD}; _serialPort->Write(data, 0, data->Length);´
  • Admin
    Admin over 10 years
    I was testing it and it sets up no issues, but when I run the program it gives me an output of "FF FF FF A0 03 00 6F 00" according to a serial port monitor. It seems like to get one iteration of FF being sent you need to send 2 0xFF, which I am at a loss for.
  • Jochen Kalmbach
    Jochen Kalmbach over 10 years
    I think you are using the worng baud rate or wrong settings for the port... can you show us your settings??? and the requested settings?
  • Admin
    Admin over 10 years
    I will find where the settings are and add them above (are you wanting the physical settings for the port, the program settings or both?). As for the baud rate (in program), it is variable based on what the user chooses although none of them yield the correct results.
  • Jochen Kalmbach
    Jochen Kalmbach over 10 years
    Relevant are: BaudRate, DataBits, DiscardNull, DtrEnable, Handshake, Parity, ParityReplace, RtsEnable, StopBits, WriteTimeout . See also: msdn.microsoft.com/en-us/library/…
  • Admin
    Admin over 10 years
    Thank you so much! I had errors in how the Parity and DataBits were set up. I set them back to the None and 8 respectively and it works flawlessly.