[Free][Windows only] UE4Duino 2 - Arduino (COM port) communication

@knotFF Don’t use PrintLine. Just Print. On the example code I made, it checks if the received string is exactly equal to “lines” or “float” etc. So, if you PrintLine, you are sending "
" or alikes with it, causing it to fail, as "lines
" is not “lines”.

It’s kinda funny because before you used Print with “float” trying to get lines. Now, you are using the correct words but sending them wrong xD

Hi, @joeycampbell
Without seeing code or Blueprints, I can only assume what’s going on. But it’s a very common problem with new Arduino/Unreal users: synchronization. Arduino speaks in a speed, Unreal reads in another. You could send “123456” and Unreal could read it correctly or read one part in a frame and the rest in another, because it’s reading before Arduino finishes sending all the characters.

You have two ways of sending a float to Unreal. Either as characters (text), as you seem to be doing, or as the 4 bytes that make a float. The first way has the synchronization problem described above. The second way could also suffer from this problem if Unreal read the 4 bytes separately. So, how to solve it? There are several ways:

  • If you know how many characters you are going to send, you could always wait until Unreal reads all of them. But this can freeze your game, even if very briefly.
  • You can send something to Arduino and make it answer to that with your data. This way, Unreal knows when to expect data. Then you can use a small delay to make sure there’s time for Arduino to send all the characters. Arduino is lightning fast, but the COM port is not. This is how in did in the plugin’s examples.
  • Harder, but the most precise way: create a protocol. Send a sequence of characters (2 or 3) before each message and another sequence at the end. Then, in Unreal, you keep checking for this sequence (even between separate frames). If found, store all coming bytes/chars/strings/whatnot in an array, until it recognizes the END sequence. After the end sequence, you know for sure the message is complete.

I always use the last method. I’ll describe an example of how I do it in Unreal to read floats or other kind of data that I can store in byte arrays.

  • First, I define the size of my “package” in Unreal and Arduino. For instance, let’s say I’ll be sending 2 floats (4 bytes each) and 2 bytes for the start and 2 more for the end of the protocol. 4 + 4 + 2 + 2 = 12 bytes
  • I define in Unreal and Arduino these start/end bytes, so both know what to expect.
  • In Unreal, I keep reading the bytes in the Serial port. If I find the first one, I register that I’ve found it and start looking for the second one.
  • If the next one is the second one, I know the next 8 bytes are my two floats and then comes my protocol end. If the next one is not the second one in the protocol, I know I’m reading some random piece of data and I start looking for the first protocol byte again.

@
Sorry for the mismatched screenshots, but I have tried them all. Print/PrintLine, ReadByte/ReadBytes, etc., all I/O methods. Nothing works for me.
In fact I wrote all available methods in a blueprint, to test all Input/Outputs from Arduino<>Unreal, like so:

I do believe I have found the possible issue, but I need to confirme it.
I’ve exported this build with Baud 9600 for testing on (it will pick the correct COM via loop from 1-50) :
https://www.dropbox.com/s/tkd23mfx9a…duino.zip?dl=0

This EXE works in 2 ways:

  1. for “ArduinoTypeExample”:

  2. This is a copy of the exemples presented in page one of this forum

  3. Pressing keyboard F = lines

  4. Pressing keyboard G = float

  5. Pressing keyboard H = int

  6. Reading Only, you need to create a scritp in Arduino IDE to print something to serial. (or just remove the IF statements from the exemple above):

  7. Pressing keyboard** T = this will go through all available READs available in UE4Arduino’s plugin**.

Could anyone try this please?
I does not work on my machine (macbook under bootcamp), but it works on my friends machine (native Windows machine). So I think the problem I am having is because of an Apple<>Driver<>Windows for the USB ports…

Thanks** **

Has anyone tried to read two sensor readings simultaneously from arduino ?
I have tried separating the sensor readings by a comma (in arduino) and then using the split string function to decipher the two readings.
I can print both readings on screen but the outputs from the split node are not working.
I have used the attached blueprint as a reference - any ideas what I’m doing wrong?

