Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
102 views
in Technique[技术] by (71.8m points)

c# - SerialPort.Open() gives Exception for set_DtrEnable

I'm trying to test a program which should connect to some hardware through serial ports. I've built a "fake" hardware able to reproduce the real one. Then i tried setting up some local port connection using tty0tty (i'm on Ubuntu) which gave me this :

(/dev/pts/1) <=> (/dev/pts/2)

However, when i tried to open the port on my test program like this :

var port = new ConcreteSerialPort("/dev/pts/2", 9600, Parity.None, 8, StopBits.One);
port.Open();

(ConcreteSerialPort is just an abstraction but all calls go directly to a real SerialPort.) I get this exception :

Error : System.IO.IOException: Invalid argument
   at System.IO.Ports.SerialStream.set_DtrEnable(Boolean value)
   at System.IO.Ports.SerialStream..ctor(String portName, Int32 baudRate, Parity parity, Int32 dataBits, StopBits stopBits, Int32 readTimeout, Int32 writeTimeout, Handshake handshake, Boolean dtrEnable, Boolean rtsEnable, Boolean discardNull, Byte parityReplace)

What i already tried :

  • Make a sudo chmod 777 /dev/pts/2
  • Add port.DtrEnable = true; port.RtsEnable = true;
question from:https://stackoverflow.com/questions/65881584/serialport-open-gives-exception-for-set-dtrenable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, after some research, it appears i needed to use the program inside Module of tty0tt0. I went into tty0tty/module then i did make in a terminal (which worked after installing kernel header). Then, from the INSTALL instructions : sudo insmod tty0tty.ko (again, from module folder). This created /dev/tnt* (* being a number from 0 to 7) which, according to what i understood, create connection between consecutive ports (0 with 1, 2 with 3...).

When i started my test program with tnt0 and tnt1 it worked, the connection was established and i could even do some calls.

I still have errors, but as it is a test program i'm not sure if it comes from my code or from the port. Also, i have no idea how to configure those ports.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...