我想要从串口读取一些信息,但是当我尝试打开连接时,它会抛出未经授权的访问异常。
这段代码读取端口名称。
SerialPort port = new SerialPort();
string[] serialPorts = System.IO.Ports.SerialPort.GetPortNames();
public Page_Main()
{
InitializeComponent();
for (int i = 0; i < serialPorts.Count(); i++)
portBox.Items.Add(serialPorts[i]);
}
这是尝试从串口获取信息的代码。
port.PortName = portBox.SelectedItem.ToString();
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();// This is where the exception is thrown
serialOutput.Text = port.ReadLine();