我该如何解决这个问题。 “访问端口'COM1' [英] How Can I solve that problem. "Access to the port 'COM1'
本文介绍了我该如何解决这个问题。 “访问端口'COM1'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好先生,
你能帮助我吗?现在,我正在使用C#编写Truck Scale程序,我可以接受来自串口(例如COM1)的重量数据的数据,我可以很好地工作但是当我关闭我的C#表格时,我关闭了我的串口。但是当我重新打开该表单时,我发现错误异常拒绝访问端口'COM1'。请问如何解决这个问题。
DataTable dt = new DataTable();
dt = DataAccess.Class.SerialPort.GetDataBasePortSetting();
myport = new SerialPort();
myport.DataReceived + = MyPortDataReceived;
myport.PortName = dt.Rows [ 0 ] [ PORTNAME跨度>]的ToString();
myport.BaudRate = Convert.ToInt32(dt.Rows [ 0 ] [ 波特率跨度>]的ToString());
myport.DataBits = Convert.ToInt32(dt.Rows [ 0 ] [ 数据位跨度>]的ToString());
myport.Parity =(System.IO.Ports.Parity)Enum.Parse( typeof (System.IO.Ports.Parity),(dt.Rows) [ 0 ] [ 奇偶校验]的ToString()));
myport.StopBits =(System.IO.Ports.StopBits)Enum.Parse( typeof (System.IO.Ports.StopBits),(dt.Rows) [ 0 ] [ StopBit]的ToString()));
myport.NewLine = \\\\ n;
myport.Handshake = Handshake.None;
myport.ReadTimeout = 100 ;
myport.RtsEnable = true ;
myport.DtrEnable = true ;
// myport.ReceivedBytesThreshold = 100000;
if (myport.IsOpen)
{
myport.Close();
}
如果(!myport.IsOpen) // 错误在这里当第二次重新打开该表格时访问端口'COM1'。
{
myport.Open();
myport.DiscardInBuffer();
System.Threading.Thread.Sleep( 500 ); // 在阅读之前总是睡觉。这是确保设备写入缓冲区的一个好方法。
myport.DiscardInBuffer(); // 丢弃过时的数据。我已经使用此代码在txtGross中保持稳定的数据。
}
谢谢
Thiha Swe
缅甸
解决方案
AsSerialPort
实现IDisposable
尝试使用包装代码
确保这一点当你完成它时,资源就会被处理掉。
例如var myport = new SerialPort();
使用(myport)
{
myport.DataReceived + = MyPortDataReceived;
myport.PortName = dt.Rows [ 0 ] [ PORTNAME跨度>]的ToString();
myport.BaudRate = Convert.ToInt32(dt.Rows [ 0 ] [ 波特率跨度>]的ToString());
myport.DataBits = Convert.ToInt32(dt.Rows [ 0 ] [ 数据位跨度>]的ToString());
myport.Parity =(System.IO.Ports.Parity)Enum.Parse( typeof (System.IO.Ports.Parity),(dt.Rows) [ 0 ] [ 奇偶校验]的ToString()));
myport.StopBits =(System.IO.Ports.StopBits)Enum.Parse( typeof (System.IO.Ports.StopBits),(dt.Rows) [ 0 ] [ StopBit]的ToString()));
myport.NewLine = \\\\ n;
myport.Handshake = Handshake.None;
myport.ReadTimeout = 100 ;
myport.RtsEnable = true ;
myport.DtrEnable = true ;
// myport.ReceivedBytesThreshold = 100000;
if (!(myport.IsOpen)) // 错误在这里当第二次重新打开该表格时,访问端口'COM1'。
{
myport.Open();
myport.DiscardInBuffer();
System.Threading.Thread.Sleep( 500 ); // 在阅读之前总是睡觉。只是确保设备写入缓冲区的一个好方法。
myport.DiscardInBuffer(); // 丢弃陈旧数据。我已经使用此代码在txtGross中保持稳定数据。
}
}
Hello Sir,
Can you help me,please? Now, I am writing a Truck Scale program using C# , I can accept data from weight data from serial port(eg.COM1) , I can work well but when I close my C# form , I close my serial port. But When I reopen that form, I found error exception "Access to the port 'COM1' is denied." How can I solve that problem,please.
DataTable dt = new DataTable();
dt = DataAccess.Class.SerialPort.GetDataBasePortSetting();
myport = new SerialPort();
myport.DataReceived += MyPortDataReceived;
myport.PortName = dt.Rows[0]["PortName"].ToString();
myport.BaudRate = Convert.ToInt32(dt.Rows[0]["BaudRate"].ToString());
myport.DataBits = Convert.ToInt32(dt.Rows[0]["DataBit"].ToString());
myport.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), (dt.Rows[0]["Parity"].ToString()));
myport.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), (dt.Rows[0]["StopBit"].ToString()));
myport.NewLine = "\r\n";
myport.Handshake = Handshake.None;
myport.ReadTimeout = 100;
myport.RtsEnable = true;
myport.DtrEnable = true;
//myport.ReceivedBytesThreshold = 100000;
if (myport.IsOpen)
{
myport.Close();
}
if (!myport.IsOpen) //Error is here "Access to the port 'COM1' when reopen that form as a second time.
{
myport.Open();
myport.DiscardInBuffer();
System.Threading.Thread.Sleep(500); //Always sleep before reading. Just a good measure to ensure the device has written to the buffer.
myport.DiscardInBuffer(); //Discard stale data. I have use this code to be stable Data in txtGross.
}
Thanks
Thiha Swe
Myanmar
解决方案
AsSerialPort
implementsIDisposable
try wrapping your code inusing
to ensure that the resource is disposed of when you've finished with it.
E.g.var myport = new SerialPort(); using ( myport ) { myport.DataReceived += MyPortDataReceived; myport.PortName = dt.Rows[0]["PortName"].ToString(); myport.BaudRate = Convert.ToInt32(dt.Rows[0]["BaudRate"].ToString()); myport.DataBits = Convert.ToInt32(dt.Rows[0]["DataBit"].ToString()); myport.Parity = (System.IO.Ports.Parity)Enum.Parse(typeof(System.IO.Ports.Parity), (dt.Rows[0]["Parity"].ToString())); myport.StopBits = (System.IO.Ports.StopBits)Enum.Parse(typeof(System.IO.Ports.StopBits), (dt.Rows[0]["StopBit"].ToString())); myport.NewLine = "\r\n"; myport.Handshake = Handshake.None; myport.ReadTimeout = 100; myport.RtsEnable = true; myport.DtrEnable = true; //myport.ReceivedBytesThreshold = 100000; if (!(myport.IsOpen)) //Error is here "Access to the port 'COM1' when reopen that form as a second time. { myport.Open(); myport.DiscardInBuffer(); System.Threading.Thread.Sleep(500); //Always sleep before reading. Just a good measure to ensure the device has written to the buffer. myport.DiscardInBuffer(); //Discard stale data. I have use this code to be stable Data in txtGross. } }
这篇关于我该如何解决这个问题。 “访问端口'COM1'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文