Page 1 of 1

sim.serialOpen returns -1

Posted: 19 Jul 2024, 19:23
by yFleury
My goal is to use serial communication between CoppeliaSim and Arduino to manipulate simulation elements through sensors read by the Arduino.

Code: Select all

port = "\\\\.\\COM4"
function sysCall_init()
    sim = require('sim')
    serialHandle = sim.serialOpen(port, 9600)
    print(serialHandle)
    pivot = sim.getObject('/Pivot')
    motor = sim.getObject('/Motor')
    
end

function sysCall_actuation()
    pivotPosition = sim.getJointPosition(pivot)
    sim.serialSend(serialHandle, tostring(pivotPosition))
    target = sim.serialRead(serialHandle, 32, '\n'); 
    sim.setJointTargetForce(motor, -target, true)
    
end
My handling of the serial data may be incorrect, however my issue lies with the opening of the serial communication, the handle returned by sim.serialOpen is -1 no matter what I do. COM4 is the port that the Arduino IDE is using successfully to upload sketches to the board.

I based my code from the one presented on the following video
https://youtu.be/ZJQV2FlGFBo

How can I get this to work?

Re: sim.serialOpen returns -1

Posted: 25 Jul 2024, 06:04
by coppelia
Hello,

this looks ok if you are running on Windows. On Ubuntu, you'd use a different string, e.g. instead of:

Code: Select all

port = "\\\\.\\COM1"
you'd do something like:

Code: Select all

port = "/dev/ttyS0"
Cheers

Re: sim.serialOpen returns -1

Posted: 29 Jul 2024, 19:31
by yFleury
Yes, I am running on Windows.

Re: sim.serialOpen returns -1

Posted: 31 Jul 2024, 09:46
by coppelia
Are you running in headless mode?

Re: sim.serialOpen returns -1

Posted: 31 Jul 2024, 15:57
by Tinayou
yFleury wrote: 19 Jul 2024, 19:23 My goal is to use serial communication between CoppeliaSim and Arduino to manipulate simulation elements through sensors read by the Arduino.

Code: Select all

port = "\\\\.\\COM4"
function sysCall_init()
    sim = require('sim')
    serialHandle = sim.serialOpen(port, 9600)
    print(serialHandle)
    pivot = sim.getObject('/Pivot')
    motor = sim.getObject('/Motor')
    
end

function sysCall_actuation()
    pivotPosition = sim.getJointPosition(pivot)
    sim.serialSend(serialHandle, tostring(pivotPosition))
    target = sim.serialRead(serialHandle, 32, '\n'); 
    sim.setJointTargetForce(motor, -target, true)
    
end
My handling of the serial data may be incorrect, however my issue lies with the opening of the serial communication, the handle returned by sim.serialOpen is -1 no matter what I do. COM4 is the port that the Arduino IDE is using successfully to upload sketches to the board.

I based my code from the one presented on the following video
https://youtu.be/ZJQV2FlGFBo

How can I get this to work?
Hey, have you resolved this problem? I got the same bug. Thank you.

Re: sim.serialOpen returns -1

Posted: 01 Aug 2024, 10:09
by Tinayou
I found my solution luckily. The problem for me is that I ran the Coppelia Sim script with the Arduino IDE serial monitor opening. So the port was detected as "is using". Please make sure that the port you selected in Coppelia Sim is not using in other software.

Re: sim.serialOpen returns -1

Posted: 01 Aug 2024, 20:34
by yFleury
Tinayou wrote: 01 Aug 2024, 10:09 I found my solution luckily. The problem for me is that I ran the Coppelia Sim script with the Arduino IDE serial monitor opening. So the port was detected as "is using". Please make sure that the port you selected in Coppelia Sim is not using in other software.
That was my guess, but then how do I upload the script to the arduino? Ulpload, close the IDE and run Coppelia?

Re: sim.serialOpen returns -1

Posted: 02 Aug 2024, 09:48
by Tinayou
I only use the Arduino transmits data from outside source to the PC port, and the script inside CoppeliaSim read and process data. So after I download the Arduino code into the chip, the Arduino IDE only is used to monitor the data to the port. Closing it would not bring any affect to the system. Maybe our application scenarios are not exactly the same. Hope it would help you.

Re: sim.serialOpen returns -1

Posted: 08 Aug 2024, 23:08
by yFleury
Posting here so the solution can be found:

Close the serial monitor on the Arduino IDE

As long as its open Coppelia won't comunicate via serial.
If you need to see what serial data is being passed in between the microcontroler and the simulation like I do, use something like Serial Port Monitor.