evolve
Serial Port
Serial Port
Evolve has support built in for serial ports this goes back to its original core as being a language for embedded software engineers who usually have a debug port out of the processor for sending and receiving debug information.
Basic Communications
Opening and closing a port
A port can be opened with the ser_open command which takes three argument the first is a boolean which it gets set to true if the port was opened successfully. The second is an integer which gets set to the port id this is used to direct an function to the port. The third is the port name e.g. COM1.
ser_open PortOpen PortId “COM1”
The port can be checked to see if it is open by the ser_isopen command which sets its argument to true true if the port is open.
The port is closed with the ser_close command.
Sending Data
Data can be sent with the ser_tx command this takes three arguments the first is used to return the actual number of bytes sent, the second argument should be a byte list containing the data to send or a string to send, other variables will be sent as their string representation. The third argument is the number of bytes to send.
Data[10] = 0x00
ser_tx BytesSent PortId Data 5
The above code would send the first 5 bytes of data from the Data list, the number of bytes sent could be confirmed by checking the value of BytesSent.
Receiving Data
Data is received with the ser_rx command this takes the same three arguments as ser_tx but the first argument gets set to the number of actual bytes received and the buffer is set to the received data. If the receive buffer is not the same size as the buffer size set in the fourth argument then the buffer will be resized to the defined size.
Data[10] = 0x00
ser_tx BytesRecieved Data PortId 5
If the above code was used upon completion the number of elements in Data would be 5 and BytesRecieved would be set to the number of bytes read by the port.
The following example creates a byte list and sends it out on Com port 3 it then receives the data back and verifies it. To run this code you need to connect the RX and TX pins on your serial port.
//---------------------------------------
// File: serialport.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//open the port
ser_open Open Id "COM4"
// check port open
if Open == false
outl "Cant open port"
exit_program
_if
//adjust settings
ser_baudrate Id #BAUD115200
ser_bytesize Id 8
ser_stopbits Id #STOPBITONE
ser_parity Id #PARITYNONE
outl "Port Open"
//create a byte list full of data
ByteVar = 0xff
Index = 0
while Index < 100
ByteVar[Index] = Index
++ Index
_while
//send the data
ser_tx BytesSent Id ByteVar 100
//check the bytes sent
outl "Bytes Sent: " BytesSent
//create a byte to RX the data into
ByteVarRX = 0xff
//read the data
ser_rx BytesRxed ByteVarRX Id 100
//check the bytes RXed
out "Bytes Received: "
outl BytesRxed
Index = 0
while Index < 100
if ByteVarRX[Index] != Index
outl "Error: " Index
exit_program
_if
++ Index
_while
outl "Test complete ok"
//send the data
ser_tx BytesSent "const" Id 5
ser_rx BytesRxed ByteVarRX Id 5
//check the bytes RXed
out "Bytes Received: "
outl BytesRxed
Test = "TestMessage"
//send the data
ser_tx BytesSent Id Test 11
ser_rx BytesRxed ByteVarRX Id 11
//check the bytes RXed
out "Bytes Received: "
outl BytesRxed
ser_close Id
_block
Getting Port Names
To get the names of the ports on the computer the ser_getports command is used this has two arguments the first is an integer which gets set to the number of ports found, and the second a string list which gets set to the port Ids.
//---------------------------------------
// File: FindSerialPorts.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
ser_getports NumberPorts PortIds
if NumberPorts == 0
outl "No Ports"
else
I = 0
while I < NumberPorts
outl PortIds[I]
++ I
_while
_if
_block
Setting Up the port
The default setup for the port is 115200bps, 8 data bits one stop bit, even parity and no hardware control.
The settings can be changed after the port is open with the following commands.
ser_baudrate This command sets the baud rate the and can be set to the following values.
#BAUD110, #BAUD300, #BAUD600, #BAUD1200, #BAUD2400, #BAUD4800, #BAUD9600, #BAUD14400, #BAUD19200, #BAUD38400, #BAUD57600, #BAUD115200, #BAUD128000, #BAUD256000
ser_dtrcontrol This command sets the DTR control the and can be set to the following values.
#DTRCONTROLDISABLE, #DTRCONTROLENABLE, #DTRCONTROLHANDSHAKE
ser_rtscontrol This command sets the RTS control the and can be set to the following values.
#RTSCONTROLDISABLE, #RTSCONTROLENABLE, #RTSCONTROLHANDSHAKE, #RTSCONTROLTOGGLE
ser_bytesize This command sets the byte size.
ser_parity This command sets the parity and can be set to the following values.
#PARITYNONE, #PARITYODD, #PARITYEVEN, #PARITYMARK, #PARITYSPACE
ser_stopbits This command sets the number of stop bits and can be set to the following values.
#STOPBITONE, #STOPBITSONE5, #STOPBITSTWO
ser_xonchar This command sets the XON character to the passed value.
ser_xoffchar This command sets the XOFF character to the passed value.