Help to Read / Write from/to serial/USB/COM port for Arduino control.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kio

OK…
First… I am not a seasoned programmer, I am a hack!

I can get port write() communications working from OS.excecute… cmd.exe to powershell… and a crazy amount of command line code that creates > opens > commands > then closes the port… this WAY TOO SLOW of a process need to keep port open accessible and ready.

Example function works (zzz slowly) with code programmed on arduino:

func _changeArduinoLEDColor(RVal,GVal,BVal):

var RGBstringtoPass = "<" + RVal + "," + GVal + "," + BVal + ">"

var output = []

var arduinoCommand = "$port= new-Object System.IO.Ports.SerialPort COM5,9600,None,8,one;$port.open();$port.WriteLine('" + RGBstringtoPass + "');$port.Close()"
var pid = OS.execute('CMD.exe', ['/C', 'powershell.exe', arduinoCommand], false, output)

After smashing my head against a wall for days… I stumbled onto this…

https://github.com/Superwaitsum/GDSercomm
https://github.com/Superwaitsum/GDSerialAsset

Started to follow all the instructions of compiling Godot from source… I do not need to do this, I have no intend to learn any of the C’s, modify or create my own modules etc… GDScript suits my needs just fine… BUT… is there any quick way to get Superwaitsum’s serial modules working in a Godot version installed from exe?

There is no real direction on GIT and the Godot documentation is sending me in circles with no end in sight.

Any help or insight would be appreciated.

This is a really difficult question. I would suggest you post on github and ask the users of those specific libraries.

Todilo | 2018-09-27 10:19

Just hoping to find another solution. If Godot could simply send a command prompt style ECHO > PORT all would be the simple, at least for sending data. But have yet to find a way for that to work without creating a hack third and fourth party SW link.

Kio | 2018-09-27 23:23

Since it’s a long time this question was asked, I think that an update is welcome.
I’m using Godot 3.3.2 and my conclusion about dealing with serial communication is : use C#

You’ll keep all the cross platform advantage of Godot without writing a single low level line of code, here is a snipped:

using System.IO.Ports;

Then in your class if you want to open the port (assuming “SerialPort port” is defined)

port = new SerialPort(portName, (Int32)baudRate, Parity.None, 8, StopBits.One);

Then use the _Process to read the data (snipped for reading lines):

	public override void _Process(float delta)
{
	Int32 idx = 0;
	if (port != null)
	{
		if (port.BytesToRead > 0)
		{
			string serData = port.ReadExisting();
			string[] str = serData.Split('\n');
			parseLines(str);
		}
	}
}

Don’t use “DataReceived” event, it doesn’t work (I think it’s a question of code context). But anyway with Godot this function is useless since you can check and parse directly in the _Process function.
Also, for efficiency it’s better to use the ReadExisting() function than the Readline() function.

papyDoctor | 2021-07-19 16:41

:bust_in_silhouette: Reply From: Superwaitsum

Hello, I’m the developer of GDSercomm.
There is no need to compile from source if you are using windows.
You can use GDSerialAsset directly since it is a godot project that comes with precompiled libraries. It is just a demo that replicates the Arduino IDE’s Serial monitor.

First let me send my full appreciation for your quick response.

I had dug into learning about the GDNative and Plugins compiling from source etc. learning a lot. Had attempted utilizing the GDSerialAsset demo, had some issues with the compiled master build v3.1… (sort of expected), and when launched with Godot v3.0.5 install from office PC had errors on launch. Just tried on my laptop using v3.0.5 and the demo launched! Awesome! Will be trying to determine the problem on my desktop PC and attach Arduino to it for testing. Will update once I make effort to incorporate into an existing project. Thank you for this utility, I look forward to experimenting.

Kio | 2018-09-28 00:12

Haven’t tested it with master for a month, but I’m glad to hear it worked for you at the end. Just one thing, please use 3.0.6. Since there was a bug in official godot builds (3.0.3-3.0.5) that caused crashes.
Extra details: GDNative error on Godot official builds only · Issue #20559 · godotengine/godot · GitHub
Good luck with the experimenting!

Superwaitsum | 2018-09-28 01:39

Update fyi:
Attempted to open the demo project in v3.0.6
Tried both 64 and 32 bit versions and get following error

