Read/ write string to com port

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravimishra11
    New Member
    • Oct 2008
    • 7

    Read/ write string to com port

    Hi friends,

    I have written code in asp.net to read or write string in com port(COM1).But when i run the program i am not getting any output .Below is my code .How can i check the string which i have send in com port is available in com port for read.

    private SerialPort port = new SerialPort("COM 1", 9600, Parity.None, 8, StopBits.One);
    port.Open();



    // port.Write("He" );

    port.Close();
    port.Open();
    port.ReadTo("e" );
    Console.WriteLi ne(port.ReadLin e());


    please reply as soon as possible....... ............... ...Thanx in advance for reply...
  • joedeene
    Contributor
    • Jul 2008
    • 579

    #2
    Is it possible to have a serialdatarecei ved args, with the handles in asp.net? If so, try that, maybe its not receiving it yet. You can always look up examples on google.


    ex of the data received handler:

    Code:
    Private Sub port_DataReceived(ByVal sender As Object, ByVal e As _
           System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived
          
           End If
        End Sub
    joedeene

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      How do you know it is not working?
      You know that code will be run on the server's COM port and not on the computer viewing the webpage right?

      Comment

      • ravimishra11
        New Member
        • Oct 2008
        • 7

        #4
        because after port.write statement i have written

        Console.WriteLi ne(port.ReadExi sting()); and in my console window m nog getting any out put;

        2)even if i used
        port.DataReceiv ed += new SerialDataRecei vedEventHandler (port_DataRecei ved);


        private void port_DataReceiv ed(object sender, SerialDataRecei vedEventArgs e)
        {
        // Show all the incoming data in the port's buffer
        Console.WriteLi ne(port.ReadExi sting());
        }
        this handle does not fire;


        Thanx for reply .if you can provide more solution i am waiting to ur suggestion.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          In your example code you call a Close() and then an Open(), why? The data could come in durring that time and you would never see it?
          Are you making sure there is no flowcontrol enabled for the port (or enabled if you need to use it)

          Comment

          • ravimishra11
            New Member
            • Oct 2008
            • 7

            #6
            Because first open port and i am writing the string to send to port like
            port.write("str ing")

            after writing string i am closing port as suggested somewhere in google.

            then i am opening the port and reading the data.

            actually i followed this link here is code for that:

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Well that is a bad practice, and nowhere in the article's sample code did I see where it said to close and then open.
              You should close the object when you are done with it, if it is not open, how do you expect to receive data?

              Comment

              • ravimishra11
                New Member
                • Oct 2008
                • 7

                #8
                Can u give me any solution???whic h is good

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  Well for testing purposes, try this:
                  [code=c#]
                  private SerialPort port = new SerialPort("COM 1", 9600, Parity.None, 8, StopBits.One);

                  port.Open();
                  port.Write("He" );
                  string firstread=port. ReadTo("e");
                  System.Threadin g.Thread.Sleep( 100);//pause for 1/10 a second
                  Console.WriteLi ne(port.ReadLin e());
                  port.Close();
                  [/code]

                  Comment

                  • ravimishra11
                    New Member
                    • Oct 2008
                    • 7

                    #10
                    Hi,
                    I did same as u send but still in console window i am not able to see any thing.
                    is there any other thing,that i need to set in computer

                    Comment

                    • Plater
                      Recognized Expert Expert
                      • Apr 2007
                      • 7872

                      #11
                      What is attached on the COM port of the server computer? Are you using the corrrect baud rate? Are you using the correct COM port?

                      I am confused, your original post says you are doing this in ASP.NET, but you say you are in a console window?

                      Comment

                      • ravimishra11
                        New Member
                        • Oct 2008
                        • 7

                        #12
                        Originally posted by Plater
                        What is attached on the COM port of the server computer? Are you using the corrrect baud rate? Are you using the correct COM port?

                        I am confused, your original post says you are doing this in ASP.NET, but you say you are in a console window?


                        yes i am doing in console application .

                        Comment

                        • ravimishra11
                          New Member
                          • Oct 2008
                          • 7

                          #13
                          It is in Asp.net Console application.

                          Comment

                          • joedeene
                            Contributor
                            • Jul 2008
                            • 579

                            #14
                            Originally posted by ravimishra11
                            It is in Asp.net Console application.
                            An Asp.Net Console Application?! I've never heard of such, even googled it and nothing comes up relevant, how did you come about creating such, like what is some code from your asp.net console app? because it could just be a console app. Can you see it in your browser? Or just in a black standard console window...?

                            joedeene

                            Comment

                            • Curtis Rutland
                              Recognized Expert Specialist
                              • Apr 2008
                              • 3264

                              #15
                              Originally posted by ravimishra11
                              It is in Asp.net Console application.

                              ASP.NET is for website programming. ASP stands for Active Server Pages. I think you might mean .NET Console Application.

                              ASP.NET Console makes no sense.

                              Comment

                              Working...