this question has answer here:
- serialport not receiving data 3 answers
i using serial port application send command unicode control characters , having no success. when send same command in hyperterminal or putty communication works curious if these 2 programs write out array of bytes or unicode characters or string. if mimic do, might have success... noticed serial port property pulled out of toolbox in visual studio listed using different com port write in code, why i'm getting no response or code override that? when conducting loopback tests, output of correct command control characters cant figure out i'm doing wrong. .net 2.0 framework fyi
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.io.ports; using system.threading; namespace simpleserial { public partial class form1 : form { string rxstring; public form1() { initializecomponent(); } private void buttonstart_click(object sender, eventargs e) { serialport1.portname = "com3"; serialport1.baudrate = 9600; serialport1.parity = parity.none; serialport1.databits = 8; serialport1.stopbits = stopbits.one; serialport1.handshake = handshake.none; serialport1.readtimeout = 500; serialport1.writetimeout = 500; serialport1.open(); if (serialport1.isopen) { buttonstart.enabled = false; buttonstop.enabled = true; textbox1.readonly = false; } } const char stx = '\u0002'; const char etx = '\u0003'; readonly string pull_shelf_104 = string.format("{0}01p00104##{1}" , stx, etx); private byte[] wrapstring(string pull_shelf_104) { return system.text.encoding.ascii.getbytes(pull_shelf_104); } private void linklabel_hc1_100_linkclicked(object sender, linklabellinkclickedeventargs e) { if (serialport1.isopen) { byte[] data = wrapstring(pull_shelf_104); serialport1.write(data, 0, data.length); } } private void buttonstop_click(object sender, eventargs e) { if (serialport1.isopen) { serialport1.close(); buttonstart.enabled = true; buttonstop.enabled = false; textbox1.readonly = true; } } private void form1_formclosing(object sender, formclosingeventargs e) { if (serialport1.isopen) serialport1.close(); } private void displaytext(object sender, eventargs e) { textbox1.appendtext(rxstring); } private void serialport1_datareceived(object sender, system.io.ports.serialdatareceivedeventargs e) { rxstring = serialport1.readexisting(); this.invoke(new eventhandler(displaytext)); } } }
the comments above helped me solve issue, thank help! needed enable both rts , dts in code, listing com ports not issue although i'm sure in cases. here's code similar issues.
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.text; using system.windows.forms; using system.io.ports; using system.threading; namespace simpleserial { public partial class form1 : form { string rxstring; public form1() { initializecomponent(); } private void buttonstart_click(object sender, eventargs e) { serialport1.portname = "com3"; serialport1.baudrate = 9600; serialport1.parity = parity.none; serialport1.databits = 8; serialport1.stopbits = stopbits.one; serialport1.handshake = handshake.none; serialport1.rtsenable = true; serialport1.dtrenable = true; serialport1.readtimeout = 2000; serialport1.writetimeout = 2000; serialport1.open(); if (serialport1.isopen) { buttonstart.enabled = false; buttonstop.enabled = true; textbox1.readonly = false; } } const char stx = '\u0002'; const char etx = '\u0003'; readonly string pull_shelf_104 = string.format("{0}01p00204##{1}" , stx, etx); private byte[] wrapstring(string pull_shelf_104) { return system.text.encoding.ascii.getbytes(pull_shelf_104); } private void linklabel_hc1_100_linkclicked(object sender, linklabellinkclickedeventargs e) { if (serialport1.isopen) { byte[] data = wrapstring(pull_shelf_104); serialport1.write(data, 0, data.length); } } private void buttonstop_click(object sender, eventargs e) { if (serialport1.isopen) { serialport1.close(); buttonstart.enabled = true; buttonstop.enabled = false; textbox1.readonly = true; } } private void form1_formclosing(object sender, formclosingeventargs e) { if (serialport1.isopen) serialport1.close(); } private void displaytext(object sender, eventargs e) { textbox1.appendtext(rxstring); } private void serialport1_datareceived(object sender, system.io.ports.serialdatareceivedeventargs e) { rxstring = serialport1.readexisting(); this.invoke(new eventhandler(displaytext)); } } }
Comments
Post a Comment