我使用mono和C#打开计算机上的ttys2端口。我花了很多时间试图弄清楚为什么我无法打开我的串口。我终于包含了一个试图捕获来帮助解决它。我是C#的新手。所以任何指导都会有所帮助。
Button was just pushed .... Opening Port....
Error in Opening dev/ttys2
System.IO.IOException: No such file or directory
at System.IO.Ports.SerialPortStream.ThrowIOException () [0x00012] in
<f8255d9ef0594d18ae2c0d97286b9a80>:0
at System.IO.Ports.SerialPortStream..ctor (System.String portName,
System.Int32 baudRate, System.Int32 dataBits, System.IO.Ports.Parity
parity, System.IO.Ports.StopBits stopBits, System.Boolean dtrEnable,
System.Boolean rtsEnable, System.IO.Ports.Handshake handshake,
System.Int32 readTimeout, System.Int32 writeTimeout, System.Int32 readBufferSize, System.Int32 writeBufferSize) [0x0001e] in <f8255d9ef0594d18ae2c0d97286b9a80>:0
at (wrapper remoting-invoke-with-check)
System.IO.Ports.SerialPortStream:.ctor (string,int,int,System.IO.Ports.Parity,System.IO.Ports.StopBits,bool,bool,System.IO.Ports.Handshake,int,int,int,int)
at System.IO.Ports.SerialPort.Open () [0x00078] in
<f8255d9ef0594d18ae2c0d97286b9a80>:0
at (wrapper remoting-invoke-with-check)
System.IO.Ports.SerialPort:Open ()
at MonoSerial.Program.Main () [0x00089] in
<805ba7595bb8403d9fb17ebbff143c7f>:0
这是我写的代码:
Console.WriteLine("Button was just pushed .... Opening Port....");
SerialPort Serial_tty = new SerialPort();
string ttyname;
ttyname = "dev/ttys2";
Serial_tty.PortName = ttyname; // assign the port name
Serial_tty.BaudRate = 9600; // Baudrate = 9600bps
Serial_tty.Parity = Parity.None; // Parity bits = none
Serial_tty.DataBits = 8; // No of Data bits = 8
Serial_tty.StopBits = StopBits.One; // No of Stop bits = 1
Serial_tty.Handshake = Handshake.None;
try
{
Serial_tty.Open(); // Open the port
Serial_tty.Write("A"); // Write an ascii "A"
Serial_tty.Close(); // Close port
Console.WriteLine("\nA written to port {0}", Serial_tty.PortName);
}
catch(Exception e)
{
Console.WriteLine("Error in Opening {0}",Serial_tty.PortName);
Console.WriteLine(e);
}
答案 0 :(得分:0)
ttyname = "dev/ttys2"
我很确定你需要在该文件名前面有一个前导/
,否则它会尝试相对于当前目录打开文件,例如home/pax/dev/ttys2
。