the result of the code is appeared in the serial monitor of the arduino and on the console application...however the data is not appeared on the windows form? is there any wrong with my code?
Your Arduino is running code outputting data to serial port.
You want the data Arduino is sending to serial port to be read into your C# program and displayed on a Windows Form.
I think you have your read and write backwards. You aren't trying to WRITE the output from serial to your C# GUI, you want to READ the output from Arduino, into your C# program and then display it in the GUI.
set a string variable in C# = sp1.readline()
Then set the GUI element ".TEXT" field = to the string variable you created to display it.
Simple serial monitor test code that sends back what is sent to the arduino. If this works, then the issue is with your pc code. If your pc code opens/closes the pc com port on each write, that will cause the arduino to reset each write.
// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial test 0021"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(2); //delay to allow byte to arrive in input buffer
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
readString="";
}
}
There are some examples on stackexchange and perhaps also on the arduino forum. That being said, I attached a small project with a multiline textbox that receives messages from the serial interface (as well as a dropdown to select and connect with the proper COM) It should be easily modifiable for your purposes.
how to seperate the the thread from UI, PaulS?sorry for asking as im new in c#...from the search in the tutorial, it seems that i have to use the backgroundworker code..how i use that in proper manner?