计算机端口com1,访问端口“COM1”被拒绝(Access to the port “COM1” is denied)

访问端口“COM1”被拒绝(Access to the port “COM1” is denied)

我有一个RFID设备通过端口COM1连接到我的笔记本电脑(确认正确,我使用设备管理器)。 但是,当我用C#语言运行这些代码。 它给我一个例外访问端口“COM1”被拒绝 。 任何人都可以帮助我解决这个问题? 以下是我的代码。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.IO.Ports;

using System.Threading;

namespace Testing1

{

public class Testing1

{

public static SerialPort iSerialPort = new SerialPort();

static int Main()

{

string strException = string.Empty;

string strComPort = "COM1";

int nBaudrate=Convert.ToInt32(9600);

int nRet = OpenCom(strComPort, nBaudrate, out strException);

if (nRet != 0)

{

string strLog = "Connect reader failed, due to: " + strException;

Console.WriteLine(strLog);

//return;

}

else

{

string strLog = "Reader connected " + strComPort + "@" + nBaudrate.ToString();

Console.WriteLine(strLog);

}

Console.WriteLine("Press any key to exit.");

Console.ReadKey();

iSerialPort.Close();

return 0;

}

public static int OpenCom(string strPort, int nBaudrate, out string strException)

{

strException = string.Empty;

if (iSerialPort.IsOpen)

{

iSerialPort.Close();

}

try

{

iSerialPort.PortName = strPort;

iSerialPort.BaudRate = nBaudrate;

iSerialPort.ReadTimeout = 200;

iSerialPort.DataBits = 8;

iSerialPort.Parity = Parity.None;

iSerialPort.StopBits = StopBits.One;

iSerialPort.Open();

}

catch (System.Exception ex)

{

strException = ex.Message;

return -1;

}

return 0;

}

}

}

I have a RFID devices connected to my laptop through Port COM1(Confirmed correct whereby i had using device manager). However, when i run these code in C# language. It give me an exception Access to the port "COM1" is denied. Anyone can help me to solve this problem? Below is my code.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Net;

using System.Net.Sockets;

using System.IO.Ports;

using System.Threading;

namespace Testing1

{

public class Testing1

{

public static SerialPort iSerialPort = new SerialPort();

static int Main()

{

string strException = string.Empty;

string strComPort = "COM1";

int nBaudrate=Convert.ToInt32(9600);

int nRet = OpenCom(strComPort, nBaudrate, out strException);

if (nRet != 0)

{

string strLog = "Connect reader failed, due to: " + strException;

Console.WriteLine(strLog);

//return;

}

else

{

string strLog = "Reader connected " + strComPort + "@" + nBaudrate.ToString();

Console.WriteLine(strLog);

}

Console.WriteLine("Press any key to exit.");

Console.ReadKey();

iSerialPort.Close();

return 0;

}

public static int OpenCom(string strPort, int nBaudrate, out string strException)

{

strException = string.Empty;

if (iSerialPort.IsOpen)

{

iSerialPort.Close();

}

try

{

iSerialPort.PortName = strPort;

iSerialPort.BaudRate = nBaudrate;

iSerialPort.ReadTimeout = 200;

iSerialPort.DataBits = 8;

iSerialPort.Parity = Parity.None;

iSerialPort.StopBits = StopBits.One;

iSerialPort.Open();

}

catch (System.Exception ex)

{

strException = ex.Message;

return -1;

}

return 0;

}

}

}

原文:https://stackoverflow.com/questions/23672344

更新时间:2019-12-22 07:47

最满意答案

当某些其他程序正在访问COM1端口时,可能会发生此异常。 您是否有使用RFID设备的其他程序?

您可以使用Portmon检查哪些程序使用哪些端口。

This exception can occur when some other program is accessing the COM1 port. Do you have any other program open that uses the RFID device?

You can check what programs use what ports using Portmon.

相关问答

您正在正确调用该函数。 除非它返回INVALID_HANDLE_VALUE,否则应该有一个可用的端口。 请注意,这可能包括USB端口! 另请注意,对于COM端口10及更高版本,您需要使用命令\\.\\COMn打开它们,该命令对应于C字符串\\\\.\\COMn (其中n是指定的1或2位数字) COM端口号)。 请参见http://support2.microsoft.com/kb/115831 。 You are calling the function correctly. Unless it

...

