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. Help with serial data display

Help with serial data display

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
25 Posts 10 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.
  • T turbosupramk3

    I am trying to read serial data from a microchip. Using the simple serial c# project I am able to read the characters being sent, but I am not able to delineate carriage returns and so the correct data will stream across the screen until the programs input box end and then it will wrap around to the next line and continue to stream, an example would be

    seveneightnineteneleventwelvethirteen
    fourteenfifteensixteenseventeeneighteen

    instead of

    seven
    eight
    nine
    ten
    eleven
    twelve
    thirteen
    fourteen
    fifteen
    sixteen
    seventeen
    eighteen

    Does anyone know how I can recognize the carriage returns? They should come through as an ascii character "13" and the serial data protocol is 8 databits no paraty one stopbit (8N1) at a baudrate between 9600 and 115200 baud. Thanks!

    D Offline
    D Offline
    DarthDana
    wrote on last edited by
    #21

    Try looking for a linefeed (10) character instead. If it's coming from a Unix/Linux box then that's what will be sent.

    1 Reply Last reply
    0
    • A Adam Yonce

      I have had luck writing serial data from a PIC microcontroller to a textbox using the following code in C#:

      private void serialPort1_DataReceived_1(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
      {
      txtToDisplay = serialPort1.ReadExisting();
      DisplayText();
      }

      public void DisplayText()
      {
      if (txtIn.InvokeRequired)
      {
      this.BeginInvoke(new MethodInvoker(DisplayText));
      }
      else
      {
      txtIn.AppendText(txtToDisplay);
      }
      }

      Also, I set the comm port parameters in the same place I open the port, rather than the receive event:

      private void GetComPorts() //populate the comm port list with the available system ports
      {
      foreach (string s in SerialPort.GetPortNames() )
      {
      lbPort.Items.Add(s);
      }
      }

      private void btnOpen_Click(object sender, EventArgs e)
      {

       this.lbPort.SelectedIndex = this.lbPort.TopIndex;          // which port?
       this.lbRate.SelectedIndex = this.lbRate.TopIndex;          // baud rate?
       this.lbProtocol.SelectedIndex = this.lbProtocol.TopIndex;  // N,8,1 or N,7,1 (strings in a collection)
      
       string crlf = Environment.NewLine;                         // this might be the real trick...
      
       serialPort1.BaudRate = Int32.Parse(lbRate.Text);
       serialPort1.PortName = lbPort.Text;
      
       serialPort1.Open();
      
       if (serialPort1.IsOpen)
       {
          btnOpen.Enabled = false;
          btnClose.Enabled = true;
      
          txtIn.AppendText(string.Format("Port {0} opened successfully." + crlf, serialPort1.PortName));
                  
       }
      

      }

      Anyway, this seems to work in my situation. Also, my PIC code is using "\r\n" so I am actually sending a {10} and a {13} pair. Hope this helps, Adam

      T Offline
      T Offline
      turbosupramk3
      wrote on last edited by
      #22

      Hey Adam, Looks like it was searching for a 10 and 13, but it wanted them as a 13, then a 10. Thanks to everyone who was kind enough to respond and offer help to me. I'll probably have more questions as time goes on and now that I have a baseline I can start to implement the other good pieces of advice in this thread :)

      1 Reply Last reply
      0
      • T turbosupramk3

        Hey Luc, I tried \r and \n too :) with no luck then I was trying the string value and I tried Chr(13) as well with no luck. I will try the binary thing tonight and see what bits I am getting

        N Offline
        N Offline
        NedPat
        wrote on last edited by
        #23

        Has the textbox a multiline option?

        S 1 Reply Last reply
        0
        • T turbosupramk3

          Hi Colin, Maybe I can send it different characters for a line feed ... do you happen to know what pair of characters is it expecting? With the byte array, are you just filling it up with 8 bits, and then at the stop bit converting it to a string?

          C Offline
          C Offline
          ColinBurnell
          wrote on last edited by
          #24

          I expect it wants a carriage return followed by a line feed - 0x0d 0x0a Reading a byte array is not that low a level; the SerialPort class gives you a method to read byte data - SerialPort.Read Method (Byte[], Int32, Int32). You can use the BytesToRead property to see how many bytes there are in the buffer.

          1 Reply Last reply
          0
          • N NedPat

            Has the textbox a multiline option?

            S Offline
            S Offline
            ShafiqA
            wrote on last edited by
            #25

            No. I am only concatenating two string like... string strValue="abc" + ";" + "xyz";

            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