Hello Guest it is April 28, 2024, 03:35:28 PM

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - mcardoso

Pages: « 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 »
81
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 26, 2019, 12:42:26 PM »
Example 3: Flushing the buffer

Flushing the buffer is super simple and all it does is delete the contents of the buffer without reading them.

Lets say you open your serial port and sit for a few minutes. The whole time your Arduino has been writing "Hello World" once per second. Obviously your buffer will be very full. Lets say we want to read one line of "Hello World" and we want it to be the most recent one.

Code: [Select]
inst = mc.mcCntlGetInstance()
string = ""

p:flush()
e, string(12, 1200) --Read all 12 characters of "Hello World\r" waiting just over 1 second for them to arrive
mc.mcCntlSetLastError(inst, string)

In this code, we make sure the buffer is completely empty before we read. We allow 1.2 seconds for the data to arrive since this will capture one and only one packet from the Arduino.

82
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 26, 2019, 12:35:45 PM »
I think I can better phrase the answer I gave earlier about the use of the colon in the serial library.

The serial library defines the "port" as a class (as in a datatype in an object oriented programming language). When we open the port, we are also declaring that "p" is a member of the class "port". This class has methods (actions) which it can do. In our case, these are:

-close
-flush
-read
-write

To call a method (action) of a class, you can use the colon. So p:write() is the way to tell "p", member of class "port", that it should write(). This is shorthand and you could still call it normally like a function.

This is explained here in the LUA reference: https://www.lua.org/pil/16.html

83
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 26, 2019, 12:27:31 PM »
Pretty sure I know where your error is in this code:

Code: [Select]
scr.SetProperty("testLabel","Label",p:read(12, 5))
LUA allows you to do multiple returns from a function. This means when the function exits, it can have a statement like:

Code: [Select]
myfunction()
...
return a,b,c
end

When you call this function you might do something like this:

Code: [Select]
x, y, z = myfunction()
Where x gets the value returned by "a", y the value for "b", and z the value for "c". You don't need to use all 3, however they always return in this order.

The LUA serial function read() returns 3 variables as shown below:

Code: [Select]
e, data_read, size = p:read(length, timeout)
"e" is the error code, "data_read" is the data, and "size" is the length of the returned string.

When you called this function inside another function, you only grabbed the first returned value, in this case the error code not the data you wanted.

To make this work, you'll need to do something like this:

Code: [Select]
e, string = p:read(12, 5)
scr.SetProperty("testLabel","Label",string)

Note that in this case I captured the first return "e" but did nothing with it and I didn't even bother to capture the 3rd return "size".

84
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 26, 2019, 12:19:04 PM »
Example 2: Read the entire buffer character by character, stop when empty. Data is concatenated into a string at the end of each loop.

Code: [Select]
inst = mc.mcCntlGetInstance()
local string = ""
local dat = ""
local len = 0

for i=0,4095,1 do
    e, dat = p:read(1,5) --1 character, 5ms timeout
    if  (e ~= 0 and e~=9) then
        mc.mcCntlSetLastError(inst, "Read Failed, Error: "..tostring(e))
    elseif (e==9) then
        mc.mcCntlSetLastError(inst, "Buffer Empty")
        break --Exit the for loop
    else
        string = string..dat
    end
end
mc.mcCntlSetLastError(inst, string.len(string).." characters received. Data is: "..string)

Here we read until getting an error 9 (Timeout) which indicates that data was not found before the timeout value expired. Timeout is a great way to handle real world latencies in network traffic.

85
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 26, 2019, 12:10:53 PM »
Lets see if I can give you some examples. I'm not familiar with labels, but I can use the error bar. I'm typing this straight into the forum so please excuse and correct any syntax errors.

Example 1: Read exactly 10 characters from the serial port and print them to the error bar on screen. This assumes the port is open.

Code: [Select]
inst = mc.mcCntlGetInstance()
local string = ""  --Empty string
local len = 0

e, string, len = p:read(10,5) --Read 10 characters, save to "string", 5ms timeout, string length saved in "len"
if (e~= 0) then
    mc.mcCntlSetLastError(inst, "Read Failed, Error: "..tostring(e))
