Let's Make Robots!

C# Serial Reading Continually Problem & Force Bar

Here is my setup:

 - Windows 7 with Visual Studio 2012 Express.
 - Coding in C#.
 - Arduino UNO providing serial inputs to the computer (via USB).
 - Flexiforce Pressure Sensor plugged into the Ardiuno (and working).

So here is what my interface in the C# program looks like, with the names of the items beside it:


And here is the code I have at the moment:


That is the cleanest I can make it look.
I have tried many, many tutorials and pieces of code, but I just cannot get my head around how to get continual reads of the information coming off the serial port, and make it relate to anything in my C# program.

So I thought I'd just see if I could get continual reads into the ForceTextbox, once the GetForceButton is pushed.

Can anyone help me with what code needs to go into the private void GetForceButton_Click section, so that the reads from the serial port are populating the ForceTextbox?

Thanks for any help.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Do you have to send a command to get the arduino to give a measurement? My laptop is in work just now, so i cant post code till tomorrow but i see there could be three ways this could go. 1 you have to send something to get a response, so serial.write("the command goes here"); set a flag to say waiting for response. Parse the response in the recieved handler, update the progress bar. 2 the arduino sends data constantly? Does it add a newline after the data? In the recieved handerler you can use serial.readline() until it returns a null, parse data and display. 3 the data comes continuously without a lime ending, repeat until serial.read returns null, mybuffer+=serial,read(); Then loop through the data parsing out the info you need, every so often you can bin whats in the buffer, as you sometimes get junk accumulating in there. Sorry i cant help more today, but if your still stuck tomorrow, i will post up an example.

Its midnight, I have to be up in the morning. I can't provide too much aid right now but if its not solved tomorrow I can.

 

 

You need to choose a max value for the force you receive. Map your last actual value to 0-100% depending on the value compared to max. Set the progress bar to this percentage. I can't recall if there is a map function in C#, if not the arduino documentation for the map function has the algorithm so it should be quick and easy to write your own.

You will need to handle the format the string comes in as, would help to have arduino code in that regard.

 

 

 

For continual text going into the textbox.

Add a bool somewhere initialised to false. When the button is clicked, set it to true. In the serial data receive handler check if the bool is true and if it is, append your text to the textbox. From memory: ForceTextbox.Text += indata + "\n";

If \n doesnt work, try Environment.NewLine or something like that, intellisense will pick it up. Make sure the textbox is multiline (properties).

 

 

If thats all a load of delusional ravings, I've just spent hours looking at assignments and shouldnt be on LMR at all, I'll write something more useful tomorrow :P