python - C# .NET serial port connects, doesn't read or write -
python - C# .NET serial port connects, doesn't read or write -
i'm using c# , .net 4.5, visual studio 2012 compiler/ide open , interact serial port. code designed connect qsb quadrature-to-usb converter digital.
here code i'm using open port , connect.
this.port = new serialport(); this.port.baudrate = 230400; this.port.portname = "com9"; this.port.parity = parity.none; this.port.handshake = handshake.none; this.port.databits = 8; this.port.stopbits = stopbits.one; this.port.open();
setting breakpoint after this.port.open() allows me verify serial port indeed connected. in section of code, next called in response button push:
this.port.writeline("w168");
this command *should cause hardware spin motor, , in fact if send command using putty, or using python script wrote (both using same settings c# code does). yet nil happens. can open port in putty or python , execute command expected results, , run c# code , nil happens.
am missing c# specific prevents working?
for it's worth, here working python code:
ser = serial.serial("com9", 230400, timeout=1) ser.write(b"w168\n")
link pyserial documentation: http://pyserial.sourceforge.net/pyserial_api.html#classes
default values fields mentioned in c# code not mentioned in python phone call above are:
bytesize = 8 parity = none stopbits = one xonxoff = false rtscts = false dsrdtr = false
when working serial ports in c# there 1 thing remember when establishing connection. if set handshake value none this:
this.port.handshake = handshake.none;
then need set few more parameters connection completed, , are:
this.port.dtrenable = true; this.port.rtsenable = true;
the reaso because dtrenable means this:
data terminal ready (dtr) signal
msdn explains dtr means this:
data terminal ready (dtr) typically enabled during xon/xoff software handshaking , request send/clear send (rts/cts) hardware handshaking, , modem communications.
rtsenable means this:
request send (rts) signal
msdn explains rts means this:
the request transmit (rts) signal typically used in request send/clear send (rts/cts) hardware handshaking.
together these 2 parameters handle handshaking of serial port communications without having define between master , slave.
c# python .net serial-port
Comments
Post a Comment