本文介绍了访问com端口12被拒绝错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用RFID进行库存控制项目。我已将我的RFID阅读器连接到笔记本电脑的com端口..但是当我第一次运行我的软件时,它可以正常工作,我可以正确读取标签ID。但是,如果我关闭此软件并重新执行,或者如果我从一个窗口格式切换到另一个窗口格式,那么我在屏幕上收到此错误 - 访问com端口12被拒绝。当我收到此错误时,我的c#软件没有正确读取RFID标签ID。总之第一次它工作正常,但下次我收到此错误..请帮助我。 我尝试过: private void Product_Load(object sender,EventArgs e) { 试试 { RFID =新的SerialPort(); RFID.PortName =COM12 ; RFID.BaudRate = 9600; RFID.DataBits = 8; RFID.Parity = Parity.None; RFID.StopBits = StopBits.One; RFID.Open(); RFID.ReadTimeout = 200; if(b) RFID.IsOpen) { DispString =; txtid.Text =; } 其他 { RFID.Close(); } RFID.DataReceived + = new SerialDataReceivedEventHandler(RFID_DataReceived); } catch(例外情况) { MessageBox.Show(ex.Message); } } private void RFID_DataReceived(object sender,SerialDataReceivedEventArgs e) { if(txtid.Text.Length> = 12) { RFID.Close(); } 否则 { DispString = RFID.ReadExisting(); this.Invoke(new EventHandler(DisplayText)); } } private void DisplayText(object sender,EventArgs e) { txtid.AppendText(DispString ); }I am working on inventory control project using RFID. I have connected my RFID reader to com port 12 of my laptop.. But when i run my software first time it works fine, i can read tag ID properly. But if i close this software and reexecute or if i switch from one window form to another then i am getting this error on the screen -access to com port 12 is denied. And when i get this error, my c# software is not reading RFID tag ID properly.. In short 1st time it works fine but next time i am getting this error..please help me.What I have tried:private void Product_Load(object sender, EventArgs e) { try { RFID = new SerialPort(); RFID.PortName = "COM12"; RFID.BaudRate = 9600; RFID.DataBits = 8; RFID.Parity = Parity.None; RFID.StopBits = StopBits.One; RFID.Open(); RFID.ReadTimeout = 200; if (RFID.IsOpen) { DispString = ""; txtid.Text = ""; } else { RFID.Close(); } RFID.DataReceived += new SerialDataReceivedEventHandler(RFID_DataReceived); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void RFID_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (txtid.Text.Length >= 12) { RFID.Close(); } else { DispString = RFID.ReadExisting(); this.Invoke(new EventHandler(DisplayText)); } } private void DisplayText(object sender, EventArgs e) { txtid.AppendText(DispString); }推荐答案我们没有足够的信息来说明你的应用程序是如何工作的就是这样 - 这样做吧我将被修复,但只是基于那些极少的代码示例和模糊的错误描述,当你完成它们时,你可能没有正确关闭和处理你的串口。如果你不这样做,他们会挂起,直到整个应用程序关闭并从内存中卸载,或者垃圾收集器绕过它们来删除它们。 和你的Load中的第一个真实行处理程序意味着这你要做的第一件事是创建一个新实例 - 那么旧实例会发生什么?如果它是打开的,那么它只是等待GC处理它可能永远不会发生。 首先检查你的Load方法是否存在现有实例:如果有,请关闭它,删除DataRecieved处理程序,并在创建新实例之前对其进行Dispose。在类的任何Close事件中执行相同的操作(可能这是一个表单,因此它将是您想要的FormClosing事件)。看看是否有所不同。We don't have enough information about how your applications works to say "it's this - do that and it'll be fixed", but just based on that little sample of code and a vague error description it's likely that you aren't properly closing and disposing of your serial ports when you are finished with them. If you don't then they "hang about" until your entire app is closed and unloaded from memory, or the Garbage Collector gets around to deleting them.And the first "real" line in your Load handler implies this. The first thing you do is create a new instance - so what happens to the old one? If it was open, then it just waits for the GC to dispose of it which may never happen.Start by checking in your Load method if there is an existing instance: if so, close it, remove your DataRecieved handler, and Dispose it before you create a new instance. Do the same in any Close events for the class (presumably this is a form, so it'll be the FormClosing event you want). See if that makes any difference.引用:总之第一次它工作正常,但下次我收到此错误。In short 1st time it works fine but next time i am getting this error.这表示端口打开一次后没有正确关闭。串口是独家资源;它们只能在一个应用程序中使用。 这也是由您的代码片段表示的,这些代码片段在出错时关闭端口,但无法成功。根据您的要求,您应该将 SerialPort 实例作为应用程序或特定对话窗口的成员。然后,您可以在应用程序或对话框的生命周期内访问它并相应地关闭它。This is an indication that the port is not closed properly after it has been opened once. Serial ports are exclusive resources; they can be only used by one application at time.This is also indicated by your code snippets which close the port upon errors but nowhere upon success. Depending on your requirements, you should make the SerialPort instance a member of your application or of a specific dialog window. Then you can access it during the lifetime of the application or dialog and close it accordingly. 这篇关于访问com端口12被拒绝错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 08:03