c++ - Send Hex Bytes To A Serial Port -
i attempting send hexadecimal bytes serial com port. issue segment sends command apparently wants system string instead of integer (error c2664 "cannot convert parameter 1 'int' 'system::string ^'). have looked way send integer instead have had no luck. (i have tried sending string representations of hexadecimal values, device did not recognize commands)
main part of code
private: system::void poll_click(system::object^ sender, system::eventargs^ e) { int i, end; double = 1.58730159; string^ portscan = "port"; string^ translate; std::string portresponse [65]; std::fill_n(portresponse, 65, "z"); (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)}; (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; += 1.58730159; } } }
here 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; }
thank time , effort!
this solved 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]);
you cannot sent integer on serial line.... can sent bytes (7-8 bit)!
you need choose want do:
sent characters: "number" 12 converted bytes
_serialport->write(12.tostring()); // => 0x49, 0x50
sent integer (4 bytes) little endian
auto data = system::bitconverter::getbytes(12); _serialport->write(data, 0, data->length); // => 0x0c, 0x00, 0x00, 0x00
or write single byte:
auto data = gcnew array<system::byte> { 12 }; _serialport->write(data, 0, data->length); // => 0x0c
or write 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
Comments
Post a Comment