> ERROR: Can't open dynamic library:
> D:/GDSerialAsset-master/bin/win32/GDSercomm.dll. Error: Error 126: The
> specified module could not be found.
> 
>    At: platform/windows/os_windows.cpp:1700 ERROR: get_symbol: No
> valid library handle, can't get symbol from GDNative object    At:
> modules/gdnative/gdnative.cpp:317 ERROR: init_library: No
> nativescript_init in "res://bin/win32/GDSercomm.dll" found    At:
> modules/gdnative/nativescript/nativescript.cpp:1054 erasing
> C:\Users\Admin\AppData\Roaming/Godot/projects/GDSerialAsset-master-c3de606ae9afe454ee8fbc14cd88bf35/filesystem_update3
> ERROR: terminate: No valid library handle, can't terminate GDNative
> object    At: modules/gdnative/gdnative.cpp:223

Also did a quick export from 3.0.5 64bit… app runs but debug console has similar errors… with the following line repeating for infinity…

> SCRIPT ERROR: _physics_process: Invalid call. Nonexistent function
> 'get_available' in base 'Nil'.
>           At: res://Example/SimpleConsole.gdc:23

Kio | 2018-09-28 14:32

Can’t reproduce 3.0.6 error, may be happening due to using VS2017 to compile it. I will use VS2015 compiler for next revision, maybe that will help.
The export bug is stated on the github readme, you have to copy sercomm.dll (of the corresponding version) to the same folder as the export executable.

Superwaitsum | 2018-09-28 15:29

update:

Definitely a digital ghost messing with me. Trying to trouble shoot. so far.
Been accessing the demo project from 3 different PC’s. 1 out of three does not give errors at launch. Without overloading post with machine specs just wanted to say…
My laptop (not my production machine) seems to open and edit fine in Godot 3.0.6 as long as I do not open the same project on another PC. If I do and open again on original laptop I get the same errors. I installed the same versions of Godot on each machine, and attempted re-downloading the demo on each machine with same results.

I re-download the zip and also cloned from git. It worked again on laptop. Was able to export for windows and open exe successfully without error on laptop (the correct sercomm.dll was exported and placed with the exe automatically. )

I transfered the file via USB drive to another PC (my arduino station) got the same errors as posted previously.

I have tried other solutions (got them to work in a hack way, and not what I would use other than for testing) I will be following your git closely for updates as this is not the forum for indepth “tech-support”. That being said if you are interested in providing a more formal $upport option, I would be open to that.

I am trying to get godot and arduino working together for ideas I have creating custom PC interfaces and applications for my daughter who is legally blind (can see to some undetermined degree) and non-verbal. I am also working on some educational ideas that would be enhanced with a more interactive and tactile experience.

again thanks for all of your time and effort
Kiotenhariyo

Kio | 2018-09-29 17:24

I will work on the plugin this week. (I also have to fix a bug and try to implement a feature request).
Hearing good uses of serial comunication encourages me to continue with the development of the plugin, thanks and best wishes.
As you said, this thread is getting a bit long, you can contact me at my email: superwaitsum@gmail.com
(will later think of using gitter or a similar chatting tool for the development)

Superwaitsum | 2018-10-01 02:50

Hi, I want to use the code for sending command to the board and get board’s data from port. but I couldn’t find how I should use the code?

nosa | 2021-01-03 22:04

:bust_in_silhouette: Reply From: sustainablelab

I haven’t tried GDSercomm or GDSerialAsset, but I found another solution that works really well for me. I’m working on a similar problem: I have an embedded system that sends sensor data over USB and I want to interact with the data in real-time on my desktop.

Some details on the results: each iteration of the Godot _process() loop requests 784 bytes of data from the embedded system and “plots it” by creating a Line2D. Godot has no problem keeping up with the data – the display on my desktop responds instantly to my physical interactions with the sensor in the embedded system.

My solution uses these two tools:

  • PythonScript – a Godot “addon” available from the Asset Library
  • Python package pyserial for USB communication

Details about PythonScript are here: GitHub - touilleMan/godot-python: Python support for Godot 🐍🐍🐍.

I’m happy to provide more detail on using either of these tools if this solutions sounds helpful to you. I just started using Godot and I’m completely addicted.

About PythonScript:

  • it makes USB communication easy because I can use the pyserial package in Godot, but any Python package can be installed in the project
  • it is under active development with lots of contributors
  • as of this writing, I’ve run into one bug, but it’s a known issue and it’s something I can workaround
  • installation is simple
- downloading via the AssetLib takes a long time and appears to hang, just be patient
  • there is even an option to add a Python REPL directly to the Godot Editor!

The one caveat is that exporting is complicated. It involves creating a .zip and manually copying in some of the files. I’ll edit this post when I figure out the steps to a successful export.