-1

On clicking an OK button I have a string of values, namely:

88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0

I am going to be sending these to an Arduino via serial, the problem is the format they are in at the moment.

The Arduino is expecting them in the same format (data type) as one would send from the serial terminal window and without a newline character added. I am currently using Serial.parseint() in my code on the Arduino to receive the values separated by commas and load them into variables. (Which currently is working when I type the following into a serial terminal window: 88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0)

I could probably do this:

string mystr = "88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0";
int[] nums = Array.ConvertAll(s.Split(','), int.Parse);

But the I have to take them back to the same format.

How to I set/change the value of mystr to what the Arduino needs?

Brian
  • 14,610
  • 7
  • 35
  • 43
user3407537
  • 51
  • 1
  • 13
  • 2
    Please add more information; it's hard to tell *exactly* what you are asking here. Please also add relevant code and errors to allow us to better assist you. – Der Kommissar May 18 '15 at 18:13
  • 2
    `string.Join(",", nums)` perhaps? – Jon Skeet May 18 '15 at 18:15
  • If you need to take them back to the same format, then: http://stackoverflow.com/questions/4756565/c-sharp-convert-array-of-integers-to-comma-separated-string – Jaunius Eitmantis May 18 '15 at 18:18
  • When you type in a terminal window you are sending text (string, not int). The question is unclear. Try to be clear about (find out) what the Arduino is actually expecting. – H H May 18 '15 at 18:19
  • Probably this article could help you [link](http://playground.arduino.cc/Csharp/SerialCommsCSharp) – Victor May 18 '15 at 18:20
  • WOW thank all of you for the answers. Perhaps I have confused myself When I use my c# program to send serial to the arduino it doesn't seem to receive it but it does when I send the string above via the serial terminal. The string 'mystr' which I collect from two datetime picker boxes and a few checkboxes, On the arduino I receive the entire string and then parse it using serial.parseint() which separates each comma separated value into ints. So based on what I have asked it seems I already have the data in the correct format ..namely string ? Can someone please confirm this ? – user3407537 May 18 '15 at 19:51
  • @ Victor ..thank you I have seen that and have been using it as a guide and reference but it uses and array. – user3407537 May 18 '15 at 19:55
  • @ Henk the Arduino is expecting the string as I have listed it here without a CR or NEW LINE character or any other line end : 88,2015,5,17,22,6,53,2015,05,17,22,06,53,0,0,0,0,0,0,0,0 , So can you confirm the serial terminal sending the same string format as I would get from textboxes and datetime picker in my string variabel 'mystr' ? If so then I have messed up is my serial send from C# side ? – user3407537 May 18 '15 at 19:57
  • It really is terribly unclear what you are asking. Your subject says you want an `int[]` but then you say you need them in the same format as a `string[]`. – ProfK Dec 22 '16 at 14:07

2 Answers2

0

I've did some conversion functions for a friend some time ago, and He was also using arduino, I'm not sure it is exactly what you need, but maybe it can help http://blogs.dotnetwork.it/sabrina/en/blog/c-conversioni-dati-che-passione/

The post is originally written in italian, but on the top of the page you can find a combobox to set the translation to english I've checked it and I think is comprehensible.

Sabrina_cs
  • 421
  • 3
  • 18
  • @ Sabrina_cs ..Thank you kindly for the response link but not quite what I'm after. – user3407537 May 18 '15 at 20:06
  • Ok then, re reading your question above what I found not clear is what you mean have them back in the same format, you have to convert the strings to number and then transmit the numbers as a string? and if the answer is Yes, in what format do you have to put the numbers on the string, is it decimal, hexadecimal or what? – Sabrina_cs May 19 '15 at 19:12
0

Not sure I understand, but if you mean you want to convert them back to a comma-separated string, you could do something like this:

string nums = string.Join(",", Array.ConvertAll(s.Split(','), int.Parse));
Rufus L
  • 36,127
  • 5
  • 30
  • 43