c# - How to send and receive 1024 Bits? -
i'm designing interface using c# windows forms , want send , receive 1024 bits using serial port, number i'm sending in hex-form, did next:
private void button2_click(object sender, eventargs e) { int i; int = 0; byte[] mychar; mychar = new byte[128]; string m = textbox1.text; (i = 0; < 127; i++ ) { mychar[i] = convert.tobyte((m.substring(a,2)),16); += 2; } serialport1.write(mychar,0,127); }
and check if data correct or not, shorted out both transmitter , receiver can see send textbox1 shown in textbox5, problem textbox shown output ascii, , couldn't tell how convert hex form ,(see attempt commented bellow):
private void displaytext(object s, eventargs e) { textbox5.clear(); textbox5.appendtext(rxstring); //int value = convert.toint32(rxstring, 16); //string stringvalue = char.convertfromutf32(value); //textbox4.appendtext(stringvalue); }
so summarize problems: 1- code send data correct? 2- how can force textbox show output hex?
thank much.
update full code, maybe understand problem :
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace windowsformsapplication1 { public partial class form3 : form { public form3() { initializecomponent(); } string rxstring = ""; private void serialport1_datareceived(object sender, system.io.ports.serialdatareceivedeventargs e) { try { rxstring = serialport1.readexisting(); this.invoke(new eventhandler(displaytext)); } catch (system.timeoutexception) { } } private void displaytext(object s, eventargs e) { textbox5.clear(); textbox5.appendtext(rxstring); //int value = convert.toint32(rxstring, 16); //string stringvalue = char.convertfromutf32(value); //textbox4.appendtext(stringvalue); } private void picturebox2_click(object sender, eventargs e) { serialport1.close(); form1 myform = new form1(); this.close(); } private void button1_click(object sender, eventargs e) { try { if (!serialport1.isopen) { serialport1.open(); button1.enabled = false; } else { messagebox.show("port open other party!"); } } catch (unauthorizedaccessexception ex) { messagebox.show(ex.message); } } private void form3_formclosed(object sender, formclosedeventargs e) { serialport1.close(); } private void button3_click(object sender, eventargs e) { textbox1.text = "0"; textbox2.text = "0"; textbox3.text = "0"; textbox4.text = "0"; textbox5.text = ""; } private void button2_click(object sender, eventargs e) { int i; int a=0; byte[] mychar; mychar = new byte[128]; string m = textbox1.text; (i = 0; < 127; i++ ) { mychar[i] = convert.tobyte((m.substring(a,2)),16); += 2; } serialport1.write(mychar,0,127); } } }
when send data textbox1 want shown send in textbox5, can me in ? https://drive.google.com/file/d/0b5pxkmhwkwqrretumxbazda1luu/view?usp=sharing
this mcve shows how convert between byte
s , hexadecimal string
s:
class program { static void main(string[] args) { byte[] bytes = fromhexstring("0123456789abcef"); string text = tohexstring(bytes); console.write(text); console.readkey(true); } static byte[] fromhexstring(string hexstring) { byte[] result; //determine length , handle case of uneven digit count int length = hexstring.length / 2; bool = length * 2 == hexstring.length; if(!even) { length++; } //allocate memory result = new byte[length]; int offset; if(even) { offset = 0; } else { //convert first digit, if digit count uneven result[0] = convert.tobyte(hexstring[0].tostring(), 16); offset = 1; } for(int = offset; < result.length; i++) { //convert digit byte result[i] = convert.tobyte((hexstring.substring(i * 2 - offset, 2)), 16); } return result; } static string tohexstring(byte[] bytes, bool uppercase = true) { string format = uppercase ? "x" : "x"; //initialize result double capacity byte in hex notation occupies 2 chars stringbuilder result = new stringbuilder(bytes.length * 2); foreach(var @byte in bytes) { result.append(@byte.tostring(format)); } return result.tostring(); } }
Comments
Post a Comment