Access to COM or Serial port denied in c# - c#

I am doing a project of Modbus SCADA using easymodbus library in c#.
In application, sometimes communication gets disconnected and then when I retry to do modbus connection I get error Access to port 'COM3' denied or else Access to serial port denied.
Here is Modbus connection code
ModbusClient modbusClient = new ModbusClient("COM3");
modbusClient.Baudrate = 115200;
modbusClient.Parity = System.IO.Ports.Parity.None;
modbusClient.StopBits = System.IO.Ports.StopBits.One;
modbusClient.ConnectionTimeout = 1000;
if (!modbusClient.Connected)
{
modbusClient.Connect();
}

Related

Open Communication With Serial Port throws ArgumentException

I am trying to open communication with Com port 3, where is my printer connected. I would like to send commands from C# application directly to printer(emmulation commands) in order to check printer status. Printer is connected to my machine via Usb-To-Serial cable on COM3.
But when I try to open port I get ArgumentException - The port name does not begin with "COM". - or -The file type of the port is not supported.
When I go to device manager I can see printer driver and Usb driver set to same COM3, baud rate = 115200, Parity = none, DataBits = 8, StopBits = 1
Here is my code:
var printerPort = new SerialPort("COM3", 115200, Parity.None, 8, StopBits.One);
if(printerPort.IsOpen)
{
printerPort.Close();
}
printerPort.ReadTimeout = 1400;
printerPort.WriteTimeout = 1000;
printerPort.Open();
I am not sure what I am doing wrong. Thanks in advance!

Unity fails to receive data from another app on certain port

I have a C# game in unity which has to read from certain port:
Public const string ip = "127.0.0.1";
Public const int port = 5000;
IPEndPoint ipep = new IPEndPoint(IPAdress.parse(ip),port);
UdpClient uc = new UdpClient(ipep);
byte[] res = uc.Recieve(ref ipep);
However I get an error:
A connection attempt failed because the connected party did not respond
while data is being sent on that port from another running app all the time.

Can't Connect Multiple TCP POP3 clients with server

I keep getting this error while connecting 2nd TCP POP3 client with the server.
1st client connects without any error.I am connecting multiple clients with the help of List<>.
ERROR: System.IO.IOException: Unable to write data to the transport connection: An established connection was aborted by the software in your host machine. ---> System.Net.Sockets.SocketException: An established connection was aborted by the software in your host machine.
Please help!
TcpPop3Client side code:
TcpClient _pop3Client1 = new TcpClient("127.0.0.1",110);
_pop3Client.Add(_pop3Client1);
_pop3Ns.Add(new NetworkStream());
_pop3Sw.Add(new StreamWriter());
_pop3Sr.Add(new StreamReader());
_pop3Ns[i] = _pop3Client.GetStream();
_pop3Sw[i] = new StreamWriter(_pop3Ns[i]);
_pop3Sr[i] = new StreamReader(_pop3Ns[i]);
string lines = pop3Response(_pop3Client, i) + "\n\n";
serverResponse code:
int ret = 0;
byte[] data = new byte[_pop3Client.ReceiveBufferSize];
ret = _pop3Ns[i].Read(data, 0, data.Length);
MessageBox.Show(Encoding.ASCII.GetString(data).ToString());

C# Listenin 5060 Port [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Listening to Port 5060
I am developing a SIP client.And I have a question.
I want listen 5060 port for catch the SIP Server Message.For this,I coding something.(Also I take admin rights in program)
But I get SocketException: "An attempt was made to access a socket in a way forbidden by its access permissions" (Native error code: 10013)...
My Code:
private void ListenPort() {
WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);
TcpListener server = null;
Int32 port = 5060;
IPAddress localAddr = IPAddress.Parse("192.168.1.33");
server = new TcpListener(localAddr, port);
Byte[] bytes = new Byte[1000];
String data = null;
while (hasAdministrativeRight == true)
{
server.Start();
int i = 0;
while (1==1)
{
TcpClient client = server.AcceptTcpClient();
NetworkStream stream = client.GetStream();
data = null;
i = stream.Read(bytes, 0, bytes.Length);
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
label3.Text += data;
this.Refresh();
Thread.Sleep(500);
}
}
}
Where do you think the problem?
Have you checked that no other program is already using port 5060? That's what this error can mean.
See this discussion on the matter: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/d23d471c-002a-4958-829e-eb221c8a4b76/
You need to call server.Start() outside the while loop and before the first AcceptTcpClient call.
Also try using IPAddress.Any instead of IPAddress.Parse("192.168.1.33") for your listener ip
Make sure any other SIP Server program not installed as a Windows service which has using according port.
Type in netstat -an and it will show you the “listening” ports or try to googling port check softwares.
And check your SIP Server configuration for is it running over TCP or UDP.

Trouble Accessing com port in c#

I'm trying to read and write to a usb modem that is using the com port 3 with this code.
SerialPort sp = new SerialPort();
sp.PortName = "COM3";
//sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
sp.Open();
sp.Write("AT<CR>");
byte[] bytes = new byte[sp.BytesToRead];
sp.Read(bytes, 0, sp.BytesToRead);
textBox1.Text = Encoding.UTF8.GetString(bytes);
But I get this error :
Access to the port 'COM3' is denied.
Someone have an idea ...
Thanks
You can only open the port once. Maybe you're accidentally opening it more than once within your code or another program is using it?

Categories

Resources