Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« on: November 10, 2011, 06:19:13 pm » |
i made a arduino program that sends joystick info like: "X,Y,SpeedPot,btn1,btn2,btn3,btn" 4 x, y and speedpot are values 1023 to 0 depending on where joystick is, and btn1... are 1 or 0 depending on if the buttons are triggered or not on the joystick. real output like: 563,521,411,1,1,1,1 SEE 2nd reply for C# code and new quesion... Arduino Code: //-----Axis-------------------------------------------------------// #define Xaxis 3 //Joystick X axis Analog pin 3 #define Yaxis 4 //Joystick Y axis Analog pin 4 #define SpeedPot 5 //Pot in base for speed, Analog pin 5
int XaxisVar = 0; //...analog pin 3---X axis int YaxisVar = 0; //...analog pin 4---Y axis int SpeedVar = 0; //...analog pin 5---Speed pot //----------------------------------------------------------------//
//-----Buttons----------------------------------------------------// #define TrigBtn 2 //trigger button, digital 2 #define MidBtn 6 //Button below trigger on joystick, digital 3 #define LowBtn 4 //Lowest button on the joystick, digital 4 #define BaseBtn 5 //button on base of joystick, digital 5
int TrigBtnState = 0; //Stores the state of TrigBtn int MidBtnState = 0; //...MidBtnState int LowBtnState = 0; //...LowBtnState int BaseBtnState = 0; //...BaseBtnState //----------------------------------------------------------------//
void setup() { Serial.begin(9600);
for (int pin=2; pin<=5; ++pin) //sets all digital pin 2-5 as input and enables internal pullups for buttons { pinMode(pin, INPUT); //sets button pins to input digitalWrite(pin, HIGH); // sets the pull up. } }
void loop() { XaxisVar = analogRead(Xaxis); //Read X-Axis postion YaxisVar = analogRead(Yaxis); //Read Y-Axis postion SpeedVar = analogRead(SpeedPot); //Read Speed Pot postion
TrigBtnState = digitalRead(TrigBtn); //read status of buttons on joystick MidBtnState = digitalRead(MidBtn); //... LowBtnState = digitalRead(LowBtn); //... BaseBtnState = digitalRead(BaseBtn); //...
//Just serial ouput formatting... String Comma = String(","); String Data = String(XaxisVar + Comma + YaxisVar + SpeedVar + TrigBtnState + Comma + MidBtnState + Comma + LowBtnState + Comma + BaseBtnState); Serial.println(Data); } also, as you can see here: String Comma = String(","); String Data = String(XaxisVar + Comma + YaxisVar + SpeedVar + TrigBtnState + Comma + MidBtnState + Comma + LowBtnState + Comma + BaseBtnState); Serial.println(Data); i have to make a "," string and i cant put a real comma in the data string like "," because it gives me an error, something about overflow. also it maybe a coincidence, but my computer severely lagged for 5 minutes after i ran the compiled C# program, then i forced stopped it in task manger(still ran after closing CMD) and it recovered a few minutes later. thanks
|
|
|
|
« Last Edit: November 11, 2011, 04:51:25 pm by sirbow2 »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 37
Arduino rocks
|
 |
« Reply #1 on: November 11, 2011, 01:25:18 am » |
How are you opening and closing the com ports in C#? What program are you using? I used visual studio express and I sent and received data from the arduino but used a byte array as my memory buffer, then read the data into the buffer. My application was simple data but it should work the same. If you can give me those details I might be able to dig through my code for some examples.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #2 on: November 11, 2011, 04:51:40 pm » |
ok it works fine now, i can receive serial data, and i figured out how to get the ArrayList JoyData from " private void DisplayText" in to public form1() so that i can use the coordinates in it to update the graph, but how do i get the number value stored in the second spot of JoyData (like JoyData[2]) into int form so taht it can be used on the graph? thanks. C# code: using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports;
namespace SimpleSerial { public partial class Form1 : Form { // Add this variable string RxString; ArrayList JoyData = new ArrayList();
private System.Windows.Forms.Button buttonStart; private System.Windows.Forms.Button buttonStop; private System.Windows.Forms.TextBox textBox1; private System.IO.Ports.SerialPort serialPort1;
public Form1() { // //Graph // this.Width = 512; this.Height = 512; //math for mapping 1023 to 512 from JoyData int y=JoyData(2) * 512 / 1023 + 0; int x=JoyData(1) * 512 / 1023 + 0;
Bitmap bmp = new Bitmap(512, 512); Graphics g = Graphics.FromImage(bmp); //draw axis lines g.DrawLine(new Pen(Color.Black, 2), 0, 240, 512, 240); g.DrawLine(new Pen(Color.Black, 2), 253, 0, 253, 512); // let's draw a coordinate g.DrawString("X", new Font("Calibri", 12), new SolidBrush(Color.Black), y - 10, x - 26);
PictureBox display = new PictureBox(); display.Width = 512; display.Height = 512; this.Controls.Add(display); display.Location = new Point(300, 5); display.Image = bmp; // //init // this.buttonStart = new System.Windows.Forms.Button(); this.buttonStop = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.serialPort1 = new System.IO.Ports.SerialPort(); this.SuspendLayout(); // // buttonStart // this.buttonStart.Location = new System.Drawing.Point(13, 13); this.buttonStart.Name = "buttonStart"; this.buttonStart.Size = new System.Drawing.Size(75, 23); this.buttonStart.TabIndex = 0; this.buttonStart.Text = "Start"; this.buttonStart.UseVisualStyleBackColor = true; this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click); // // buttonStop // this.buttonStop.Enabled = false; this.buttonStop.Location = new System.Drawing.Point(105, 12); this.buttonStop.Name = "buttonStop"; this.buttonStop.Size = new System.Drawing.Size(75, 23); this.buttonStop.TabIndex = 1; this.buttonStop.Text = "Stop"; this.buttonStop.UseVisualStyleBackColor = true; this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(13, 43); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(267, 211); this.textBox1.TabIndex = 2; this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); // // serialPort1 // this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(820, 525); this.Controls.Add(this.textBox1); this.Controls.Add(this.buttonStop); this.Controls.Add(this.buttonStart); this.Name = "Joystick C# Interface"; this.Text = "Joystick C# Interface"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.ResumeLayout(false); this.PerformLayout(); }
// static SerialPort serialPort1; static void Main() { //serialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
private void buttonStart_Click(object sender, EventArgs e) { serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600;
serialPort1.Open(); if (serialPort1.IsOpen) { buttonStart.Enabled = false; buttonStop.Enabled = true; textBox1.ReadOnly = false; } }
private void buttonStop_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); buttonStart.Enabled = true; buttonStop.Enabled = false; textBox1.ReadOnly = true; } }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { // If the port is closed, don't try to send a character. if (!serialPort1.IsOpen) return;
// If the port is Open, declare a char[] array with one element. char[] buff = new char[1];
// Load element 0 with the key character. buff[0] = e.KeyChar;
// Send the one character buffer. serialPort1.Write(buff, 0, 1);
// Set the KeyPress event as handled so the character won't // display locally. If you want it to display, omit the next line. e.Handled = true; }
private void DisplayText(object sender, EventArgs e) { textBox1.AppendText(RxString);
//Handle data recieved; something like: 1023,1023,1,1,0,1, string[] split = RxString.Split(new Char[] { ',' });
foreach (string s in split) { if (s.Trim() != "") { JoyData.Add(s); //adds each new value split(s) from SerRead string to JoyData array Console.WriteLine("split #s:"); Console.WriteLine(s); } } }
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText)); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #3 on: November 12, 2011, 07:09:20 pm » |
anything?
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35472
Seattle, WA USA
|
 |
« Reply #4 on: November 12, 2011, 07:26:37 pm » |
Why are you using a generic collection, ArrayList, instead of a specific collection?
You are getting the data for the ArrayList collection from an array of string objects. The Int32 class has a Parse() method that converts a string to an int. Convert each string to an int, and store the ints in an array.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #5 on: November 12, 2011, 08:32:56 pm » |
i originaly wanted to use an array, but i didnt know how to change the values of the array as they were read from serial... wow im stupid, heh i see how i could do it now. i now have "int[] JoyData = new int[7];" at the beginning of the code where "ArrayList JoyData = new ArrayList();" was //Handle data recieved; something like: 1023,1023,1023,1,1,0,1, string[] split = RxString.Split(new Char[] { ',' });
int cntwrite=0; foreach (string s in split) { if (s.Trim() != "") { int c = Int32.Parse(s); JoyData[cntwrite]=c; //adds each new value split(s) from SerRead string to JoyData array Console.WriteLine("split #s:"); Console.WriteLine(c); ++cntwrite; if(cntwrite >7) //reset cntwrite for next time serial data is recieved { cntwrite=0; } } } but now when i try to use int y=JoyData[2] * 512 / 1023 + 0; int x=Joydata[1] * 512 / 1023 + 0; for the graph in "public Form1()" i get "JoyData doesnt exsist in this context". i thought declaring the JoyData array in the class would fix it, but the error only happens when i use "int y=JoyData[0]"(trying to access first number stored in array), but if i use "int y=JoyData[3]" it doesn't give me an error. arrays start at 0 so why does this happen? ok thats really weird, now it works....
|
|
|
|
« Last Edit: November 12, 2011, 10:41:56 pm by sirbow2 »
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #6 on: November 13, 2011, 12:17:34 am » |
sorry, got another issue... in this code: int x=JoyData[0]/2; int y=JoyData[1]/2;
Bitmap bmp = new Bitmap(512, 512); Graphics g = Graphics.FromImage(bmp); //draw axis lines g.DrawLine(new Pen(Color.Black, 2), 0, 240, 512, 240); g.DrawLine(new Pen(Color.Black, 2), 253, 0, 253, 512); // let's draw a coordinate g.DrawString("•", new Font("Calibri", 18), new SolidBrush(Color.Red), y - 12, x - 30);
PictureBox display = new PictureBox(); display.Width = 512; display.Height = 512; this.Controls.Add(display); display.Location = new Point(300, 5); display.Image = bmp; even though i can now use JoyData in this code, it doesn't work. JoyData[1] and JoyData[2] are both equal to about 550 when joy stick is centered, then i divide by 2 so it fits on my graph. except no got shows up any where, not even around the center of the graph. if i set x and y manually to 512 it puts a dot right smack in the center. i think i may know why, jut not how no fix it... private void DisplayText(object sender, EventArgs e) { textBox1.AppendText(RxString);
//Handle data recieved; something like: 1023,1023,1023,1,1,1,1, string[] split = RxString.Split(new Char[] {','});
int cntwrite=0; foreach (string s in split) { if (s.Trim() != "") { int c = Int32.Parse(s); JoyData[cntwrite]=c; //adds each new value split(s) from SerRead string to JoyData array ++cntwrite; if(cntwrite >6) //reset cntwrite for next time serial data is recieved { cntwrite=0; } } } textBox2.AppendText("X:"); textBox2.AppendText(JoyData[0].ToString()+Environment.NewLine); textBox2.AppendText("Y:"); textBox2.AppendText(JoyData[1].ToString()+Environment.NewLine); textBox2.AppendText("S:"); textBox2.AppendText(JoyData[2].ToString()+Environment.NewLine); textBox2.AppendText(Environment.NewLine); } i set up another text box to display the receiving of serial data and the splitting afterwards: in: 535,502,1023,1,1,1,1
split(this is just for one "535,502,1023,1,1,1,1": X:535 Y:502 S:0
X:1023 Y:1 S:1
X:1 Y:1 S:1
|
|
|
|
« Last Edit: November 13, 2011, 12:19:47 am by sirbow2 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35472
Seattle, WA USA
|
 |
« Reply #7 on: November 13, 2011, 09:27:49 am » |
I think that you need to write out the values in the JoyData array before you try to use them.
Posting your entire code would be useful, too. Having to scroll back through several posts, and imagine that the only changes you made were the ones you described makes it difficult to follow what the code you see looks like.
One of the issues that you are having is that the serialPort1 object has a handler registered to invoke when serial data arrives. That handler runs in a separate thread. Sharing data between threads (the user interface thread and the serial receive thread) is not quite as simple as you are doing it. The serial thread can not update the user interface.
I have a C# application that reads from an Arduino, and uses proper thread sharing techniques to share data between the user interface thread and the serial thread.
PM me with an e-mail address if you would like that code. You could easily adapt it to do something other than show the serial data in a text field.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #8 on: November 13, 2011, 10:15:30 am » |
ahh good point about the threading. by email is sirbow2@gmail.comusing System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports;
namespace SimpleSerial { public partial class Form1 : Form { // Add this variable string RxString; int[] JoyData = new int[6];
private System.Windows.Forms.Button buttonStart; private System.Windows.Forms.Button buttonStop; private System.Windows.Forms.Button buttonClear; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.IO.Ports.SerialPort serialPort1;
public Form1() { // //Graph // this.Width = 512; this.Height = 512; int x=JoyData[0]/2; int y=JoyData[1]/2;
Bitmap bmp = new Bitmap(512, 512); Graphics g = Graphics.FromImage(bmp); //draw axis lines g.DrawLine(new Pen(Color.Black, 2), 0, 240, 512, 240); g.DrawLine(new Pen(Color.Black, 2), 253, 0, 253, 512); // let's draw a coordinate g.DrawString("•", new Font("Calibri", 18), new SolidBrush(Color.Red), y - 12, x - 30);
PictureBox display = new PictureBox(); display.Width = 512; display.Height = 512; this.Controls.Add(display); display.Location = new Point(300, 5); display.Image = bmp; // //init // this.buttonStart = new System.Windows.Forms.Button(); this.buttonStop = new System.Windows.Forms.Button(); this.buttonClear = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.serialPort1 = new System.IO.Ports.SerialPort(); this.SuspendLayout(); // // buttonStart // this.buttonStart.Location = new System.Drawing.Point(13, 12); this.buttonStart.Name = "buttonStart"; this.buttonStart.Size = new System.Drawing.Size(75, 23); this.buttonStart.TabIndex = 0; this.buttonStart.Text = "Start"; this.buttonStart.UseVisualStyleBackColor = true; this.buttonStart.Click += new System.EventHandler(this.buttonStart_Click); // // buttonStop // this.buttonStop.Enabled = false; this.buttonStop.Location = new System.Drawing.Point(105, 12); this.buttonStop.Name = "buttonStop"; this.buttonStop.Size = new System.Drawing.Size(75, 23); this.buttonStop.TabIndex = 1; this.buttonStop.Text = "Stop"; this.buttonStop.UseVisualStyleBackColor = true; this.buttonStop.Click += new System.EventHandler(this.buttonStop_Click); // // buttonClear // this.buttonClear.Location = new System.Drawing.Point(197, 12); this.buttonClear.Name = "buttonClear"; this.buttonClear.Size = new System.Drawing.Size(75, 23); this.buttonClear.TabIndex = 1; this.buttonClear.Text = "Clear"; this.buttonClear.UseVisualStyleBackColor = true; this.buttonClear.Click += new System.EventHandler(this.buttonClear_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(13, 43); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ReadOnly = true; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(267, 211); this.textBox1.TabIndex = 2; this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); // // textBox2 // this.textBox2.Location = new System.Drawing.Point(13, 263); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.ReadOnly = true; this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox2.Size = new System.Drawing.Size(267, 211); this.textBox2.TabIndex = 2; // // serialPort1 // this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(820, 525); this.Controls.Add(this.textBox1); this.Controls.Add(this.textBox2); this.Controls.Add(this.buttonStop); this.Controls.Add(this.buttonStart); this.Controls.Add(this.buttonClear); this.Name = "Joystick C# Interface"; this.Text = "Joystick C# Interface"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.ResumeLayout(false); this.PerformLayout(); }
// static SerialPort serialPort1; static void Main() { //serialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }
private void buttonStart_Click(object sender, EventArgs e) { serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600;
serialPort1.Open(); if (serialPort1.IsOpen) { buttonStart.Enabled = false; buttonStop.Enabled = true; textBox1.ReadOnly = false; } }
private void buttonStop_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); buttonStart.Enabled = true; buttonStop.Enabled = false; textBox1.ReadOnly = true; } } private void buttonClear_Click(object sender, EventArgs e) { textBox1.Clear(); textBox2.Clear(); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (serialPort1.IsOpen) serialPort1.Close(); }
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { // If the port is closed, don't try to send a character. if (!serialPort1.IsOpen) return;
// If the port is Open, declare a char[] array with one element. char[] buff = new char[1];
// Load element 0 with the key character. buff[0] = e.KeyChar;
// Send the one character buffer. serialPort1.Write(buff, 0, 1);
// Set the KeyPress event as handled so the character won't // display locally. If you want it to display, omit the next line. e.Handled = true; }
private void DisplayText(object sender, EventArgs e) { textBox1.AppendText(RxString);
//Handle data recieved; something like: 1023,1023,1023,1,1,1,1, string[] split = RxString.Split(new Char[] {','});
int cntwrite=0; foreach (string s in split) { if (s.Trim() != "") { int c = Int32.Parse(s); JoyData[cntwrite]=c; //adds each new value split(s) from SerRead string to JoyData array ++cntwrite; if(cntwrite >6) //reset cntwrite for next time serial data is recieved { cntwrite=0; } } } textBox2.AppendText("X:"); textBox2.AppendText(JoyData[0].ToString()+Environment.NewLine); textBox2.AppendText("Y:"); textBox2.AppendText(JoyData[1].ToString()+Environment.NewLine); textBox2.AppendText("S:"); textBox2.AppendText(JoyData[2].ToString()+Environment.NewLine); textBox2.AppendText(Environment.NewLine); }
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { RxString = serialPort1.ReadExisting(); this.Invoke(new EventHandler(DisplayText)); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35472
Seattle, WA USA
|
 |
« Reply #9 on: November 13, 2011, 10:29:00 am » |
int cntwrite=0; foreach (string s in split) { if (s.Trim() != "") { int c = Int32.Parse(s); JoyData[cntwrite]=c; //adds each new value split(s) from SerRead string to JoyData array ++cntwrite; if(cntwrite >6) //reset cntwrite for next time serial data is recieved { cntwrite=0; } } } This will put data in JoyArray[0], [1], [2], [3], [4], [5], and [6], before wrapping back to 0. JoyArray is declared as a 6 element array, with indices from 0 to 5. Writing to [6] is generally not advised.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #10 on: November 13, 2011, 10:51:45 am » |
ok so i changed the array to have 7 "spots". i got an error on your code: System.ArgumentOutOfRangeException: InvalidArgument=Value of '2' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex but thats because i only have one serial port. so i changed selectedindex to 0 PS boeing...nice!
|
|
|
|
« Last Edit: November 13, 2011, 11:04:05 am by sirbow2 »
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35472
Seattle, WA USA
|
 |
« Reply #11 on: November 13, 2011, 11:18:13 am » |
but thats because i only have one serial port. so i changed selectedindex to 0 When I plug my Arduino in, it is always the 3rd or 4th port in the list. So, I default to there. If yours is first in the list, then you made the correct change. PS boeing...nice! Yep. 32 years last week.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #12 on: November 13, 2011, 11:28:01 am » |
now back to C# errors  ok so i get an index out of range exception when i run the program(your program modified; no clr, demo buttons and my graph/string splitting stuff added). it occurs right after it receives the first line of data from Arduino(so when its splitting the string?). i dunno why this happens, it works fine(the string splitting) in the old program; i just copied to your new one. i emailed you the code; post was too long...
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 311
Posts: 35472
Seattle, WA USA
|
 |
« Reply #13 on: November 13, 2011, 11:34:12 am » |
I can't see what you are receiving. If it looks like this - "535,502,1023,1,1,1,1" - that's 7 values that you are trying to store in a 6 element array. That will generate an index out of range exception.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Sr. Member
Karma: 1
Posts: 474
|
 |
« Reply #14 on: November 13, 2011, 11:42:02 am » |
maybe i dont understand arrays still  oops for some reason that version of the code had a 6 instead of a 7: int[] JoyData = new int[7]; and this is what i get as output: 548,503,774,1,1,1,1 X: 548 Y: 503 S: 774 using: public void Read() { while (port.IsOpen) { try { string message = port.ReadLine(); this.SetText(message); //Handle data recieved; something like: 1023,1023,1023,1,1,1,1, string[] split = message.Split(new Char[] {','}); int cntwrite=0; foreach (string s in split) { if (s.Trim() != "") { int c = Int32.Parse(s); JoyData[cntwrite]=c; //adds each new value split(s) from SerRead string to JoyData array ++cntwrite; if(cntwrite >6) //reset cntwrite for next time serial data is recieved { cntwrite=0; } } } this.SetText("X:"); this.SetText(JoyData[0].ToString()+Environment.NewLine); this.SetText("Y:"); this.SetText(JoyData[1].ToString()+Environment.NewLine); this.SetText("S:"); this.SetText(JoyData[2].ToString()+Environment.NewLine); this.SetText(Environment.NewLine); } catch (TimeoutException) { } } } which is good but my graph still doesnt work...hmmm.... EDIT: ok i have an idea on why the graph isnt working. i put some debugging code that prints x and y in the public form. it says 0 for x and y, and only once when the program is first ran. how do i make the graph update every time serial is read?
|
|
|
|
« Last Edit: November 13, 2011, 11:48:51 am by sirbow2 »
|
Logged
|
|
|
|
|
|