I can’t test it, but you are very likely correct.

What are you splitting and trying to cast to Int?

Anyway, I strongly suggest you walk away from String for this.

Hi I’m trying to communicate with arduino from within a pawn blueprint.
It seems to work based on the print strings I am using to troubleshoot where its going wrong…no errors in the blueprint but no signal happening in the arduino board.
I have managed to get arduino working before in a non level blueprint (I used it before in the third person character blueprint) but in this pawn blueprint I am having no luck.
Does something need to be changed in the pawn blueprint settings in order for it to send data to the arduino ?

From what you explained, it seems that your success messages are being printed, but how do you know there’s no signal on arduino? Are you controlling something and it’s not being controlled correctly?

Hi - yes I am using arduino to control a servo motor. The same arduino code has worked for me when I trigger the servo from the level blueprint or third person character blueprint but no luck using a pawn blueprint…no movement happens at all on the servo.

Hi - I couldn’t fix the above issue so I brought my code back to the level blueprint instead of trying to interact with arduino via the pawn.
I was wondering if anyone knows of a work around for trying to read from the arduino and write to it also from within Unreal.
I am using the following code to enable the control a servo motor from within Unreal. The UE4Duino Print node sends an updated variable to the arduino serial and this value dictates the servo rotation. This all works fine but I would also like the ability to read from a sensor and bring this value into arduino.
Unfortunately when I try to do this the sensor values conflict with the servo variable being sent from unreal.
Is there a way to have two serial windows ? Or should I put a unique identity tag on my data being sent from Unreal so that the serial can decipher between sensor data and Unreal servo variables ?

*#include <Servo.h>
Servo servo1;
long num;

void setup()
{
servo1.attach(2);
Serial.setTimeout(50);
Serial.begin(9600);
}

void loop()
{
while(Serial.available()>0)
{
num= Serial.parseInt();
}
servo1.write(num);
delay(15);
}*

From further searching it seems as though this is a common issue wit arduino. A work around is done using SerialCallResponse or Hand Shaking.
I’ve found an example done in processing - if anyone has an Unreal workaround please let me know:
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/shaking-hands-part-2

4.19 binaries released! (click here)](https://github.com//UE4Duino/releases/tag/v2.2.1)

@_joey Hand Shaking and SerialCallResponse are protocols. They are surely helpful. I’ve done something like that before to keep several ints in sync between Unreal and Arduino. If you’re willing to pay for some help, I do consulting through Skype. My nick is r_skull

Hi,
Thanks for this plugin but I have an issue. It works perfectly in editor mode but when I package the game, it doesn’t work. The port is avalaible, there’s nothing connected except the arduino so I can’t figure it why it doesn’t work. Maybe you have some clue.

Are you using it inside an ActorComponent? I’ve had issues with Unreal not compiling plugins when I only use them in ActorComponents. Try referencing any of the plugin methods in the level blueprint or an Actor blueprint

Hi
I’m trying to build my project and i get error CS0101 The namespace ‘<global namespace>’ already contains a definition for 'UE4Duino"
The problematic file it states is UE4Duino.Build.cs(3,14)
Everything works perfectly in engine

What do i do?

@anonymous_user_5d80843e Usually, when UE4 insists on that error, it’s because of previously cached compiled files, with an existing somewhat different copy of the same plugin.

Delete project’s *Intermediate, Saved *and *Binaries *folders, right click the project’s .uproject file and choose Generate Visual Studio Project Files. Then see if it compiles. And also delete Intermediate and Binaries folders inside the UE4Duino plugin folder, before generating Visual Studio Project Files.

I tried but I’ve got the same problem. I tried with UE 18 and UE 16 but still. I’ve got “serial is not opened” even if it works perfectly in editor mode. I started with a blank project, I just had the plugin.

Open your project’s .uproject file in a text editor and let me see the plugins part of it, please.

Thanks, it works now. I followed your instructions on post #37. Thanks again for this great plugin.