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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How to read data from COM port (want to get remote to work)

How to read data from COM port (want to get remote to work)

Scheduled Pinned Locked Moved C#
csharpcomhelptutorialquestion
7 Posts 4 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.
  • D Offline
    D Offline
    daniilzol
    wrote on last edited by
    #1

    Hi, I've got Creative Infra Remote and I want to get it working. Yes, it does have some program that comes with it, but I want to try to write my own. The remote is this one: Creative Infra Remote It's using COM port. I connected it and remote itself is working (I verified it by installing supplied software). The problem is I cannot get it working with NET2.0. I've looked at several articles here on codeproject and on the web, I created test app, but it just won't work. I create SerialPort class using IO.Ports namespace, I open port and then I try to read data either on a button or using DataReceived event and yet, no matter what I try it won't work. The event never fires. I know port is opening because when I run software supplied with the remote and my application at the same time, whenever I try to open port in my test app it throws exception that COM1 port is already used and access is denied. However, even though the port is opened I cannot receive any data. Am I supposed to receive any data at all using ReadLine or Read or ReadExisting methods? Or am I supposed to do something else? Can anyone point me in the right direction? Thanks.

    D 1 Reply Last reply
    0
    • D daniilzol

      Hi, I've got Creative Infra Remote and I want to get it working. Yes, it does have some program that comes with it, but I want to try to write my own. The remote is this one: Creative Infra Remote It's using COM port. I connected it and remote itself is working (I verified it by installing supplied software). The problem is I cannot get it working with NET2.0. I've looked at several articles here on codeproject and on the web, I created test app, but it just won't work. I create SerialPort class using IO.Ports namespace, I open port and then I try to read data either on a button or using DataReceived event and yet, no matter what I try it won't work. The event never fires. I know port is opening because when I run software supplied with the remote and my application at the same time, whenever I try to open port in my test app it throws exception that COM1 port is already used and access is denied. However, even though the port is opened I cannot receive any data. Am I supposed to receive any data at all using ReadLine or Read or ReadExisting methods? Or am I supposed to do something else? Can anyone point me in the right direction? Thanks.

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      JazzJackRabbit wrote:

      The problem is I cannot get it working with NET2.0.

      You can't get it working with your app, not .NET. Did you open the correct COM port? Did you use the correct parameters?? Baud rate, data bits, stop bits, flow control?

      Dave Kreskowiak Microsoft MVP - Visual Basic

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        JazzJackRabbit wrote:

        The problem is I cannot get it working with NET2.0.

        You can't get it working with your app, not .NET. Did you open the correct COM port? Did you use the correct parameters?? Baud rate, data bits, stop bits, flow control?

        Dave Kreskowiak Microsoft MVP - Visual Basic

        D Offline
        D Offline
        daniilzol
        wrote on last edited by
        #3

        Yes, the port is correct, COM1. I did not use any parameters since I don't know what they are supposed to be and all of the examples I've looked at use default (empty) constructor. I actually don't know if I can get it to work at all. I've downloaded WinLIRC, just to see if it will be able to read data from remote, but it won't work either. -- modified at 18:06 Saturday 30th December, 2006 OK, now I'm getting somewhere. Googled for CIMR100 which is the name of the receiver and used the following settings: m_SerialPort = new SerialPort("COM1",9600,Parity.None,8,StopBits.One); m_SerialPort.DtrEnable = true; m_SerialPort.RtsEnable = true; And it appears to work, at least DataReceived event is firing. I get an error though: "Cross-thread operation not valid: Control 'txtInput' accessed from a thread other than the thread it was created on." when I try to show data which was read, but at least that's a start and I can work with it.

        G 1 Reply Last reply
        0
        • D daniilzol

          Yes, the port is correct, COM1. I did not use any parameters since I don't know what they are supposed to be and all of the examples I've looked at use default (empty) constructor. I actually don't know if I can get it to work at all. I've downloaded WinLIRC, just to see if it will be able to read data from remote, but it won't work either. -- modified at 18:06 Saturday 30th December, 2006 OK, now I'm getting somewhere. Googled for CIMR100 which is the name of the receiver and used the following settings: m_SerialPort = new SerialPort("COM1",9600,Parity.None,8,StopBits.One); m_SerialPort.DtrEnable = true; m_SerialPort.RtsEnable = true; And it appears to work, at least DataReceived event is firing. I get an error though: "Cross-thread operation not valid: Control 'txtInput' accessed from a thread other than the thread it was created on." when I try to show data which was read, but at least that's a start and I can work with it.

          G Offline
          G Offline
          gnadeem
          wrote on last edited by
          #4

          hi, good that u got it working. the last error u mentioned is a result of check by the runtime that a thread can access a control only if it created that control. possibly you are accessing txtInput created by 'Main' thread in some other thread. have a look at this. http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx[^] regards.

          D 1 Reply Last reply
          0
          • G gnadeem

            hi, good that u got it working. the last error u mentioned is a result of check by the runtime that a thread can access a control only if it created that control. possibly you are accessing txtInput created by 'Main' thread in some other thread. have a look at this. http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx[^] regards.

            D Offline
            D Offline
            daniilzol
            wrote on last edited by
            #5

            Yes, I've read this webpage already (that's what VS IDE advised to read when it displayed the error). I don't multithread for now and I don't think I will (doesn't make sense in an app like this, although it may change later). For now all I have is one main form frmMain, I create SerialPort object on the main form, I attach DataReceived event to the SerialPort object and let it be. Basically something like this:

            m_SerialPort = new SerialPort("COM1",2400,Parity.None,8,StopBits.One);
            m_SerialPort.DtrEnable = true;
            m_SerialPort.RtsEnable = true;
            m_SerialPort.Open();

            m_SerialPort.DataReceived += new SerialDataReceivedEventHandler(m_SerialPort_DataReceived);

            void m_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
            txtInput.Text += " " + m_SerialPort.ReadExisting();
            }

            Funny, I only see this error when running application from VS IDE. If I run executable from release folder it runs just fine, no error thrown or nothing. This may sound dumb, but this is the first time I see an error like this when I simply access textbox from an event. Am I missing something here, have I been doing something fundamentally wrong for the past three years? And why do I only see this error when running IDE, not executable?

            L 1 Reply Last reply
            0
            • D daniilzol

              Yes, I've read this webpage already (that's what VS IDE advised to read when it displayed the error). I don't multithread for now and I don't think I will (doesn't make sense in an app like this, although it may change later). For now all I have is one main form frmMain, I create SerialPort object on the main form, I attach DataReceived event to the SerialPort object and let it be. Basically something like this:

              m_SerialPort = new SerialPort("COM1",2400,Parity.None,8,StopBits.One);
              m_SerialPort.DtrEnable = true;
              m_SerialPort.RtsEnable = true;
              m_SerialPort.Open();

              m_SerialPort.DataReceived += new SerialDataReceivedEventHandler(m_SerialPort_DataReceived);

              void m_SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
              {
              txtInput.Text += " " + m_SerialPort.ReadExisting();
              }

              Funny, I only see this error when running application from VS IDE. If I run executable from release folder it runs just fine, no error thrown or nothing. This may sound dumb, but this is the first time I see an error like this when I simply access textbox from an event. Am I missing something here, have I been doing something fundamentally wrong for the past three years? And why do I only see this error when running IDE, not executable?

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

              I expect most IO drivers to use at least one additional thread to handle incoming data: it needs buffering, decoding, signalling, etc even when there is no client thread actively asking for anything from the IO. The obvious way to achieve this is for the IO code to create a local thread; when something needs signalling (thru a callback function, an event handler, whatever), I expect that to be handled by the same additional thread. If you want to verify this theory, you should try to obtain a thread ID (I do not know by heart how to do this with managed code), do it once in the GUI thread, and once in the DataReceived handler, and compare them ! Even simpler, you could just print the value of someControl.InvokeRequired from within the DataReceived handler; if it is true, the above has been proven. Also the MSDN documentation clearly states "The DataReceived event is raised on a secondary thread when data is received from the SerialPort object". (see ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref8/ html/E_System_IO_Ports_SerialPort_DataReceived.htm) So in order to access GUI elements from receive handlers (DataReceived, and similarly PinChanged and ErrorReceived) you should use Invoke and/or BeginInvoke to delegate the GUI work to the right thread. BTW, that your program works right under some circumstances does not prove it is correct; especially threading and synchronization problems can be hidden and only show under some circumstances. Only correct code works all the time. :) -- modified at 22:34 Saturday 30th December, 2006

              Luc Pattyn

              D 1 Reply Last reply
              0
              • L Luc Pattyn

                I expect most IO drivers to use at least one additional thread to handle incoming data: it needs buffering, decoding, signalling, etc even when there is no client thread actively asking for anything from the IO. The obvious way to achieve this is for the IO code to create a local thread; when something needs signalling (thru a callback function, an event handler, whatever), I expect that to be handled by the same additional thread. If you want to verify this theory, you should try to obtain a thread ID (I do not know by heart how to do this with managed code), do it once in the GUI thread, and once in the DataReceived handler, and compare them ! Even simpler, you could just print the value of someControl.InvokeRequired from within the DataReceived handler; if it is true, the above has been proven. Also the MSDN documentation clearly states "The DataReceived event is raised on a secondary thread when data is received from the SerialPort object". (see ms-help://MS.VSExpressCC.v80/MS.NETFramework.v20.en/cpref8/ html/E_System_IO_Ports_SerialPort_DataReceived.htm) So in order to access GUI elements from receive handlers (DataReceived, and similarly PinChanged and ErrorReceived) you should use Invoke and/or BeginInvoke to delegate the GUI work to the right thread. BTW, that your program works right under some circumstances does not prove it is correct; especially threading and synchronization problems can be hidden and only show under some circumstances. Only correct code works all the time. :) -- modified at 22:34 Saturday 30th December, 2006

                Luc Pattyn

                D Offline
                D Offline
                daniilzol
                wrote on last edited by
                #7

                OK, thanks a lot for explanation. I'll continue playing with it for the next couple of days/weeks, hopefully I won't lose my interest in it after a few days just like with many of my other projects :laugh:

                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