c# - NXT to pc bluetooth text message -
how can send bluetooth text message nxt pc , read in pc?
i use this:
byte[] byteout = new byte[65]; int = 0; try { byteout[0] = (byte)(textbox1.textlength + 5); //number bytes in output message byteout[1] = 0x0; //should 0 nxt byteout[2] = 0x00; //&h0 = reply expected &h80 = no reply expected byteout[3] = 0x9; //send bluetooth byteout[4] = 0x0; //box number - 1 byteout[5] = (byte)(textbox1.textlength + 1); //message size null terminator //copy bytes output array (i = 1; <= textbox1.textlength; i++) { byteout[(i + 5)] = convert.tobyte(asc(textbox1.text.substring((i - 1), 1))); } byteout[textbox1.textlength + 6] = 0x0; //add null terminator serialport1.write(byteout, 0, textbox1.textlength + 7); //send message // try use reading doesn't work good. gets lot of numbers , no text. char[] read1 = new char[65]; serialport1.read(read1, 0, textbox1.textlength + 7); string box1 = ""; (int i1 = 0; i1 < read1.length; ++i1) { box1 += read1[i1]; } messagebox.show(box1); } catch (exception ex) { messagebox.show(ex.tostring()); }
i try use reading doesn't work good. gets lot of numbers , no text.
what should do?
add function code:
private string nxtsendcommandandgetreply(byte[] command) { string r = ""; byte[] messagelength = { 0x00, 0x00 }; messagelength[0] = (byte)command.length; bluetoothconnection.write(messagelength, 0, messagelength.length); bluetoothconnection.write(command, 0, command.length); int length = bluetoothconnection.readbyte() + 256 * bluetoothconnection.readbyte(); (int = 0; < length; i++) r += bluetoothconnection.readbyte().tostring("x2"); return r; }
then can send number :
private void send(int a) { byte[] nxtmessage = { 0x00, 0x09, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 }; nxtmessage[2] = (byte)(0); int tmp = (int)a; (int bytectr = 0; bytectr <= 3; bytectr++) { nxtmessage[4 + bytectr] = (byte)tmp; tmp >>= 8; } nxtsendcommandandgetreply(nxtmessage); }
send text :
public void send(string s) { byte[] nxtmessage = new byte[5 + s.length]; nxtmessage[0] = 0x00; nxtmessage[1] = 0x09; nxtmessage[2] = 0x00; nxtmessage[3] = (byte)(s.length + 1); byte[] array = encoding.ascii.getbytes(s); (int bytectr = 0; bytectr < array.length; bytectr++) { nxtmessage[4 + bytectr] = array[bytectr]; } nxtmessage[4 + s.length] = 0x00; nxtsendcommandandgetreply(nxtmessage); }
get number nxt :
private int read(int mailbox) { byte[] nxtmessage = { 0x00, 0x13, 0x00, 0x00, 0x01 }; nxtmessage[2] = (byte)(mailbox - 1 + 10); string r = nxtsendcommandandgetreply(nxtmessage); return convert.toint32((r[10] + "" + r[11]), 16); }
and text nxt :
private string read_txt(int mailbox) { byte[] nxtmessage = { 0x00, 0x13, 0x00, 0x00, 0x01 }; nxtmessage[2] = (byte)(mailbox - 1 + 10); string r = nxtsendcommandandgetreply(nxtmessage); int = 0; string recieved = ""; (int = 10; ; += 2) { = int.parse(r[i] + "" + r[i+1], system.globalization.numberstyles.hexnumber); if (a == 0) break; recieved += char.convertfromutf32(a); } return r; }
checkout http://geekbrothers.org/index.php/categories/electronics-computers/20-video-controlled-robot see great use of sending numbers nxt bluetooth , download source code. , checkout http://geekbrothers.org/index.php/teachings put complete tutorial send , receive bluetooth , nxt soon. if have other problem can use geekbrothers contact-us form or can send email info@geekbrothers.org
Comments
Post a Comment