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. C#
  4. read from serialport problem

read from serialport problem

Scheduled Pinned Locked Moved C#
questioncsharpsharepointhelp
3 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 input "at" in a textBox,then the program send "at" to serialport,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); } } }

    H L 2 Replies Last reply
    0
    • S supercsharp1

      in my C# program,i input "at" in a textBox,then the program send "at" to serialport,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); } } }

      H Offline
      H Offline
      Hesham Yassin
      wrote on last edited by
      #2

      http://www.codeproject.com/useritems/TCP_IP_protocols.asp[^]

      1 Reply Last reply
      0
      • S supercsharp1

        in my C# program,i input "at" in a textBox,then the program send "at" to serialport,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
        #3

        if the end result is not what you want it is wise to look at the intermediate data, in your case log the bytes that the serial port returns to you (I would use HEX numbers). If they are exactly what you have sent, then either your target device is echoing them, or you have an unintended connection between TxD and RxD (transmit data and receive data) on your serial cable. :)

        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