Any C# 2010 Experts on the Forums?
NWCCTV
Posts: 3,629
I am looking for a C# 2010 novice or expert that can maybe help me out a bit. I know this is not a C# Forum, but waiting on an answer on the MS forums can be daunting.
I have written a program that opens Comm port and communicates to my BS2 BoeBot. I am stuck at the portion I need to get my Servos to move and I am wondering if anyone may have some sample code that I can play with to try and get it to work. Here is what I have that does not work: I am stuck at the _port.WriteLine portion.
I have written a program that opens Comm port and communicates to my BS2 BoeBot. I am stuck at the portion I need to get my Servos to move and I am wondering if anyone may have some sample code that I can play with to try and get it to work. Here is what I have that does not work: I am stuck at the _port.WriteLine portion.
do { in_data = _port.ReadLine(); } while (!(in_data == "RDY")); _port.WriteLine("!" + Constants.vbLf + servo1 + Constants.vbLf + servo2);
Comments
I do not really understand what you are trying to do - but PLEASE consider the following advice.
EDIT: I stand corrected ... there is no UNTIL in C# ... so the following is edited to correct it
There are tons of way to write loops in C# but please remember the differences between 'do...while' and 'while' .
Example: (edited)
will not run at all if condition is not met - it is head-controled - hence the condition belongs on the top
will run at least once - it is foot-controled - hence the condition on the end
I hope that helps you.
If not - please post more of your code - I program in c# since 1998 (beta 1) for a living and can fix any problem for sure.
Enjoy!
Mike
after reading my post i think it might be still unclear
so what you need to do is
EDIT: I stand corrected ... there is no UNTIL in c#
Enjoy!
Mike
Anyway, one thing I have found very useful is a program to sniff what is going on with the serial ports, so you can actually see your bytes going out and coming in. Program attached. Should be fairly simple to install and run.
However, the do...until loop is a little confusing. First off, the ReadLine() method assumes a delimiter character is received in the serial buffer - something like control, linefeed - 0x0D, 0x0A. It is unclear from your post if the BOE sends end of line.
When execution drops into the do...until loop, it will not exist until it sees a "RDY". You could be stuck in an infinite loop. I believe your question is less about C# and more about understanding the logic to accomplish the task at hand.
2 Solutions
1) Use the DataReceived event of the SerialPort object to handle incoming serial data.
2) Poll the serial port every x seconds to check for data received.
DataReceived w/ example code
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx
Serial Port object w/ example code
http://msdn.microsoft.com/en-us/library/30swa673
post # 8 to C#. I thought I had the most of it down but after going through it this morning I am seeing more errors, If you could convert this for me and send it back I would appreciate it. The "RDY" part is on the Stamp side. I am able to get the commuication working and have the "Hello World" from the first example working.
The ""do until" is throwing a "while expected" error.
To look at the "Real Objective". I have this project completed in VB 2010. However, the goal is to use this in conjunction with MS Robotics Studio 4.0. With the latest release of MSRS, VB is not supported for the dssnewservice command. Only C# and BOO. The dssnewservice command MUST be utilized when creating a new Manifest that can be used within MSRS. Does this all make more sense now?
Below is the code I have come up with to move the Servos using 2 Trackbars.
[code][private
void trackBar1_Scroll(object sender, EventArgs e)
{
// Set the window title text each time a scroll occurs.
this.Text = "Value: " + trackBar1.Value;
this.Servo1 = trackBar1.Value;
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
// Set the window title text each time a scroll occurs.
this.Text = "Value: " + trackBar2.Value;
this.Servo2 = trackBar2.Value;
}
/CODE]
to much spin and vb lately ... I have to correct myself... there s no until in c#. So the valid forms are:
while(condition)
{
whatever;
}
and
do
{
whatever;
}
while (condition)
so your loop is correct as is. SORRY...
there is a nice vb-to-c# converter online. Try this to translate your program.
Enjoy!
Mike
Use the same idea with the Slider controls events. Check the ready flag before sending the servo command.
SerialPortConsole.zip
Post your source code for help!
The posted console application gets around this in an asynchronous manor by setting a flag when it sees "RDY" 0x13, 0x10. That's like cocking the gun. Now we're ready to fire. Pulling the trigger and fire the command then immediately resets the flag. While the gun is cocked no position commands are accepted.
A better approach is a request-response scheme. Send a position command and wait for an acknowledge response from the BS2.
As written, the serial port buffer does not flush until a slider is moved. Plus only the last "RDY" 0x13, 0x10 is flushed. If the TrackBar is not moved, eventually the serial port buffer will overrun. This is a logic bug!
The console application gets around overflow exceptions by capturing everything on the serial port, parsing, and invoking logic.
The application source is easily copied into a Win form but you must understand the concepts presented. I specifically wrote the console app to provide an example of consuming the Data_Received event in the context of your query.
I would design a much different communication protocol - Request/Response or Command/Acknowledgement
I did much the same in the console application... only in C#. Plus I made the code modular for copy and paste purposes.
The code you posted polls the serial port when the user moves the TrackBar control. Which is nothing like post #8.
I can code in VB OK, just not in C#. I wish Microsoft would have left well enough alone and kept the VB support in MSRS 4.0. They seem to think that everyone should change because they do not like the way things work.
No, just copy the factory class, event assignment, and handlers.
No, just copy the code to your existing Win form.
The trackbar value can be accessed through the value property; trackbar.Value.
The scroll event should be assigned through a delegate when the form is initialized.
You have the option to roll your own or have the editor stub out the assignment and event handler by double clicking the Scroll event from the TrackBar properties window.
VB.NET and C# build the same intermediate code and target the same .NET framework. VB is a bit more verbose than C# and the editors have different features. However, the framework objects are the same regardless of the language used to invoke the objects.
I know older versions of MSRS handled VB.NET. It looks like MS recommends the C# editor with MSRS 4.0.
And yes, MSRS 4.0 only allows creating new services in C#. Although, If you type in the new service help, it shows VB in the /language function.
Thanks a bunch for your assistance,
http://www.reflector.net/ Use the free trial first.
Check the following link , it will give yo the different types of loops in vb.net.
http://vb.net-informations.com/programflow/vb.net_Program_Flow_Control_tutorials.htm
also the website has lot of vb.net-c# parllal programs .
wills.