else
    mc.mcCntlSetLastError(inst, tostring(len).." characters read. Data is: "..string)
end

In this example, we want to read 10 characters. If this happens without error, we print the string length (returned from the function call) and the actual data received (not that termination characters count to the length but are not visible). If an error occurs, we print an error message with the error code.

86
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 25, 2019, 10:09:06 PM »
I am not an expert in programming by any stretch so this is likely a super butchered explanation...

When you call the line to open the port, you assign a pointer or address of the port to the variable "p"

From then on, your functions will use the name of the port "p" (which you can set to anything you want BTW) and call functions from the luars232 library.

As I understand... p:write() is shorthand for p.luars232.write() or something like that. It is an abbreviated form of a function call.

Once I got all my serial code working, I wrapped it up and put it in a module. I found this helpful to help keep my code organized and keep the screen load script from getting filled up with too much junk.

Signing off for the night. Check in tomorrow to see where you got.

Nice Work.

-Mike

87
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 25, 2019, 09:47:40 PM »
Oh yeah:)

Once the port is open you aren't allowed to open it again!

Try a button on the screen with the code

Code: [Select]
p:write("Hello Arduino!\r")

88
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 25, 2019, 09:38:42 PM »
OK, So it is getting an error opening the port. Hmmmmm. Is this a USB port or a hardware RS232 port?

I don't see why you couldn't use a USB port or a USB->RS232 converter, however the PC I am using had a RS232 port embedded already so I stuck to using that.

It seems like you're doing everything right? I'm a bit stumped, but now very invested in getting this working!

I know you posted about it earlier, but can you double check that you don't have PuTTY or anything else open that might be using the serial port? I usually get this error when I leave my servo configuration software open and try to use Mach.

89
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 25, 2019, 09:29:58 PM »
Preemptively, here are the error codes, extracted from the LUA source.

0=    { "RS232_ERR_NOERROR", RS232_ERR_NOERROR },
1=   { "RS232_ERR_UNKNOWN", RS232_ERR_UNKNOWN },
2=   { "RS232_ERR_OPEN", RS232_ERR_OPEN },
3=   { "RS232_ERR_CLOSE", RS232_ERR_CLOSE },
4=   { "RS232_ERR_FLUSH", RS232_ERR_FLUSH },
5=   { "RS232_ERR_CONFIG", RS232_ERR_CONFIG },
6=   { "RS232_ERR_READ", RS232_ERR_READ },
7=   { "RS232_ERR_WRITE", RS232_ERR_WRITE },
8=   { "RS232_ERR_SELECT", RS232_ERR_SELECT },
9=   { "RS232_ERR_TIMEOUT", RS232_ERR_TIMEOUT },
10=   { "RS232_ERR_IOCTL", RS232_ERR_IOCTL },
11=   { "RS232_ERR_PORT_CLOSED", RS232_ERR_PORT_CLOSED },
12=   { "RS232_ERR_BREAK", RS232_ERR_BREAK },
13=   { "RS232_ERR_FRAME", RS232_ERR_FRAME },
14=   { "RS232_ERR_PARITY", RS232_ERR_PARITY },
15=   { "RS232_ERR_RXOVERFLOW", RS232_ERR_RXOVERFLOW },
16=   { "RS232_ERR_OVERRUN", RS232_ERR_OVERRUN },

I entered the numbers by hand, but I'm pretty sure they are right.


90
Mach4 General Discussion / Re: Making a connection through rs232 in Mach4
« on: November 25, 2019, 09:13:44 PM »
OK, two thoughts here.

First, 3804 is a little old for Mach 4. Couldn't hurt to install the latest. Maybe something has changed since then? Probably not, but thinking out loud.

Second (and maybe do this first), the returned error code "e" is a numerical value. It is 0 when there is no error and something else that we can use to troubleshoot the error.

Let'd modify your button code to say the following

Code: [Select]
mc.mcCntlSetLastError(inst, "Cannot Open Serial Port. Error: "..tostring(e))
That should print the error code number in the window and we can go from there.

Pages: « 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 »