University project - Electronic Stethoscope

I[quote]'ve tidied the program up as you suggested, many thanks! I don’t think i’m using the usbserial command properly, how would I tell the cerbuino to stream the analog pin data through the com port?[/quote]

First, you do not need a data received event, since you are not receiving any data from the usbserial connection.

I would either use a timer, or a thread.

With a timer, you could have an event occur periodically, read the port and then write the results to the usbserial connection.

With a timer, you can read the analog port and then write the results to the usbserial connection. You can then do a Thread.Sleep(…) and then repeat.

 void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
{
                int NumberOfBytesToRead = usbSerial.SerialLine.BytesToRead;
                byte[] readInputBuffer = new byte[NumberOfBytesToRead];
                usbSerial.SerialLine.Write(readInputBuffer);
 
}

What this section does is when there is new data to be read, it creates the input buffer and reads those characters. Then, the last line, outputs those same characters back out the serial line with the serial.write() method call.

If you want to read the analog input port, then output it, then instead of writing the readInputBuffer you should first read the analog value in, then use the serialline.Write method to output it. To do that though, your analog input port definition needs to be defined outside programstarted() so just move it’s definition up one level.

Here’s a tip. Try using DEBUG.PRINT() and breakpoints to show what’s going on. You also need to understand what the serial line WRITE method expects as input data and you might have to use the .ToString() method of the returned value.

I guess the second timer is a thread :-X

As Mike says though, you need to tell us what you expect your program to do. Does it respond to data that comes in from the PC? Does it just send the data every second no matter what? Does it just send it as quickly as it can? Knowing how you expect the program to flow will help us give you a pattern that will work best.

@ ks09aao

This thread has been going on for two weeks with little apparent progress. While we are glad to help with questions, we can not do your school project for you.

You should start with a simple program, which only ready the analog port, and displays the results using a Debug.Print statement.

You should then write another simple program, which writes values to the usb serial port.

Once you have done this, you will be in a better state to ask questions about the how to put both actions together.

BTW, if you are planning take the analog values and perform a FFT on the PC, you better make sure you can get a high enough sampling rate and can transfer the data in real time. Further, what will be the effect of the jitter you can expect with the Micro Framework?

Thank you brett that was most helpful! I have rearranged the program as you suggested, I now have one error remaining, “use of unassigned local variable lightsensor”?

I expect the program to output the values from the analoginput always no matter what, the remaining analysis can be done in matlab (which can read COM ports) or my windows form application.

Im sorry for any inconvenience I’ve caused you guys, I know many of the queries I raise are simple, but I’m afraid I’ve never been taught c or c#, and am suddenly asked to use it for my final year project! I really appreciate the help and feedback you’ve given me

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHIElectronics.Gadgeteer;
using Microsoft.SPOT.Hardware;
using GHI.OSHW.Hardware;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace commmmmmmmmmmm
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            AnalogInput lightSensor = new AnalogInput(FEZCerbuino.Pin.AnalogIn.A1);

            usbSerial.Configure(115200,
                    GT.Interfaces.Serial.SerialParity.None,
                    GT.Interfaces.Serial.SerialStopBits.One,
                    8);
            usbSerial.SerialLine.Open();
        }


                   void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
{
                     string lightsensor;
                     int NumberOfBytesToRead = usbSerial.SerialLine.BytesToRead;
                     usbSerial.SerialLine.Write(lightsensor);
  
}

        }
        

              
 
            
           
        }
    



Thanks! ok having moved the line and removed the string, it now comes up with "Argument 1: cannot convert from ‘Microsoft.SPOT.Hardware.AnalogInput’ to ‘string’

So i need to convert the analoginput to type string beforehand?

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHIElectronics.Gadgeteer;
using Microsoft.SPOT.Hardware;
using GHI.OSHW.Hardware;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace commmmmmmmmmmm
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
           

            usbSerial.Configure(115200,
                    GT.Interfaces.Serial.SerialParity.None,
                    GT.Interfaces.Serial.SerialStopBits.One,
                    8);
            usbSerial.SerialLine.Open();
        }


                   void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
{
    AnalogInput lightSensor = new AnalogInput(FEZCerbuino.Pin.AnalogIn.A1);
                     int NumberOfBytesToRead = usbSerial.SerialLine.BytesToRead;
                     usbSerial.SerialLine.Write(lightSensor);
  
}

        }
        

              
 
            
           
        }
    



I’m lost, that’s everything i’ve written! Am i missing important parts of my code?

I’ve found in many of the example programs that people use “setlinearscale” to scale the output, but when i try to use the command it does not exist with my current references, nor does the reference “GHIElectronics.NETMF.FEZ;” which i believe it relies on.

