[Closed] Read Serial Port Arduino
I send actual values but maxscript puts them in quotation marks ie: “161,200”
If I use the filterstring I get
“161”
“200”
I need to transform that into integers
so… the code that i gave you above has to do it
you can change (execute data[n]) to (data[n] as integer) if you want, but it’s almost the same.
and when you will use the values in eulerangles the system converts them to floats anyway.
DanGrover thank you very much! Your code splits the string but the values are in quotation marks and they can’t be converted to floats. It does the same as
data = filterstring (port.ReadLine()) “,”
denisT you still have to get rid of the quotation marks, you can’t convert directly “200” to 200
I changed to “”,” but the values ares still in quotation marks. I’ve found someone who had the same problem with the quoation marks. He solved the problem with this code but it only works for one value, I need to put the data = filterstring (port.ReadLine()) “,” in there but I don’t know how to do it.
port.Open()
x=1
while x>0 do
(
rotValue = port.ReadLine() as string
finalValue = 0
s2 = "0"
for i=1 to rotValue.count-1 do
(
s = substring rotValue (i) (1)
append s2 s
)
finalValue = s2 as float
print finalValue
completeRedraw()
if finalValue == 0.0 then
(
x=0
)
)
Again, this won’t be the most direct way, but if you have a string and you want to get rid of certain characters, one sure fire way of doing it is to convert the string to a StringStream and use ReadDelimitedString again, but for the character you wish to remove. If you use this in the context of “while do” loop. For example, the following…
hello = "CG_Talk" as stringstream
while (eof hello) == false do
(
print (readdelimitedstring hello "_")
)
will print “CG” then “Talk”. So, if instead, you do the following…
new_hello = ""
hello = "CG_Talk" as stringstream
while (eof hello) == false do
(
new_hello = new_hello + (readdelimitedstring hello "_")
)
print new_hello
You’ll find that it now prints “CGTalk”. You could always do the same with “‘s (though, as Denis point out above, in MaxScript a quotation mark is referred to as the following:
“
This is because it needs a way of knowing you mean “quotation mark” rather than it being a string declaration.
Edit: “eof” stands for End Of File. So “while eof stringstream == false do” just means “until you get to the end of the string, do the following”. Without that, you’ll get infinite “” returned.
I wish I could understand what you just posted lol
This is my code:
port = dotNetObject "System.IO.Ports.SerialPort" "COM3" 38400
port.Open()
x = 1
while x > 0 do (
data = filterstring (port.ReadLine()) ","
print data[1]
print data[2]
completeRedraw()
)
port.Close()
it prints
“200”
“450”
and I need it to print
200
450
Where in the code should I put yours?
Well it’s worth bearing in mind that prints are ALWAYS encapsulated in quotation marks, because prints are all strings. If it has only one set of quotation marks, then in theory it should just be a number. It’s odd that it won’t let you convert that to a float or integer. Will it let you JUST convert it to a float or integer, without it then being assigned to the rotation.
And my example above was simply to give an idea of how you can go about pulling specific characters out of a string. The logic is basically to run through the string, adding together all the bits between the given character – in my example the given character was “_”, but you can just as easily make it “”” to remove any quotation marks – but as I said above, I don’t think that’s actually the problem, since it already appears to be a number. As an example, try putting this into your maxscript listener:
print (45 as string)
You’ll notice it prints as “45”, not 45.
print (45 as integer) gives you 45
I have to have an integer or a float,basically a number
i really don’t understand your problem. you have everything to make it works. if you can’t convert a string to integer probably the issue is over your head.
learn max script.
If i knew maxscript i wouldnt be here! I did
Var = data[1] as integer
And it worked but for data[2] it didnt. When i print data1 and data2 i get
“200”
“300
“
Instead of
“200”
“300”
Maybe its because of that,dont know…
I think your best bet here is to post your Arduino script so that a fellow Arduino geek (i.e Me) can look at it.
That second string in the array is failing to convert to an integer because it has a line-end character at the end – see how its the quotemark is on the next line.
A simple way to fix that would be to add the /n (and probably /r) linebreak character to filterstring’s delimiter-argument:
data = for item in (filterstring (port.ReadLine()) ",
\r") collect (item as integer)
That’ll return an array of integers (unless you feed it any other characaters)
It sounds like data2 has a new line at the end of it – a carriage return, enter button etc. You’ll need to trim that off. If you can trim it from the data being passed, that’d be best. If not, and you know it’ll always be the last line of the second bit of data, you could simply do…
X = ""
For a in 1 to (data[2].count - 1) do x = (x + data[a])
Print data[1]
Print x7
This goes through the string data2 and adds all the characters to a new variable. But the loop goes through that by 1 less than the total character count, thus getting rid of the new line character.
Not the most efficient way but it’s the easiest to understand I think.
You could also use substitutestring to get rid of the new line.
var = (substitutestring data[2] "
" "") as integer