Page 1 of 1

Idea: Writing to Serial Port

Posted: Sat Feb 24, 2018 7:33 am
by Luxus21552

I'm not sure if MicroMacro is still being developed, but I've been using it for a while and I really like it.
Recently, I started to learn about programming microcontrollers - the MEGA32U4 to be exact - and I found out that it is fairly easy. The chip also has the functionality to double as an input device through USB, without having to rely on non-microsoft drivers or anything that circumvents anti-cheat modules. What I did, was to interface it between my actual keyboard and my PC and it's able to send input sequences without a problem.

Currently I'm using python to do something like this:

Code: Select all

serial.Serial("COM6", 115200).write("do stuff".encode());
The microcontroller receives "do stuff" through the port COM6 with a baudrate of 115200 and then does whatever.

My suggestion for MicroMacro would be to add a similiar function like Serial.Write that allows communication on a USB port.
I think it's pretty niche to have this kind of function, but it could lead to interesting projects that are entirely isolated in MM. Like running a bot on a raspberryPI for another PC, without having to rely on a seperate server-client software solution. Just Plug & Play.

Anyway, just a small idea.

Re: Idea: Writing to Serial Port

Posted: Sat Feb 24, 2018 7:16 pm
by Administrator
I'm not sure if MicroMacro is still being developed
It sure is!

I also really like your idea. I had wanted add some functionality for GPIO and this seems quite similar. Problem is, I need a way to test these things if I'm going to code them.

I think I might very well have a microcontroller around somewhere that I might be able to use for testing. Worth a shot, at least. Maybe if I can find some time to experiment with it in the next week. I'll keep you updated.

Re: Idea: Writing to Serial Port

Posted: Sun Feb 25, 2018 9:38 pm
by Administrator
And it's done. I'll probably change a few things yet, and need to write up proper documentation, but this is a good start.

Commit: ... 1ca92522a5

Packages for 1.97.01, which include the serial module, are available on the download page

Quick and easy, here's the run down.

Code: Select all

local myPort ="COM7");
print("Serial port:", myPort); -- should print either "Serial Port COM7" or "Disconnected Serial Port"
Opens a serial connection. Accepts the port name and optionally baud rate; default 9600.

Code: Select all

if( myPort:isConnected() ) then
  print("Connection succeeded");
Right now always return a serial port object even if it fails. I might change that behavior, but for the time being be sure to test if it is connected.

Code: Select all

local data,more = myPort:read(); -- Read as much as our buffer can hold (10240)

-- or --

repeat -- Keep reading until there's no more left in the queue
  data,more = myPort:read(4); -- Read 4 bytes at a time
until not more
read() can accept one parameter: the number of bytes to read (up to the maximum buffer size of 10240). It returns the data itself (as a string) and a boolean describing if there's still more data in the queue to read.

Code: Select all

if( not myPort:write("Hello World") ) then
  print("Failed to write data");
Write() accepts a string to write to the serial connection and returns a boolean describing the success.

Code: Select all

myPort:close(); -- We're done here. Time to go home.
Closes the connection, obviously.

Re: Idea: Writing to Serial Port

Posted: Mon Feb 26, 2018 12:16 pm
by Luxus21552
Thank you, that was fast! :o

I've had no problems using your example and it seems to be working flawlessly, without any sort of delay or issues.
Your function to read is also a much appreciated and easy solution to work with.

Tested it using a Arduino Pro Micro via USB ($4~). I'd be happy to donate a few bucks if you'd get a donation button some day! Done!

Re: Idea: Writing to Serial Port

Posted: Mon Feb 26, 2018 12:35 pm
by Administrator
That would be appreciated! I slapped together a crappy donate page.

I'm also glad to hear the code is working well for you. Please let me know if you have any other suggestions; whether that be in relation to the serial methods or other functionality.