这不是一个直接的答案,但是如果你非常绝望,我有一个朋友使用Detours将与COM接口相关的输出重定向到他想要的地方,当他无法编辑另一个程序的源时。 This isn't a direct answer, but if you get desperate enough, I had a friend use Detours to redirect output relating to COM interfaces to where he wanted it when he couldn't edi

...

我的情况非常具体。 发生的事情是,有一个先前已连接的设备。 当它被删除时,进行连接的对象仍然保持在端口上。 现在,当插入一个新设备时,虽然搜索该设备显示它在COM1中,但是尝试打开它会导致此异常,因为之前仍然存在的对象仍然保留在端口上。 My case is a very specific one. What was happening is, there was a device that was previously connected. When it's removed, the Obje

...

当某些其他程序正在访问COM1端口时,可能会发生此异常。 您是否有使用RFID设备的其他程序? 您可以使用Portmon检查哪些程序使用哪些端口。 This exception can occur when some other program is accessing the COM1 port. Do you have any other program open that uses the RFID device? You can check what programs use what p

...

如果您使用的是ch341g,有时您的操作系统可能无法检测到串行接口。 您需要安装适当的驱动程序才能工作。 安装ch341g驱动程序后,您可以在设备管理器和Arduino IDE工具上查看它。 mac OSX的说明在这里 。 对于Windows,说明在这里 。 If you are using ch341g, sometimes your operating system may not detect the serial interface. You need to have proper dri

...

您可能需要重新启动才能清除它,尽管一种方法是使用Process Explorer并搜索\Device\Serial0的句柄。 然后,您可以查看关闭此句柄是否有效,但它可能不会。 为了防止将来发生这种情况,您需要将comPort.Close()调用放在finally块中: try

{

comPort.Open();

// ...

}

finally

{

// Almost always ensures the COM port will be cleaned up,

...

这些是保留字。 不要使用以下保留的设备名称作为文件名:CON,PRN,AUX,NUL,COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,LPT1,LPT2,LPT3,LPT4,LPT5 ,LPT6,LPT7,LPT8和LPT9。 同时避免使用这些名称,然后立即进行扩展; 例如,不建议使用NUL.txt。 有关更多信息,请参阅命名空间。 https://superuser.com/questions/467782/why-cant-i-create-a-fol

...

不知道为什么这是修复,但我在form2中引用Form1以在Form1中使用2个函数。 删除引用并将两个函数复制到表单2后,它似乎工作。 作品: namespace FanHubController

{

public partial class Form2 : Form

{

Form1 mainForm = new Form1();

public Form2(String currFan)

{

InitializeComponent();

c

...

如果您使用的是FTDI USB /串行适配器,则可以直接从托管包装器( FTDI托管驱动程序包装器 )检索状态,并根据连接状态重新初始化串行端口。 原谅我对FTDI设备缺乏经验,但这应该重置你的R-232适配器: FTD2XX_NET.FTDI device = new FTD2XX_NET.FTDI();

string port;

device.GetCOMPort(out port);

if (!string.IsNullOrEmpty(port) && (port.Equals(targe

...

首先,使用带有80绑定的WorkerRole是奇怪的,而80端口可用的WebRole打开了。 下一个。 您似乎尚未为WorkerRole定义输入端点,Azure防火墙会关闭所有端口。 因此,要打开80端口,请尝试在ServiceDefinition.csdef文件或WorkerRole属性窗口“端点”选项卡中指定输入端点。 在发布到Azure时,它应该连接此配置以打开端口。 所以简而言之,尝试使用以下方法更新ServiceDefinition.csdef :

...

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误通常发生在你使用Java发送邮件时没有正确设置JavaMail Session。你需要使用JavaMail API创建一个Session对象,该对象可以指定邮件服务器的地址、端口、用户名和密码等信息。在创建Session对象时,你需要使用Authenticator对象来设置SMTP服务器的用户名和密码,以便能够成功地连接到邮件服务器。 以下是一个示例代码,用于设置JavaMail Session,并通过SMTP服务器发送邮件: ``` Properties props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "587"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("username", "password"); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress("from@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("to@example.com")); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!"); Transport.send(message); System.out.println("Done"); } catch (MessagingException e) { throw new RuntimeException(e); } ``` 请注意,在上面的示例代码中,我们使用了Gmail的SMTP服务器,如果你使用不同的SMTP服务器,你需要相应更改邮件服务器的地址和端口号。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值