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

Hi,

Are you planning a 4.21 release for the plugin?

What do you mean by start? Arduino starts by plugging it to any energy source, like the USB port. Do you mean send a command to it for it to start something? If thatā€™s it, use the Serial classā€™ Write*** nodes. Check the OP for example images.

I donā€™t rush to it when a new engine version comes along because the source is on GitHub, so anyone can compile it. But there you go, buddy.

Thanks very much, appreciate it.

Hi

How do I send Hex values via this plugin?

Iā€™m trying to use this to communicate with a FTDI device (USB device that converts into a COM port) It is a speed and heart beat reader for a thread mill.
Windows is connecting to it via COM3. 57600 baud. In Unreal I can connect to the device with this plugin, so everything seems fine.

The protocol for this device tells me to send 0x44 to start the device, then 0x36 to set the output sample rate, and 0x0A to set this rate to 10 samples /sec and then 0x38 to get it to start sending data.
The paper tells me what data to expect in return from these commands, also given in Hex.
It says I could send the decimal ekvivalent, eg. 68, 54, 10, 56.

What node should I use to send this? Write a Byte? Write Bytes? I have tried some of them, but donā€™t get the expected result.

A led on the device flashes when I send commands, so I am pretty sure I have a connection.

Any advice is greatly appreciated!

Thanks, Ole

You are assuming correctly. Each of those is a byte, so Write a Byte. Do use the decimal equivalents, because Unreal only understands decimal in its node fields.
Right after you Write a Byte, if you call Read Bytes, does the array comes empty? Did you try to delay before reading the deviceā€™s answer? Maybe youā€™re just expecting the answer too soon?
Iā€™d try it with a Read Byte on Tick for a while after sending a command. To see how long it takes to answer.

I recommend using Flush before sending a command, just to make sure thereā€™re no previous bytes in the read stream waiting to be read. Because that could look like a command answer, but would just be stuff that was sent before your command.

Thank you ever so much! I tried this, and it actually works!
I send 68 as one byte, then 54, 10 and finally 56. Then my device starts sending data! ( I also get some correct response bytes when sending the 3 first commands)

I have set up a Read Bytes - node in the Tick event. It seems to get the correct data back.
The device sends 16 bytes of data, including speed and heart rate. I can decide how often it sends this info, so now I just have to find a good balance of it sending the info and Unreal trying to read it.
And then teach Unreal to decode 3 separate bytes of numbers to a float that I can use in my application :slight_smile:

Thanks again for the info! And thank you for maintaining this plugin!

Ole

3?? Thatā€™s kinda odd.
Anyway, if you need to convert 4 bytes to int or float or from those into bytes, there are nodes for that in the plugin too. BytesToInt and IntToBytes. Same for float.

Yes, it is!
Here is the data string I now successfully receive (from the manual of the device):

"The data string/packet is 16 bytes long. The data string/packet
starts with 26 00 decimal (1A 00 hex) then 14 bytes of data.
Example : 1A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 (hex)
Data string description (HEX):

1A 00 | 00 00 00 | 00 00 00 | 00 00 00 00 | 00 00 00 | 00
ACKā€¦| Verticalā€¦| Speedā€¦| Run Timeā€¦| Horizont. | Heart Rate

ā€¦
SPEED: Speed of running belt in yds per second. 3 bytes. 00.000-99.999 Yards/Second.
00. 00 00 format. Example: 04 03 5B = 4.859 yds per second. 04.(03 5B). 03 5B hex = 859.
ā€¦"

Itā€™s the speed Iā€™m mainly interested in.
You see it is coded in a non standard way with one byte before the decimal mark and two after.
So I have to take byte 6 to 8 and convert it in this way and make in into the speed of my camera in the VR simulation.

Iā€™ll take a look at your nodes and the C++ behind it. I guess Iā€™ll make my own blueprint node in C++ to do this. Since it is so ā€œnon standardā€ there is no reason that you have support for this in your plugin. But it is really neat that we can play with small hardware devices in this way!

It is! Iā€™ve used it to control lights and stuff like that from inside Unreal. Really cool.

Thatā€™s a very interesting way to encode a float. The way I make my conversions in C++ wonā€™t help you with that, because it just breaks apart or put back together actual float/int bytes, in the way they are organized in memory.

I bet youā€™ve already came up with some algorithm for that, but in case you didnā€™t, either in Blueprints or C++ Iā€™d do it this way:

  • Letā€™s call the three bytes A, B and C. A is the integral part and BC the decimal one.
  • A + (B * 256 + C) * 0.001

