How to read from Serial (COM) Port? [modified]
-
i want to read data from a serial port(where a gsm modem is connected). I want to read the reply from modem when an AT command is send to the modem. i.e for eg. if i send AT to the modem i want to read the reply OK from the modem! I had used the below codes but the application hangs while executing these lines:
string data = Convert.ToString(serialPort1.Read(buffer, 0, (int)buffer.Length)); //read using a Stream string data1 = Convert.ToString(serialPort1.BaseStream.Read(buffer, 0, (int)buffer.Length));
I had also triedstring data = serialPort1.ReadLine();
Below is my full code section:private void button1_Click(object sender, EventArgs e)
{
try
{//configuring the serial port serialPort1.PortName = "COM6"; serialPort1.Handshake = Handshake.None; serialPort1.BaudRate = 115200; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.DtrEnable = true; serialPort1.Open(); string c = "AT+CMGS="; string c1 = "\\"+6582083412\\"\\r\\n"; serialPort1.Write(c+c1); string d = "Test message from coded program "; //Now send the contents of the message serialPort1.Write(d); //SerialPortListen(); char\[\] arr = new char\[1\]; arr\[0\] = (char)26; //ascii value of Ctrl-Z serialPort1.Write(arr, 0, 1); //string data = Convert.ToString(serialPort1.Read(buffer, 0, (int)buffer.Length)); //read using a Stream //string data1 = Convert.ToString(serialPort1.BaseStream.Read(buffer, 0, (int)buffer.Length)); //MessageBox.Show(data + " part 2 is : " + data1); serialPort1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
I would appreciate your help very much, since this is my first time i am trying to communicate with a serial port. Thanks in advance
modified on Thursday, January 15, 2009 11:48 PM
-
i want to read data from a serial port(where a gsm modem is connected). I want to read the reply from modem when an AT command is send to the modem. i.e for eg. if i send AT to the modem i want to read the reply OK from the modem! I had used the below codes but the application hangs while executing these lines:
string data = Convert.ToString(serialPort1.Read(buffer, 0, (int)buffer.Length)); //read using a Stream string data1 = Convert.ToString(serialPort1.BaseStream.Read(buffer, 0, (int)buffer.Length));
I had also triedstring data = serialPort1.ReadLine();
Below is my full code section:private void button1_Click(object sender, EventArgs e)
{
try
{//configuring the serial port serialPort1.PortName = "COM6"; serialPort1.Handshake = Handshake.None; serialPort1.BaudRate = 115200; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.DtrEnable = true; serialPort1.Open(); string c = "AT+CMGS="; string c1 = "\\"+6582083412\\"\\r\\n"; serialPort1.Write(c+c1); string d = "Test message from coded program "; //Now send the contents of the message serialPort1.Write(d); //SerialPortListen(); char\[\] arr = new char\[1\]; arr\[0\] = (char)26; //ascii value of Ctrl-Z serialPort1.Write(arr, 0, 1); //string data = Convert.ToString(serialPort1.Read(buffer, 0, (int)buffer.Length)); //read using a Stream //string data1 = Convert.ToString(serialPort1.BaseStream.Read(buffer, 0, (int)buffer.Length)); //MessageBox.Show(data + " part 2 is : " + data1); serialPort1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
I would appreciate your help very much, since this is my first time i am trying to communicate with a serial port. Thanks in advance
modified on Thursday, January 15, 2009 11:48 PM
-
i want to read data from a serial port(where a gsm modem is connected). I want to read the reply from modem when an AT command is send to the modem. i.e for eg. if i send AT to the modem i want to read the reply OK from the modem! I had used the below codes but the application hangs while executing these lines:
string data = Convert.ToString(serialPort1.Read(buffer, 0, (int)buffer.Length)); //read using a Stream string data1 = Convert.ToString(serialPort1.BaseStream.Read(buffer, 0, (int)buffer.Length));
I had also triedstring data = serialPort1.ReadLine();
Below is my full code section:private void button1_Click(object sender, EventArgs e)
{
try
{//configuring the serial port serialPort1.PortName = "COM6"; serialPort1.Handshake = Handshake.None; serialPort1.BaudRate = 115200; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.DtrEnable = true; serialPort1.Open(); string c = "AT+CMGS="; string c1 = "\\"+6582083412\\"\\r\\n"; serialPort1.Write(c+c1); string d = "Test message from coded program "; //Now send the contents of the message serialPort1.Write(d); //SerialPortListen(); char\[\] arr = new char\[1\]; arr\[0\] = (char)26; //ascii value of Ctrl-Z serialPort1.Write(arr, 0, 1); //string data = Convert.ToString(serialPort1.Read(buffer, 0, (int)buffer.Length)); //read using a Stream //string data1 = Convert.ToString(serialPort1.BaseStream.Read(buffer, 0, (int)buffer.Length)); //MessageBox.Show(data + " part 2 is : " + data1); serialPort1.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
I would appreciate your help very much, since this is my first time i am trying to communicate with a serial port. Thanks in advance
modified on Thursday, January 15, 2009 11:48 PM
Hi Aghosh Babu, It seems like you are try to build an application which send SMS using a connect Phone , and the serial port you are using (COM6) is a virtual serial port (port used to connect to through buletooth or USB ) , so please make sure that you com6 is working , by using Apllication like Hyper Terminal (it comes with windows XP) , try to send AT , and recive OK , if its is working over there , than you can try it in your code (dont forget to disconnect hyper terminal bepore try from you code , other wise it will give error , say port already in use). I have used the following code recive data that arrives on serial port
private static void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (_serialPort.BytesToRead > 0)
{
int bytesToRead = _serialPort.BytesToRead;
byte[] dataRead = new byte[bytesToRead];
string linedata;
_serialPort.Read(dataRead, 0, bytesToRead);
linedata = ConvertToString(dataRead);
}
}private string ConvertToString(IEnumerable ByteArray) { //This Function converts an Byte Array to string , This we // nned to do because if we directly try to read srting , // From the com port , it take Unicode encoding , we need ASCII encoding // Hence we need to string tempStringToReturn = string.Empty; foreach (byte bt in ByteArray) { tempStringToReturn = tempStringToReturn + Convert.ToChar(bt); } return tempStringToReturn; }
In the above code SerialPortDataReceived is a event of serial port object , this event is raised when there data arrives on the serial port . Try out with above information , see if it helps , i need to move now , let me know if you find any problem regarding the same
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
-
Hi Aghosh Babu, It seems like you are try to build an application which send SMS using a connect Phone , and the serial port you are using (COM6) is a virtual serial port (port used to connect to through buletooth or USB ) , so please make sure that you com6 is working , by using Apllication like Hyper Terminal (it comes with windows XP) , try to send AT , and recive OK , if its is working over there , than you can try it in your code (dont forget to disconnect hyper terminal bepore try from you code , other wise it will give error , say port already in use). I have used the following code recive data that arrives on serial port
private static void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
{
if (_serialPort.BytesToRead > 0)
{
int bytesToRead = _serialPort.BytesToRead;
byte[] dataRead = new byte[bytesToRead];
string linedata;
_serialPort.Read(dataRead, 0, bytesToRead);
linedata = ConvertToString(dataRead);
}
}private string ConvertToString(IEnumerable ByteArray) { //This Function converts an Byte Array to string , This we // nned to do because if we directly try to read srting , // From the com port , it take Unicode encoding , we need ASCII encoding // Hence we need to string tempStringToReturn = string.Empty; foreach (byte bt in ByteArray) { tempStringToReturn = tempStringToReturn + Convert.ToChar(bt); } return tempStringToReturn; }
In the above code SerialPortDataReceived is a event of serial port object , this event is raised when there data arrives on the serial port . Try out with above information , see if it helps , i need to move now , let me know if you find any problem regarding the same
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
Hi Bharat, Thanks for the code... i am now able to read data from serial port.. initially i haven't set my handshake property properly...I used
serialPort1.ReadExisting();
to read the data from serial port... i think i will redo my code with yours :-D ...i hope your code will automatically initiate the method when a data comes into the port... :-O -
Hi Bharat, Thanks for the code... i am now able to read data from serial port.. initially i haven't set my handshake property properly...I used
serialPort1.ReadExisting();
to read the data from serial port... i think i will redo my code with yours :-D ...i hope your code will automatically initiate the method when a data comes into the port... :-OGr8 to know that the code was helpful.
-Regards Bharat Jain bharat.jain.nagpur@gmail.com