Simon,
Here are some of the key parts of scripting serial communications. I have attached my Mach 4 module (titled mcServo) which I created to communicate with my AB Ultra 3000 servo drives. If you do want to use this module, just be aware that the functions are tailored for my particular communications needs (including checksum calculation and verification, timing requirements, and Master/Slave communication). You will need to learn how the serial library works and use my scripts as an example to build what you need.
First off, you need to add the following header to wherever you are writing your scripts. This gets the current Mach 4 instance, includes the use of the LUA serial library, and defines a variable with the name of whatever COM port you would like to use. The io.stderr[/code] line was included in some example code I found, however I don't know if it is necessary or what its function is.
local inst = mc.mcGetInstance()
rs232 = require("luars232")
port_name = "COM1"
local out = io.stderr
Next you need to open the Serial Port and set the communication parameters. In this example I open COM1 at 38400 Baud and 8N1. You will find I do handle exceptions in the real serial module (attached), however they have been removed for clarity here.
e, p = rs232.open(port_name)
-- set port settings
assert(p:set_baud_rate(rs232.RS232_BAUD_38400) == rs232.RS232_ERR_NOERROR)
assert(p:set_data_bits(rs232.RS232_DATA_8) == rs232.RS232_ERR_NOERROR)
assert(p:set_parity(rs232.RS232_PARITY_NONE) == rs232.RS232_ERR_NOERROR)
assert(p:set_stop_bits(rs232.RS232_STOP_1) == rs232.RS232_ERR_NOERROR)
assert(p:set_flow_control(rs232.RS232_FLOW_OFF) == rs232.RS232_ERR_NOERROR)
From now on, your open port will be named "p" and the methods you call will begin with "p:". For example if we wish to close the port, we use the following, where "e" is a returned error code.
e = p:close()
If we wish to flush the serial buffer, we can use either of the following lines of code. Obviously using the method :flush is simpler, however reading the entire contents of the buffer (4096 bytes, 5ms timeout) is equally effective.
e = p:flush()
e = p:read(4096, 5)
Now comes the good stuff. If we wish to write "Hello World" out of the port, we use the following. Note the use of standard string escape characters, in this case \r is carriage return. If you had a laptop with PuTTy open and connected to the serial network, you should see "Hello World" appear on the screen. len_written is an optional function return which gives you the length of the written string
e, len_written = p:write("Hello World\r", 5)
To read data on the port, we use the following. In this example we would be reading 10 characters (bytes) which included non-printed characters like carriage returns and applying a 5ms timeout. Again, "e" is an exception code (hopefully zero), "data_read" is the string of characters read from the port (this is what we want), and "size" is the length of the received string.
e, data_read, size = p:read(10, 5)
If you are writing out on the port and expecting a response back, it is a good idea to flush the buffer immediately before writing.
Please ask questions if you have them.
Mike