In Blueprints, Iā€™d write that formula in a Math Expression node and convert it to a macro. That way itā€™s easy to use it elsewhere and the Math Expression node makes it very performant.

EDIT: Actually, in C++ you can make that formula even faster: A + ((B << 8) + C) * 0.001f

Thank you for your suggestions!

I was not aware of the Math Expression node in Blueprint. That seems very usefull.
But I really liked your C++ bitshift version, so I guess Iā€™ll make use of that!

Hello there.

Thanks so much for the plugin, I think itā€™s going to be perfect for what i want to do.

Iā€™m fairly new to Arduino and to Blueprints in Unreal. I was able to get your plugin up and running and iā€™m sending data from a potentiometer into Unreal and using it to drive a attribute on a camera. Thatā€™s all working great. Now iā€™m looking at adding some buttons in Ardunio to change what attributes in Unreal iā€™m controlling. So i have the issue of needing to send multiple items of data. In this case just two items, the ā€œmodeā€ that the Arduino is in, and the values from the potentimeter. But my hope is to add some more items in the future.

So after reading through all the posts on this thread it sounds like iā€™m dealing with the classic ā€œprotocolā€ issue that you describe back in post #42. My issue is that your solution is a bit beyond my abilities at the moment. While Iā€™ve done a fair bit of scripting in other languages Iā€™ve never had to deal with bytes before so itā€™s a whole new scary world.

However i came up with a different way if handling multiple bits of data all as a string. I know in your original post you say that this can freeze your game, but in my, albeit limited testing, i am not seeing any freezing. everything seems to run fine at 60fps.

In my Arduino code iā€™m sending it out like this:

Serial.print(ā€œm:ā€);
Serial.print(mode);
Serial.print(ā€œv:ā€);
Serial.print(outputValue);
Serial.println(ā€œeolā€);

Then in blueprints iā€™m using a serials of string splits to separate out the data. Iā€™ve attached a screenshot. This seems to be working just fine. But iā€™m guessing there is some very good reason why doing it this way is not such a great idea, but i donā€™t know what that reason is.

thanks
eric

Defining what you are sending and is expecting to receive is the more correct way of doing it, it will look cleaner and error checking is simpler. You will also save resources on both ends doing the more correct way.

Like @Lambdarevolution said, youā€™d save resources by using bytes. Each character in a string is a byte by itself (plus the null one at the end to signal the end of the string). Besides the processing to process the strings, convert from/to numbers etc.

But, if you still want to use strings, replace ReadString with ReadLine. If you just ReadString, itā€™ll read whatever is in the serial buffer, and youā€™ll likely get half strings many times. Because Unreal and the Arduino are not in sync. So you might get things like ā€œmodev:outpā€ in one frame and ā€œutValueeolā€ on the next frame, because Unreal read while Arduino was still sending. Whereas if you use ReadLine, the reading will only end once it gets a line end, which youā€™re sending with Serial.println. It will freeze if Arduino takes too long to send another line end, so keep doing that in the loop as fast as possible.

Another thing you will want to do is use Flush after every ReadLine, to clear the serial buffer before the next frameā€™s ReadLine. Otherwise, if Arduino is sending data faster than Unreal is reading, as each ReadLine stops in the next line end, it will potentially leave many other strings in the serial buffer, and on each ReadLine youā€™d be reading only one group of commands, which would not be the latest one.

Thanks for the advice and tips
Itā€™s not that I WANT to use strings, itā€™s just the only thing currently within my current technical skills and knowledge. I just need to go off and learn how to use bytes as it does sounds like the better solution. Maybe after iā€™ve done a lot of the other components for this project iā€™ll come back to that.

thanks again for the great plugin.

thx-e

Can I use this plugin in my c++ project? I tried to include in the dependency in Build.cs and it does not work.

Hi,
is it posible to run with 4.22?
Thank you for this great tool

OlĆ” Rvilani, o plugin Ć© fantastico, qual a possibilidade de uma compilaĆ§Ć£o para a UE 4.22?

4.22 release is here

It was missing the UE4DUINO_API macro in the class declaration. Sorry about that! I only learned about it recently. I guess all the times I needed to use my plugins in C++, I got lucky by having that macro already unaware of what itā€™s for. hahaha You can download the fixed source or the released version for 4.22 in the link above.

Youā€™re very welcome! It always was :slight_smile: But if you donā€™t want to build it, the release is in the link above.

Obrigado! Bem possĆ­vel, eu diria. XD

Obrigado! !!! top