Click here to Skip to main content
15,884,933 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
Java
System.out.println("Trying to open port...");
           CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(comPort);
           port = (SerialPort)portId.open("SMS Transceiver", 10);
           port.setSerialPortParams(boardRates, dataBits, stopBits, 0);
           out = new OutputStreamWriter(port.getOutputStream(), "ISO-8859-1");
           in = new InputStreamReader(port.getInputStream(), "ISO-8859-1");
           System.out.println("Port is opened successfully..... \n");
           Thread.sleep(10000L);


What I have tried:

C#
Console.WriteLine("Trying to open port...");
Console.WriteLine(comPort);
port = new System.IO.Ports.SerialPort(comPort);
port.Open();
SerialPort Serial1 = new System.IO.Ports.SerialPort(comPort, boardRates, Parity.None, dataBits);
port.Encoding = Encoding.GetEncoding("iso-8859-1");
Console.WriteLine("Port is opened successfully..... \n");
Thread.Sleep(10000);
Posted
Updated 25-May-16 5:24am
v3
Comments
Richard MacCutchan 25-May-16 6:38am    
Please show the code you have tried, and explain what the error is, and where it occurs.
Member 12363094 25-May-16 6:42am    
Console.WriteLine("Trying to open port...");
Console.WriteLine(comPort);
port = new System.IO.Ports.SerialPort(comPort);
port.Open();
SerialPort Serial1 = new System.IO.Ports.SerialPort(comPort, boardRates, Parity.None, dataBits);
port.Encoding = Encoding.GetEncoding("iso-8859-1");
Console.WriteLine("Port is opened successfully..... \n");
Thread.Sleep(10000);
Member 12363094 25-May-16 6:45am    
above code i have mention streamreader sout=null& streamwriter sin=null;
wen i was sending AT commands(modem) to port then i have to use like
sout.write(AT).
in this case i am getting null reference exception
Richard MacCutchan 25-May-16 7:06am    
If sout is null then sout.write cannot work; basic OOP.
Member 12363094 25-May-16 7:15am    
then how to convert
out = new OutputStreamWriter(port.getOutputStream(), "ISO-8859-1");to dotnet

1 solution

It is usually not a good idea to just try to convert code from one language to another line by line.
Sometimes it works, but sometimes not.

Instead try to analyze the code you want to translate and understand what it is supposed to do and then find out how to do it in another language.

In your case it looks like you want to

1. Open a specific COM port.
2. Set communication parameters
3. Write data
4. Read data

But more than this I cannot know, because you don't give that information.
So you need to do this analyse.

For example what kind of communication do you want to do?
* Is it a master - slave setup where one side is always sending data and the other side only respond?
* Who is the master? The PC or the device?
* Will the connected device send data at any time, like a barcode reader?


What do you in general need to do in C# to communicate over a serial port?

1. Set up the communication parameters
C#
string portName = "COM1";
int baudRate = 9600;
Parity parity = Parity.None;
int dataBits = 8;

2. Create an instance of the class SerialPort
C#
SerialPort serial = new SerialPort(portName, baudRate, parity, dataBits);
serial.Encoding = Encoding.GetEncoding("iso-8859-1");

3. Open the port
C#
serial.Open();

4. Write data
C#
serial.WriteLine("Hello World");
or
serial.Write(new byte[] {0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64});

5. Read data
C#
string retVal = serial.ReadLine();  // Requires that the data is ended with a new line
or
byte[] buffer = new byte[serial.BytesToRead()];
int bytesRead = serial.Read(buffer, 0, buffer.Length);

You can also use the SerialPort.DataReceived Event (System.IO.Ports)[^] for reading bytes. This is useful for, for example, a barcode reader where data can be sent from the reader at any time.

That is the basics of serial communication.
Use MSDN and read the documentation: SerialPort Class (System.IO.Ports)[^]

As you don't explain what your goal is, it is difficult to give more help than this.
 
Share this answer
 
Comments
Matt T Heffron 25-May-16 15:41pm    
+5
George Jonsson 25-May-16 18:27pm    
Thanks Matt.
Member 12363094 25-May-16 23:42pm    
but in my solution i am getting "object reference has not set " runtime exception
when i was writing serialport.write(s);
public static Boolean atCommandCheck()
{
String s1 = " ";
try
{
Port.writeln("AT");
//Thread.Sleep(1000);
s1 = Port.read();
}
catch(Exception e)
{
Console.WriteLine((new StringBuilder()).Append("Exception in atCommandCheck(AT Command Checking)..........").Append(e.Message).ToString());
s1="OK";
}
Console.WriteLine((new StringBuilder()).Append("AT..................").Append(s1).ToString());
return s1.IndexOf("OK") >= 0;
}
above one is in sms class i am calling writeln method from another class
public static void writeln(String s)
{
Serial1.Write(s);

}
in this case i was not able to write command to serial port
George Jonsson 26-May-16 0:17am    
You can only have one instance of SerialPort for the same COM port at a time.
You seem to have two, port and serial. Why?
Member 12363094 26-May-16 0:36am    
that Port is a class not serialport
in that writeln is method.
i have only one port that is serial1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900