Open Communication With Serial Port throws ArgumentException - c#

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!

Related

Access to COM or Serial port denied in 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();
}

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?

Android Receive UDP broadcast from C# desktop app over LAN?

I'm trying to create a sever application on PC for many android devices using the same wi-fi network.
The devices will find the server's IP by receiving UDP broadcast from it contains the server IP data.
I've started by creating a sample udp broadcaster in C# and udp receiver in java but I never managed to get the packet on the android side . here is the code :
C#:
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, listenPort);
listener.Connect(groupEP);
listener.EnableBroadcast = true;
byte[] data = new byte[1024];
try
{
while (!done)
{
Console.WriteLine("broadcast");
Thread.Sleep(400);
listener.Send(data,2);
}
Android code :
DatagramSocket socket;
try {
socket = new DatagramSocket(11000);
socket.connect(getBroadcastAddress(), 11000);
socket.setBroadcast(true);
byte[] buf = new byte[4];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
The Internet Permission is set correctly in the manifest. still not able to receive the packets.
Suggestions:
Make sure you don't have any firewalls (software or hardware) blocking you
Consider using Wireshark:
http://www.wireshark.org/
Look at this example:
http://code.google.com/p/boxeeremote/wiki/AndroidUDP

How can i get a file from remote machine?

How can i get a file from remote computer? i know remote computer ip and 51124 port is open. i need this algorith:(this is a Windows Application visual studio 2008)
1) Connect 192.xxx.x.xxx ip via 51124 port
2) filename:123456 (i want to search it on remote machine)
3) Get File
4) Save C:\
51124 port is open. can i access and can i search any file according to filename?
My code is below:
IPEndPoint ipEnd = new IPEndPoint(IPAddress.Any, 51124);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
sock.Bind(ipEnd);
sock.Listen(maxConnections);
Socket serverSocket = sock.Accept();
byte[] data = new byte[bufferSize];
int received = serverSocket.Receive(data);
int filenameLength = BitConverter.ToInt32(data, 0);
string filename = Encoding.ASCII.GetString(data, 4, filenameLength);
BinaryWriter bWrite = new BinaryWriter(File.Open(outPath + filename, FileMode.Create));
bWrite.Write(data, filenameLength + 4, received - filenameLength - 4);
int received2 = serverSocket.Receive(data);
while (received2 > 0) {
bWrite.Write(data, 0, received2);
received2 = serverSocket.Receive(data);
}
bWrite.Close();
serverSocket.Close();
sock.Close();
MyQuery(targetip, port, filename) i can use it like that: MyQuery(192.xxx.x.xxx,51124,"MyNeddedFile");
MyQuery(targetip, port, filename)
{
.....
...
..
.
}
You have been trying to ask this question a few times now - perhaps this explains why we cannot answer your question:
If you have an FTP server, it will (by default) listen on port 21. So if I send a message according to the FTP protocol to port 21, it will respond.
If I have apache or IIS (or some other webserver) listening on for instance port 80, and I send an FTP message to it, it will give me an error, because they are expecting HTTP requests.
Without knowing what application is listening on port 51124, we can't possibly tell you how to talk to it.

Categories

Resources