In this code I tried using the command “scale”, which returns the error "Non-invocable member ‘Microsoft.SPOT.Hardware.AnalogInput.Scale’ cannot be used like a method.

should i be using setlinearscale instead? and if so which reference is needed, my setup seems to be missing the aforementioned reference :confused:

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHIElectronics.Gadgeteer;
using Microsoft.SPOT.Hardware;
using GHI.OSHW.Hardware;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace commmmmmmmmmmm
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
           

            usbSerial.Configure(115200,
                    GT.Interfaces.Serial.SerialParity.None,
                    GT.Interfaces.Serial.SerialStopBits.One,
                    8);
            usbSerial.SerialLine.Open();
        }


                   void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
{
    AnalogInput lightSensor = new AnalogInput(FEZCerbuino.Pin.AnalogIn.A1);
                       lightSensor.Scale(0, 100);
                       string lightSensorReading;
    
    while (true)
    {
        
        lightSensorReading = lightSensor.Read();
        usbSerial.SerialLine.Write(lightSensorReading);
    }

}
        }  }
    



@ ks09aao - i hate to say it but you really need to get back to basics and go thru the begineers tutorials etc.

Break things down by doing simple tasks eg. just create a project that reads the sensor to the debug window, then add the serial stuff etc.

At the moment you are wait for the serial to fire your sensor code and that is all wrong.

Datareceived means coming in which you dont need. you need to be sending via serial.

Agree, you need to go to town on the basics manual and really do some simple apps.

But again, I’ll throw you a bone or two.

SetLinearScale does one thing, it tells the AnalogInput to change the value that is read in a certain way - it SCALES it. You can do that manually, if you need to, but you don’t.

Serial data received event is only needed if you are SENDING data from the PC to the Fez device. If you don’t need to ever do that, don’t use it ! And certainly you would NEVER put a while (true) loop inside such an event handler; so you’re using the wrong construct.

If you want to “do something periodically” then you need a timer. If you want to “do something over and over and never stop” then you want a thread. I am betting that you want to put that while loop into it’s own thread. Read up on starting a thread.

Yes, you’re quite right i have allot to learn i’m afraid :frowning:
However, my deadline is this Wednesday to complete the project, and all that remains is this program! Ive got several programs lined up that can perform an FFT on a com port I’m sorry for my ignorance, I’m awfully new to all this!

I think I’ve worked it out now, all that remains is putting the command into a thread, as you suggested Brett!

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using GHIElectronics.Gadgeteer;
using Microsoft.SPOT.Hardware;
using GHI.OSHW.Hardware;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace commmmmmmmmmmm
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
           

            usbSerial.Configure(115200,
                    GT.Interfaces.Serial.SerialParity.None,
                    GT.Interfaces.Serial.SerialStopBits.One,
                    8);
            usbSerial.SerialLine.Open();
        }


                   void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
{
    AnalogInput lightSensor = new AnalogInput(FEZCerbuino.Pin.AnalogIn.A1);                                  
                        usbSerial.SerialLine.Equals(lightSensor);
    

}
        }  }
    



If all you are trying to do is read an analog value ,and then send it to the PC over the serial port, then the code you posted will not work. What command are you going to put into a thread?

i thought the lines

 AnalogInput lightSensor = new AnalogInput(FEZCerbuino.Pin.AnalogIn.A1);                                  
                        usbSerial.SerialLine.Equals(lightSensor);

As this would result in an infinite loop where data is continually sent?
Is is easier instead to use a loop command?

@ ks09aao - Have tried to run any of your code yet? I don’t think it will even compile.

Have you read any of the documentation on what the commands actually do?

People have told you how to do what you want. I suggest you reread this thread carefully.

If your project is due Wednesday, you might be in a bit trouble.

The code runs, but i dont think any output comes out, nothing is seen in matlab or in the serialchart program recomended earlier on in this thread.

Your quite right time is running short, but i have done a great deal of work, this is the ony barrier remaining! If i can just send my analog values to a com port ill be fine :slight_smile:

@ ks09aao - Do you understand what Equals method does?

And have you proven that you can send any data to the PC and read it in ?

There must be a bazillion topics here and the codeshare that have a new thread in a Gadgeteer app that you can look to for inspiration, but it’s something like

[quote]System.Threading.Thread someThread = new Thread(MyMethodName);
[/quote]

I think you are hoping that someone will write/correct the program for you. It will not be me.

As i understood it, equals can be used to refer to an object within the program?

Yes, I can most definitely read the com-port values, My university supervisor tested my program with an existing FEZ and it works fine!
What i’m trying to do is replicate his programming, I’ve got most of the software work done!

I never asked for someone to do it for me Mike, only aid in getting to the solution. I fully intend to get this working through my own hard work :slight_smile: