Hello everybody
I have a challenging first Arduino project where I would greatly appreciate some guidance.
Here is a short introduction to the problem and situation:
We have a machine that works with a laser. The machine is a bit older, no longer supported and should be considered as given (no possibility to access the control program).
The problem: it came without the laser and so we had to buy a new one to make it work again. Even though the new laser is also controlled (parametrized) via RS232 it doesn’t work as a drop-in replacement. Of course the new laser has a different command protocol than the original one
I now have to realize some protocol translation between the machine and the laser:
[machine] ============[laser]
as translator I am using an Arduino Mega with appropriate RS232<>TTL converter on the hardware serial ports 1 and 2. The serial settings for all devices are: 57600 Baud, 8 bits data length,
1 stop bit, no even/parity check,
There is a two-way communication i.e. the machine polls the laser status / laser sends its status, the machine sets output power / laser confirms, laser sends error message / machine shuts down laser
The (new) laser accepts and sends commands and replies as ASCII strings via RS232, from the manual:
“All commands and responses will consist of printable ASCII characters. Commands are typically three or four letter mnemonic codes followed by a parameter, if required. All commands and responses will be terminated with a (CR, 0x0D, \r) character”
The machine sends and expects HEX codes via RS232, from the manual:
"The command from computer consists of operation code (1 byte) and optional parameter
(ASCII-string), terminated by the “carriage return” (“CR”) byte (0x0D)."
an example is given in the manual:
Command | Operation code (hex)| example
“Read pump current value” | 0x82 | 82 0D Laser answers by the CR-terminated
ASCII-string, current value is in Amperes.
Using a terminal program like hterm I can pretend to be the laser or the machine and talk to the other side. Here is a part of the hterm communication log (received hex characters exported to text, commented with signification by me; with timestamps).
10:44:15.487:
840D //read temperature
10:44:17.806:
8F0D //turn emission OFF
10:44:17.931:
80302E3030303030300D85300D //set current 0.000000; set op. mode ACC
10:44:18.025:
900D //switch ON aiming beam
10:44:18.478:
860D //read status
10:44:19.181:
840D //read temperature
The Arduino Mega now has to translate commands and answers to the protocol used by the other side.
I think, with these explanations you can more or less see, what I am trying to do.
Questions:
-
How would you organize the program (divide in sub-tasks) and what kind of basic program structure would be apropriate? My approach now is to divide into the parts a) communication with machine, b) communication with laser, c) translation between the two protocols.
-
I am stuck with mainly a) and to some extent b): the machine seems to use some mixture of hex command byte and optional ascii strings. I have some troubles translating this into chosing the right code for reading the serial port and transferring the received data to something useable in c).
-
Testing code variants was made impossible by the fact that I was not able to receive anything when the Arduino was connected to laser and machine. I found that Serial1.available() and Serial2.available() always return “0” when I print them to the serial monitor. At the same time I am sure that the machine was constantly polling status and laser temperature as verified by connection to the PC and hterm.
I am also sure that the Arduino ports work correctly as I tested them simultaneously connecterd to 3 RS232 ports on a PC.
Thank you for reading until here
have a nice weekend!