title: Send Hex Bytes To A Serial Port
body: <p>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)</p>
  
  <p>Main part of Code</p>
  
  <pre><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&lt;64;i++)
              {
                  if(this-&gt;_serialPort-&gt;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&lt;10;end++)
                      {
                          this-&gt;_serialPort-&gt;WriteLine(command0[end]);
                      }
  
                      translate = (this-&gt;_serialPort-&gt;ReadLine());
                      MarshalString(translate, portresponse [i]);
                      if(portresponse [i] != "Z")
                      {
                          comboBox7-&gt;Items-&gt;Add(i);
                      }
                      this-&gt;progressBar1-&gt;Value=a;
                      a += 1.58730159;
                  }
              }
  
  
       }
  </code></pre>
  
  <p>Here is the function dectohex:</p>
  
  <pre><code>    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;
           }
  </code></pre>
  
  <hr>
  
  <p>This is what solved my problem, courtesy of Jochen Kalmbach</p>
  
  <pre><code>auto data = gcnew array&lt;System::Byte&gt; { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0xBF, 0x00, 0x00, 0xBD };
  _serialPort-&gt;Write(data, 0, data-&gt;Length);
  </code></pre>
  
  <p>Replaced this</p>
  
  <pre><code>this-&gt;_serialPort-&gt;WriteLine(command0[end]);
  </code></pre>