Change COM Port and reconnect
DarioScocco opened this issue · comments
Hi,
I am using Ardity in an Arduino project. It works great. But sometimes when I connect the USB the Arduino COM port changes. So I have to change the COM form the editor. Is there a way to change the COM from the application itself using code and re-connect?
Thenk you!
Hello,
To do what you ask, I can think of a bit of a hack: you could call Destroy
with the SerialController
(not the GameObject
but the MonoBehaviour
). Then attach a new SerialController
to the same GameObject
using AddComponent
. You can configure the newly created component before your function ends, and those values will be respected by the initialization code of the SerialController
.
But, have you tried these options?
- Arduinos come with a "reset" button, have you tried pressing it instead of physically reconnecting it? Maybe it doesn't change the COM number in this way.
- Have you inquired into why the COM port used is different each time? Usually, reconnecting a device on the same USB port gives the same COM port. Anyway, have you gone to the "Device Manager" > Find your device > "Properties" and assign a fixed COM port there?
Regards.
Hello,
Is your issue solved?
I dont know if it's too late, but I've solved the problem creating a public methon inside the SerialController class:
public void ChangeCOM(string COM) {
OnDisable();
portName = COM;
OnEnable();
}
Maybe it's not the best way to solve the issue, but it works for me.
Hey guys, @tlondero 's solution is working, but @dwilches 's solution of creating the component from code doesn't. This is what I'm trying to do:
_serialController = gameObject.AddComponent<SerialController>();
_serialController.baudRate = 115200;
_serialController.portName = "COM7";
-> and anyway it will try to open COM3.
I think eventually there should be a proper way to create the SerialController class from code, because in my case for example I'm setting the COM port with a config file. For now, @tlondero 's solution is working but it's a bit hacky.