[Closed] Read Serial Port Arduino
Hi, I’m trying to read the serial port in maxscript and apply a rotation to an object. I’m sending the values from the arduino separated by commas that I need to split into integers in maxscript. I’ve searched and found this code but it’s saying “unable to convert: undefined to type float”. Hope you guys can help!
port = dotNetObject “System.IO.Ports.SerialPort” “COM5” 115200
port.Open()
x = 1
while x > 0 do (
rotX = port.ReadLine() as Integer
rotY = port.ReadLine() as Integer
rot_obj = eulerangles rotX rotY 0
$Box01.rotation = rot_obj
completeRedraw()
)
port.Close()
Is the information being read correctly from the port? For example, if you print the values of rotX and rotY rather than try and apply them as rotations, what does it read? I’m guessing it’s the variable assignment that is causing the error (given your error message) so I don’t think the problem is with the maxscript per se, but rather than incorrect dotnet reading from the input.
When I print rotX the scrip runs and I get undefined values. I think I have to do port.Readline() as string and then separate the values from the commas, I just don’t know how to do that!
Try ReadDelimitedString(). It allows you to define the character that is read up to (ie you can use ‘,’ to separate values). So, as you say, you might have to read the string, then churn THAT for data, and apply that data to the rotation controller.
denisT I’m sending
x,y
x,y
x,y
but I can use whatever works!
DanGrover thank you for your sugestion but I’m a noob at maxscript, I don’t know how to implement that…
If you know the format of the data you’re getting in advance – and it sounds like you do – it’ll be very simple. Something like…
input_data = port.ReadLine() as String
while input_data != undefined do
(
input_X = (readDelimitedString input_data ",")
input_Y = (readDelimitedString input_data ",")
)
rot_obj = eulerangles (input_X as Integer) (input_Y as Integer) 0
$Box01.rotation = rot_obj
I can’t test right now, but this may error if it doesn’t find a second “,” after the Y rotation data. This can be fixed relatively easily by using another read statement, or else you could just reformat the ingoing information for the benefit of my laziness to have it formated “x,y,” rather than “x,y”.
Edit: Tweaked the code to make the inputs integers.
Thank you very much for your help. I tried it with and withou the comma after the y value but it didn’t work, I get this error ’ no ReadDelimitedString for “161,” ’
EDIT: Instead of applying a rotation I printed the values and they are undefined
Oops, my mistake – you can’t run readDelimitedString on strings (ironically), you have to run it on StringStreams. So first, you’ll need to convert the string to a stringstream:
input_data = port.ReadLine() as StringStream
if input_data != undefined do
(
input_X = (readDelimitedString input_data ",")
print ("X: " + input_X)
input_Y = (readDelimitedString input_data ",")
print ("Y: " + input_Y)
)
Xrot = input_X as float
YRot = input_Y as float
rot_obj_2 = eulerangles XRot YRot 0
$Box01.rotation = rot_obj_2
I just did a little test in Max and this worked in giving me the correct result (when I substituted “port.ReadLine()” for a literal string of “161,201”). That has far, far more lines than it needs, I just wanted to explain (via prints) what was going on.
in this case the base read cycle has to be:
data = filterstring (port.ReadLine()) ","
<node>.rotation = eulerangles (execute data[1]) (execute data[2]) 0
where <node> is a node has data to apply to…
BTW: do you really ignore Z rotation?
denisT your code is working in spliting the values but when I print them I get “value” instead of an integer so I can’t use them as they are undefined
if you print port.ReadLine() in cycle what do you get?
while <something> do format ">> %
" (port.ReadLine())
I don’t understand your question, like this?
port = dotNetObject "System.IO.Ports.SerialPort" "COM3" 38400
port.Open()
x = 1
while x > 0 do (
value = port.ReadLine() as String
print value
completeRedraw()
)
port.Close()
If thats what you’ve asked I get “value.x, value.y”
I don’t know what that code does
that explains everything… you don’t send actual values… you send the values names probably.
so you have to fix the sending part first