Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Hardware & Devices
  4. problem of reading from serial port

problem of reading from serial port

Scheduled Pinned Locked Moved Hardware & Devices
questioncsharpsharepointhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    supercsharp1
    wrote on last edited by
    #1

    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); } } }

    S L 2 Replies Last reply
    0
    • S supercsharp1

      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); } } }

      S Offline
      S Offline
      Steve S
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • S Steve S

        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

        S Offline
        S Offline
        supercsharp1
        wrote on last edited by
        #3

        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!

        S 1 Reply Last reply
        0
        • S supercsharp1

          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!

          S Offline
          S Offline
          Steve S
          wrote on last edited by
          #4

          Hmm. My modem programming is rusty. I think it may be AT&E0 that you need to turn off echo. Have you got a reference manual for the modem? Failing that, you could try google...

          Steve S Developer for hire

          1 Reply Last reply
          0
          • S supercsharp1

            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); } } }

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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] }


            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups