Thread: C# output based on data read by serial port.

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Smile C# output based on data read by serial port.

    Greetings ppl..im doing this assignment on C# programming and im really really stucked right now.

    What i need to do is to read either '0' or '1' from a circuit ( circuit is connected to my laptop via serial port ), and based on either '0' or '1', different videos ( video 1 or video 2) will be played.

    The following is my source code by far, and there're 6 errors which popped out and i have no idea how to fix it.


    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO.Ports;
    
    namespace cs_open_videos
    {
        class Program
        {
            static void Main(string[] args)
            {
                OpenSerialPort();
                //String fileToOpen = "C:/myog.avi"; System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo("C:/Program Files/Windows Media Player/wmplayer.exe/play/fullscreen",  fileToOpen); System.Diagnostics.Process.Start(ps);
            } 
            SerialPort com=new SerialPort();
            private void OpenSerialPort()
            {
                SerialPort com=new SerialPort();
    
                com.BaudRate=38400;
                
                com.Open();
                com.DataReceived = OnRecieved;
            
            }
            private void OnRecieved(object sender, SerialDataReceivedEventArgs c)
            {
    
                char c = com.ReadChar();
                switch (c)
                {
                    case "0":
                        
    String fileToOpen = "C:/video1.avi"; System.Diagnostics.ProcessStartInfo 
     ps = new 
    System.Diagnostics.ProcessStartInfo("C:/Program Files/Windows Media Player/wmplayer.exe/play/fullscreen",  fileToOpen); System.Diagnostics.Process.Start(ps);
                    case "1" :
                String fileToOpen = "C:/video2.avi"; System.Diagnostics.ProcessStartInfo ps = new 
    System.Diagnostics.ProcessStartInfo("C:/Program Files/Windows Media Player/wmplayer.exe/play/fullscreen", fileToOpen); System.Diagnostics.Process.Start(ps);
      
    
                        play();
                
                }
                
            
            }
        }
    }


    apologies if it's kinda messy, but ppl pls help this beginner here if u can

    Thank you in advance.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Post the errors

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well first of all you have two "com" objects, one class member, one local variable. I'm sure you've mixed up these two.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Wink the errors which occured in my code.

    Error 1
    An object reference is required for the nonstatic field,
    method, or property 'cs_open_videos.Program.OpenSerialPort()'

    Error 2
    The event 'System.IO.Ports.SerialPort.DataReceived' can only appear on the left hand side of += or -=

    Error 3
    A local variable named 'c' cannot be declared in this scope because it would give a different meaning to 'c', which is already used in a 'parent or current' scope to denote something els

    Error 4
    Cannot implicitly convert type 'int' to 'System.IO.Ports.SerialDataReceivedEventArgs'

    Error 5
    A value of an integral type expected

    Error 6
    A local variable named 'fileToOpen' is already defined in this scope

    Error 7
    A local variable named 'ps' is already defined in this scope

    Error 8
    The name 'play' does not exist in the current context

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well each of them would come with line numbers as well.

    In fact, in the IDE, you would then typically just double-click on the error message to get to the line.

    Pressing F1 might give you help on the error message as well.

    For example
    > A local variable named 'fileToOpen' is already defined in this scope
    1. Change it's name
    2. re-use the previous one
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) Don't really know what it means. Give the code that has that piece of code in it. Maybe you don't create an object?
    2) What it says. System.IO.Ports.SerialPort.DataReceived is an event and you can set an event handler to it with += or remove one with -=. If you use = then just use += instead and you ll get the result you wanted.
    3) Well 'c' is really a bad named for a non-local variable. So change it. If you have a variable in a class named c and you declare a method in the class that has a local variable c then you get an error, for obvious reasons.
    4) What it says. You might want to cast but I don't think that will be correct.
    5) Integral types are like int, char, byte etc etc
    6) Change the name
    7) Change the name
    8) Well simply it doesn't exist. Maybe you want ObjectName.play() instead

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. HELP with storing serial port data into txt file
    By inmaterichard in forum C Programming
    Replies: 2
    Last Post: 04-02-2008, 02:20 AM
  3. Reading and writing to a serial port
    By SwarfEye in forum C Programming
    Replies: 2
    Last Post: 08-18-2006, 12:28 AM
  4. my serial port data reading program isn't working
    By gnychis in forum C Programming
    Replies: 5
    Last Post: 06-02-2005, 08:40 AM
  5. how to get data from the serial port?
    By Jasonymk in forum C++ Programming
    Replies: 1
    Last Post: 02-26-2003, 07:31 AM

Tags for this Thread