Idea: Writing to Serial Port

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
Luxus21552
Posts: 2
Joined: Sat Feb 24, 2018 6:47 am

Idea: Writing to Serial Port

#1 Post by Luxus21552 » Sat Feb 24, 2018 7:33 am

Hey,

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.

User avatar
Administrator
Site Admin
Posts: 4849
Joined: Sat Jan 05, 2008 4:21 pm

Re: Idea: Writing to Serial Port

#2 Post by Administrator » Sat Feb 24, 2018 7:16 pm

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.

User avatar
Administrator
Site Admin
Posts: 4849
Joined: Sat Jan 05, 2008 4:21 pm

Re: Idea: Writing to Serial Port

#3 Post by Administrator » Sun Feb 25, 2018 9:38 pm

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: https://github.com/SolarStrike-Software ... 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 = serial.open("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");
end
Right now serial.open() 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");
end
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.

Luxus21552
Posts: 2
Joined: Sat Feb 24, 2018 6:47 am

Re: Idea: Writing to Serial Port

#4 Post by Luxus21552 » Mon Feb 26, 2018 12:16 pm

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!
Last edited by Luxus21552 on Tue Feb 27, 2018 2:02 am, edited 1 time in total.

User avatar
Administrator
Site Admin
Posts: 4849
Joined: Sat Jan 05, 2008 4:21 pm

Re: Idea: Writing to Serial Port

#5 Post by Administrator » Mon Feb 26, 2018 12:35 pm

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests