SerialPort Issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: SerialPort Issue

  1. #1
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question SerialPort Issue

    Hi Friends, the following code showing the Message at Customer Display System Which is on COM1 Port with Pc.
    the problem is that each time i am running the following code . it overwrites in the same line .is there any
    way to clear the Screen the again Display the Message at Customer Display Lcd ? .and Formating the size at Customer Display System. let me know please .any help would be highly appreciated .
    Code:
     private void SerialPortTest_Load(object sender, EventArgs e){
                SerialPort port = new SerialPort();
                port.PortName = "COM1";
                port.Parity = Parity.None;
                port.DataBits = 8;
                port.StopBits = StopBits.One;
                if (port.IsOpen ){
                    port.Close();                
              } 
               
                port.Open();
                port.ReadExisting();
                port.WriteLine("WELCOME TO ALHOKAIR IT ");
                port.Close();
                port.Dispose();
            }
        }
    Last edited by firoz.raj; December 26th, 2012 at 01:33 PM.

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: SerialPort Issue

    Your use of the serial port seems OK in this case. What type of data does the hardware device expect (probably will need to look through the data sheet)? Use of hyper terminal (or Putty??) might help in debugging or understanding the protocol.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Posts
    22

    Re: SerialPort Issue

    Check that there is a CR at the end of the data coming in, if there isn't you will need to set one up, or add one to your code.

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: SerialPort Issue

    You can avoid the new-line encoding issue by using port.Write(...) and instead using \r and \n for CR and NL characters respectively. If your device is unix-like, the newline would be like:

    Code:
    port.Write("WELCOME TO ALHOKAIR IT \n");
    Or if Windows-like:
    Code:
    port.Write("WELCOME TO ALHOKAIR IT \r\n");
    Or the newline character might be totally ignored (for example, your device might simply expect a string of length N where N is the number of display elements in your sign).
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: SerialPort Issue

    Hi Friends, problem is that . i simple want . it needs to print the Text in one line .why it is breaking in Second Line ?.
    let me know please . any help would be highly appreciated .

  6. #6
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question Re: SerialPort Issue

    Hi Friends, problem is that . i simple want . it needs to print the Text in one line .why it is breaking in Second Line ?.let me know please . any help would be highly appreciated .
    Name:  CustomerDisplaySystem.jpg
Views: 61
Size:  17.5 KB

  7. #7
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: SerialPort Issue

    I think there is insufficient information to answer your question.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Dec 2012
    Posts
    22

    Re: SerialPort Issue

    You are sending 23 characters of data to a 2 line, 20 characters display. I don't understand what else you expect it do?

  9. #9
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Re: SerialPort Issue

    i want to display 23 character in one line .i don't want to display the following text in two line . let me know please .
    Code:
    "WELCOME TO SILICONSOFT IT"
    Last edited by firoz.raj; January 5th, 2013 at 12:44 PM.

  10. #10
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,006

    Re: SerialPort Issue

    Since it's (obviously) impossible to display 23 characters simultaneously on a single line of the display, I guess you could scroll the message then. Something along the lines of:

    Code:
    string message = "WELCOME TO ALHOKAIR IT     "; //Padded with some spaces at the end for better readability
    const int SINGLE_LINE_WIDTH = 20;
    
    // Probably want to be running this whole loop in a seperate thread to avoid halting the main thread;
    int offset = 0;
    while( true )
    {
        //Calculate a wrapped version of the string
        string curString = "";
        int wrapIndex = 0;
        while( curString.Length < 20 )
        {
            curString = curString + curString.Substring(wrapIndex, Math.Min(SINGLE_LINE_WIDTH, curString.Length - wrapIndex));
            wrapIndex = 0;
        }
    
        //Pad the string out to 40 characters (seems prudent to always fully-specify the display out)
        curString = curString.PadRight(40, ' ');
    
        port.Write(curString);
    
        
        Thread.Sleep(500); //Wait 500 milliseconds
        
        //Update the offset
        offset = (offset + 1) % message.Length;
    }
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #11
    Join Date
    Dec 2008
    Location
    Step Into(F11)
    Posts
    465

    Question Re: SerialPort Issue

    Still it is showing the same Result . let me know some more idea . it needs to show in one line .any help would be highly appreciated .
    Code:
    private void SerialPortTest_Load(object sender, EventArgs e){
                
                port.PortName = "COM1";
                port.Parity = Parity.None;
                port.DataBits = 8;
                port.StopBits = StopBits.One;
                if (port.IsOpen ){
                    port.Close();                 
              } 
               
                port.Open();             
                //port.WriteLine("WELCOME TO ALHOKAIR IT ");           
             //   port.Write(DisplayMessage());
                DisplayMessage();
                port.Close();
                port.Dispose();
            }
            public string DisplayMessage()  {
                string message = "WELCOME TO ALHOKAIR IT     "; //Padded with some spaces at the end for better readability
                const int SINGLE_LINE_WIDTH = 20;
                // Probably want to be running this whole loop in a seperate thread to avoid halting the main thread;
                int offset = 0;
                while (true)
                {
                    //Calculate a wrapped version of the string
                    string curString = "Welcome to Alhokair IT";
                    int wrapIndex = 0;
                    while (curString.Length < 20)
                    {
                        curString = curString + curString.Substring(wrapIndex, Math.Min(SINGLE_LINE_WIDTH, curString.Length - wrapIndex));
                        wrapIndex = 0;
                    }
                    //Pad the string out to 40 characters (seems prudent to always fully-specify the display out)
                   curString = curString.PadRight(40, ' ');           
                    port.Write(curString);
    
                    Thread.Sleep(500); //Wait 500 milliseconds
                    //Update the offset
                    offset = (offset + 1) % message.Length;
                }
     
    
            }

  12. #12
    Join Date
    Jan 2013
    Posts
    3

    Re: SerialPort Issue

    If you want all your text to be in one line, try using .Write method istead of .WriteLine


    He doesn't need a code to scroll the message, the display already has a built in function for that. He just needs to turn on this option using his microcontroller.
    Last edited by blacblu; February 4th, 2013 at 01:01 PM.

  13. #13
    Join Date
    Dec 2012
    Posts
    22

    Re: SerialPort Issue

    Write or WriteLine will make no difference.

    This is a piece of hardware that has a FIXED display of 2 lines of 20 characters. Like a calculator, you cannot make the characters bigger or smaller, or place 23 characters in 20 spaces. Please trust me, I have worked with POS for over 20 years.

    The idea of scrolling the text, either all the way through the line, or back and forth is actually a very elegant solution, however this does mean that any other working code will have to branch from this loop. The only other option is to LF and show part of the data on the 2nd line, but I guess this is not desirable either.

    If none of the above is acceptable then decide to change what you want to display, or buy different hardware.


  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,359

    Re: SerialPort Issue

    You are trying to cram 2 pounds of beans into a 1 pound bag.

    Obviously if the hardware display is only xx characters long and you are trying to set xx + nn characters, it isn't going to fit on the same line.

    You're kind of lucky that your display wraps at all. Many displays would either truncate or throw an error.

    Here are your options:
    1) Reduce the length of the string.
    2) Let it wrap
    3) Write it in multiple lines

    Good luck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center