problem of reading from serial port
-
in my C# program(I mainliy use SerialPort class),I input "at" in a textBox,then the program send "at" to serial port(the other end of serial port is a modem which is connected to my computer by a data line),but when i click receive button,the message I receive from serial port is the same as what I input,namely "at",not "ok",why?who can answer my question?How I solve this problem and get the right response "ok" from serial port? next is my code: public partial class Form1 : Form { private SerialPort sp = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { sp = new SerialPort("COM1"); sp.ReadTimeout = 5000; sp.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { try { sp.Write(textBox1.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button3_Click(object sender, EventArgs e) { try { /*string text=null; char[] buffer = new char[10]; sp.Read(buffer,0,6); for (int i = 0; i < buffer.Length; i++) { text += buffer[i]; } textBox2.Text = text;*/ /*string text = null; char t = (char)sp.ReadChar(); text+=t.ToString(); while (true) { t = (char)sp.ReadChar(); if (t == '\0') break; else text += t.ToString(); } textBox2.Text = text;*/ string text = null; byte[] t = new byte[20]; char[] c = new char[20]; sp.Read(t, 0, t.Length); sp.Read(t, 0, t.Length); c = Encoding.ASCII.GetChars(t); int i; for (i = 0; i < c.Length; i++) { if (c[i] == 'O' || c[i] == 'K') text += c[i].ToString(); } textBox2.Text = text; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
-
in my C# program(I mainliy use SerialPort class),I input "at" in a textBox,then the program send "at" to serial port(the other end of serial port is a modem which is connected to my computer by a data line),but when i click receive button,the message I receive from serial port is the same as what I input,namely "at",not "ok",why?who can answer my question?How I solve this problem and get the right response "ok" from serial port? next is my code: public partial class Form1 : Form { private SerialPort sp = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { sp = new SerialPort("COM1"); sp.ReadTimeout = 5000; sp.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { try { sp.Write(textBox1.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button3_Click(object sender, EventArgs e) { try { /*string text=null; char[] buffer = new char[10]; sp.Read(buffer,0,6); for (int i = 0; i < buffer.Length; i++) { text += buffer[i]; } textBox2.Text = text;*/ /*string text = null; char t = (char)sp.ReadChar(); text+=t.ToString(); while (true) { t = (char)sp.ReadChar(); if (t == '\0') break; else text += t.ToString(); } textBox2.Text = text;*/ string text = null; byte[] t = new byte[20]; char[] c = new char[20]; sp.Read(t, 0, t.Length); sp.Read(t, 0, t.Length); c = Encoding.ASCII.GetChars(t); int i; for (i = 0; i < c.Length; i++) { if (c[i] == 'O' || c[i] == 'K') text += c[i].ToString(); } textBox2.Text = text; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
supercsharp1 wrote:
but when i click receive button,the message I receive from serial port is the same as what I input,namely "at",not "ok",why?
Your modem probably has 'echo' switched on. Hence you send "AT\r" and get back "AT\r\nOK\r\n" To forestall your next question, if your modem uses the standard AT command set, you can probably send "ATE0\r" instead of a plain "AT". That will turn echo off, although of course, your initial command will be echoed back to you, while subsequent ones won't.
Steve S Developer for hire
-
supercsharp1 wrote:
but when i click receive button,the message I receive from serial port is the same as what I input,namely "at",not "ok",why?
Your modem probably has 'echo' switched on. Hence you send "AT\r" and get back "AT\r\nOK\r\n" To forestall your next question, if your modem uses the standard AT command set, you can probably send "ATE0\r" instead of a plain "AT". That will turn echo off, although of course, your initial command will be echoed back to you, while subsequent ones won't.
Steve S Developer for hire
Thanks for answer my question!But the problem is not what you said,I send "at e0\r",and then I send "at\r",but the message I receive from the SerialPort is "at e0\rat\r",that's very curious!Waiting for you reply!Thanks you very much!
-
Thanks for answer my question!But the problem is not what you said,I send "at e0\r",and then I send "at\r",but the message I receive from the SerialPort is "at e0\rat\r",that's very curious!Waiting for you reply!Thanks you very much!
-
in my C# program(I mainliy use SerialPort class),I input "at" in a textBox,then the program send "at" to serial port(the other end of serial port is a modem which is connected to my computer by a data line),but when i click receive button,the message I receive from serial port is the same as what I input,namely "at",not "ok",why?who can answer my question?How I solve this problem and get the right response "ok" from serial port? next is my code: public partial class Form1 : Form { private SerialPort sp = null; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { sp = new SerialPort("COM1"); sp.ReadTimeout = 5000; sp.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button2_Click(object sender, EventArgs e) { try { sp.Write(textBox1.Text); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void button3_Click(object sender, EventArgs e) { try { /*string text=null; char[] buffer = new char[10]; sp.Read(buffer,0,6); for (int i = 0; i < buffer.Length; i++) { text += buffer[i]; } textBox2.Text = text;*/ /*string text = null; char t = (char)sp.ReadChar(); text+=t.ToString(); while (true) { t = (char)sp.ReadChar(); if (t == '\0') break; else text += t.ToString(); } textBox2.Text = text;*/ string text = null; byte[] t = new byte[20]; char[] c = new char[20]; sp.Read(t, 0, t.Length); sp.Read(t, 0, t.Length); c = Encoding.ASCII.GetChars(t); int i; for (i = 0; i < c.Length; i++) { if (c[i] == 'O' || c[i] == 'K') text += c[i].ToString(); } textBox2.Text = text; } catch (Exception ex) { MessageBox.Show(ex.Message); } } }
Hi, If you have trouble with serial port software in connection with modem, then I strongly suggest you split the problem in two parts: - test you cable and modem using an existing app, such as HyperTerminal; this will also help you in learning modem commands (use on info from Google) - develop your serial port code without a modem, use a second PC (maybe again with HyperTerminal; and with a null modem, or null modem cable) or something else Only once you got both parts sufficiently solved, try to combine your app with the modem. BTW: I advise you to log all serial data (both in and out) in a file using both hex and ASCII while debugging (and to leave the code in, but switch it off, once you think it works adequately). :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }