We will continue with expression functions used for serial communication. In first part we described array expression functions, which are used to prepare the data for serial communication. Now we will describe serial expression functions that are responsible for actually sending the data.
serial_list() - Displays all available COM ports of computer. serial_info(port) - Displays additional info of COM port of computer port - COM port name. Use string type ; e.g. "COM3" serial_open(port,baudrate,bits,parity,stopbits,flowcontrol) - Opens serial COM port channel and sets serial communication parameters port - COM port name. Use string type ; e.g. "COM3" baudrate - baudrate value, e.g. 4800bd ; 9600bd ; 19200bd ; 115200bd bits - data bits value, 5 ; 6 ; 7 ; 8 ; 9 data bits parity - parity value, 0 - no parity ; 1 - odd parity ; 2 - even parity stopbits - stop bit value , 0 - no stop bit ; 1 - one stop bit ; 2 - two stop bits flowcontrol - flow control value; 0 - no flow control ; 1 - flow control enabled Return value: Returned value is zero when port is successfully opened. Returned value is 1 when port has not been opened. Returned value is integer number. serial_config(port,baudrate,bits,parity,stopbits,flowcontrol) - Configures serial COM port channel communication parameters port - COM port name. Use string type ; e.g. "COM3" baudrate - baudrate value, e.g. 4800bd ; 9600bd ; 19200bd ; 115200bd bits - data bits value, 5 ; 6 ; 7 ; 8 ; 9 data bits parity - parity value, 0 - no parity ; 1 - odd parity ; 2 - even parity stopbits - stop bit value , 0 - no stop bit ; 1 - one stop bit ; 2 - two stop bits flowcontrol - flow control value; 0 - no flow control ; 1 - flow control enabled Return value: Returned value is zero when port is successfully configured. Returned value is 1 when port has not been configured. Returned value is integer number. serial_close(port) - Closes serial COM port channel port - COM port name. Use string type ; e.g. "COM3" Return value: Returned value is zero when port is successfully closed. Returned value is 1 when port has not been successfully closed. Returned value is integer number. serial_write(port,str) - Sends string type data trough serial COM port port - COM port name. Use string type ; e.g. "COM3" str - String type data that will be sent ; String data should be enveloped in quotes Returned value: Return value is data size. Returned value is integer number. serial_writedata(port,data) - Sends byte type data trough serial COM port port - COM port name. Use string type ; e.g. "COM3" data - Byte type data that will be sent Returned value: Return value is data size. Returned value is integer number. serial_writearray(port,hnd) - Sends array data trough serial COM port port - COM port name. Use string type ; e.g. "COM3" hnd - array handle Return value: Return value is data size. Returned value is integer number. serial_read(port,size,timeout(optional)) - Reads data trough serial COM port and prints it at the output window. port - COM port name. Use string type ; e.g. "COM3" size - data size limit to be read; 0 -> no limit in data size, 256 -> Max value of limit timeout - timeout value in milliseconds. After time has elapsed, COM port will not read any incoming data. This argument is optional. Return value: Return value is data size. Returned value is integer number. serial_readdata(port,size,timeout(optional)) - Reads data trough serial COM port and prints it at the output window. port - COM port name. Use string type ; e.g. "COM3" size - data size limit to be read; 0 -> no limit in data size, 256 -> Max value of limit timeout - timeout value in milliseconds. After time has elapsed, COM port will not read any incoming data. This argument is optional. Returned value: Return value is data size. Returned value is integer number. serial_readarray(port,hnd,size,timeout) - Reads data trough serial COM port and saves it to array port - COM port name. Use string type ; e.g. "COM3" hnd - array handle size - data size limit to be read; 0 -> no limit in data size, 256 -> Max value of limit timeout - timeout value in milliseconds. After time has elapsed, COM port will not read any incoming data. Return value: Return value is data size. Returned value is integer number.
Now its time for a simple serial communication example. For this we will use two COM ports, terminal program and PlanetCNC TNG software. Such approach is suitable for getting comfortable with array and serial functions on a somewhat real communication case.
First lets check for available COM ports of our computer. If your computer does not offer any physical serial COM port, you can use virtual COM port software.
Under Control Panel/Device Manager/Ports(COM&LPT) you can easily check available ports:
Same can be done in TNG software using a serial_info() command, which prints the following info:
I will use the COM4 and COM5 ports for communication between the TNG software and terminal program.
TNG software will be “attached” to COM5 and terminal program will be “attached” to COM4. In order that the communication channel between both ports is established, we need to connected COM4 and COM5 with each other. Transmit line of COM4 is connected at the receive input of COM5, and transmit line of COM5 is connected at the receive input of COM4. So anything that will be sent at the COM5 end will arrive at the COM4 receiving end and vice versa.
Terminal program configuration:
HTerm terminal program is attached to COM4 port, communication parameters are set:
– baudrate at 9600
– 8 data bits
– 1 stop bits
-none parity
– no flowcontrol
Since both devices on serial bus need to be configured the same, we will use same port configuration in TNG using function: serial_open():
serial_open(“COM5”,9600,8,0,1,0)
It is mandatory that afters finished transmission, port is closed using serial_close() function.
For start, we just want to say hello to Hterm program, best way to do it is to send string data using serial_write() function.
So, our short MDI program would look like this:
At the Hterm’s end we receive the message:
It would only be appropriate that we answer the call and reply with: Hello PlanetCNC!
However, we need to make sure that PlanetCNC is ready to receive the call and is in “listening” mode, this is done using a serial_read() function:
Timeout value will open the listening window for 4 seconds, which gives more than enough time for Hterm to send the message:
Since this function only reads and prints the string data, we should be able to see the message in our output window:
But usually devices don’t talk to each other using string data for saying hello, but they use data in some structured manner.
This will all make more sense in the next chapter where we will use MODBUS to communicate with relay board.
Last example will describe a short and simple correspondence where we will send array data and set received data into array.
-new array with handle name hnd is created:
=hnd =array_new()
-arraydata is set with array_setdata() function:
array_setdata(hnd,0,0xA1,0xB2,0xC3)
-serial port is opened and configured:
serial_open("COM5", 9600, 8, 0, 1, 0)
-array data is sent:
serial_writearray("COM5",hnd)
-array is cleared and ready for incoming data:
array_clear(hnd)
-data is read and set to array
serial_readarray("COM5",hnd,5000)
port is closed and array is deleted, data is printed:
serial_close("COM5") array_printdata(hnd) array_delete(hnd)
Hterm received data:
Sent data from terminal:
Output window displays the contents of hnd array:
Continued at part 3:
https://planet-cnc.com/serial-communication-modbus-relay-board-with-planetcnc-tng-part-3/
The post Serial communication(MODBUS relay board) with PlanetCNC TNG – Part 2 appeared first on Planet CNC.