sim.serialOpen returns -1

Typically: "How do I... ", "How can I... " questions
Post Reply
yFleury
Posts: 4
Joined: 19 Jul 2024, 19:09

sim.serialOpen returns -1

Post 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?

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: sim.serialOpen returns -1

Post 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

yFleury
Posts: 4
Joined: 19 Jul 2024, 19:09

Re: sim.serialOpen returns -1

Post by yFleury »

Yes, I am running on Windows.

coppelia
Site Admin
Posts: 10504
Joined: 14 Dec 2012, 00:25

Re: sim.serialOpen returns -1

Post by coppelia »

Are you running in headless mode?

Tinayou
Posts: 3
Joined: 31 Jul 2024, 15:54

Re: sim.serialOpen returns -1

Post 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.

Tinayou
Posts: 3
Joined: 31 Jul 2024, 15:54

Re: sim.serialOpen returns -1

Post 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.

yFleury
Posts: 4
Joined: 19 Jul 2024, 19:09

Re: sim.serialOpen returns -1

Post 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?

Tinayou
Posts: 3
Joined: 31 Jul 2024, 15:54

Re: sim.serialOpen returns -1

Post 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.

yFleury
Posts: 4
Joined: 19 Jul 2024, 19:09

Re: sim.serialOpen returns -1

Post 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.

Post Reply