c# - How to read data from serial port and write to serial port? -
hi i'm writing application in c# connect device via rs232(com) port. need send "read" command read data , "write" command send data it.
i read articles in here , other sites , know there methods when i'm defining serial port in c#.
but question ,should concerned dtr ,rts ,... ? for? how use them?
just give idea simple function writes command byte
array on serial , reads corresponding input serial port (in example read fourth byte of value read serial port):
private string readfromserial() { try { system.io.ports.serialport serial1 = new system.io.ports.serialport("com1", 9600, system.io.ports.parity.none, 8, system.io.ports.stopbits.one); serial1.dtrenable = true; serial1.rtsenable = true; serial1.readtimeout = 3000; var messagebufferrequest = new byte[8] { 1, 3, 0, 28, 0, 1, 69, 204 }; var messagebufferreply = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 }; int bufferlength = 8; if (!serial1.isopen) { serial1.open(); } try { serial1.write(messagebufferrequest, 0, bufferlength); } catch (exception ex) { logex(ex); return ""; } system.threading.thread.sleep(100); serial1.read(messagebufferreply, 0, 7); return messagebufferreply[3].tostring(); } catch (exception ex) { logex(ex); return ""; } }
Comments
Post a Comment