RS485 Module
Group |
Available functions |
---|---|
Configuration |
|
Data Transfer |
|
Misc |
- class ClassRS485.ClassRS485(_address)
- setBaudrate(baudrate: int) bool
With this method you can setup the baudrate for the RS485 interface.
- Parameters:
baudrate – Choose any baudrate between 300 and 1000000.
- Returns:
True if baudrate was set successfully, False if not.
- Return type:
bool
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setBaudrate(115200)
- setDataSize(datasize) bool
- setPartiy(parity: int) bool
Sets the parity of the communication. Default: NONE
- Parameters:
parity – One of the following constants:
PARITY_NONE
,PARITY_ODD
,PARITY_EVEN
- Returns:
True if parity was set successfully, False if not.
- Return type:
bool
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setPartiy(rs485.PARITY_EVEN)
- setStopbits(stopbits: int) bool
Sets the number of stopbits for the communication. Default: 1 Stop Bit
- Parameters:
stopbits – One of the following constants:
STOPBITS_0_5
,STOPBITS_1
,STOPBITS_1_5
,STOPBITS_2
- Returns:
True if stopbits was set successfully, False if not.
- Return type:
bool
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setStopbits(rs485.STOPBITS_1_5)
- setResponseTimeout(timeout: float) bool
Sets the timeout for a response to a sent message.
- Parameters:
timeout – Timeout in milliseconds
- Returns:
True if timeout was set successfully, False if not.
- Return type:
bool
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setResponseTimeout(1.5)
- setFrameTimeout(timeout: int) bool
Sets the timeout for seperation of frames.
- Parameters:
timeout – Timeout in number of Bits!
- Returns:
True if timeout was set successfully, False if not.
- Return type:
bool
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setFrameTimeout(50) #set timeout to 50 bits
Note
To calculate the timeout time you just have to divide the timeout by the baudrate.
e.g.: a value of 96 with a baudrate of 9600 Baud leads to a timeout of 10ms.
A value of 576 with a baudrate of 115200 leads to a timeout of 5ms
- setTermination(termination: bool) bool
Enables or Disables the 120 Ohm termination resistor on the RS485 line.
- Parameters:
termination –
True
to enable the termination,False
to disable the termination resistor.- Returns:
True if termination was set successfully, False if not.
- Return type:
bool
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setTermination(True)
- resetToDefaults() bool
Resets all settings back to default values. This function is useful for resetting all settings to their defined default state at the start of a test.
- Returns:
True if reset was successfully, False if not.
- Return type:
bool
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.resetToDefaults()
- sendData(data: bytearray) bool
- Sends out the given data over the RS485 interface.
Note: the function returns if the data was transfered and acknowledged from the RS485 Module. So the function returns before data was sent completely.
- Args:
data (bytearray): data to be sent
- Returns:
bool: feedback whether the data was accepted or not. (true = success, false = failed)
- discardInbuffer() bool
This will discard the inbuffer. It is recommended to call this method before you start listening for new data
- Returns:
bool: feedback whether the data was accepted or not. (true = success, false = failed)
- getInbufferSize() int
Returns the amount of data bytes in the inbuffer.
- Returns:
number of bytes
- Return type:
int
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setBaudrate(115200) dataCnt = rs485.getInbufferSize() print(dataCnt)
- getInbufferData(startIndex: int = 0, length: int = 0) bytearray
Returns the received bytes. The data remains in the inbuffer and can be read as often as you want. To clear the inbuffer use the
discardInbuffer()
function- Parameters:
startIndex – The startindex in the buffer from which the data should be read (default is 0, which means from the beginning)
length – Number of bytes which should be read from the inbuffer. 0 means: all data until end of the buffer. (default is 0, which means all data will be returned)
- Returns:
data of the inbuffer
- Return type:
bytearray
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) allData = rs485.getInbufferData() print(allData)
- getFramebufferSize() int
Returns the amount of frames in the inbuffer.
- Returns:
number of frames
- Return type:
int
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setBaudrate(115200) frameCnt = rs485.getFramebufferSize() print(frameCnt)
- getFramebufferData(index: int) bytearray
Returns the received Frame. The data remains in the inbuffer and can be read as often as you want. To clear the inbuffer use the
discardInbuffer()
function- Parameters:
index – The index of the frame (0-255)
- Returns:
data of the frame
- Return type:
bytearray
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) allData = rs485.getFramebufferData(0) print(allData)
- async waitForResponse(timeout_ms: int) bool
Returns the received Frame. The data remains in the inbuffer and can be read as often as you want. To clear the inbuffer use the
discardInbuffer()
function- Parameters:
index – The index of the frame (0-255)
- Returns:
data of the frame
- Return type:
bytearray
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) rs485.setBaudrate(115200) rs485.sendData((1,0,5)) await rs485.waitForResponse(2000) rxData = rs485.getInbufferData() print(rxData)
- getLastResponseTime() int
Returns the time between last sent data byte and the receiption of the fist byte
- Returns:
response time in milliseconds
- Return type:
float
Example usage:
from testframework import * #ext.init() #only neccessary if you run this code in debug console, outside of a testrun rs485 = ext.getRS485(0) responseTime = rs485.getLastResponseTime() print("%.2f ms" %responseTime)
- async doBaudrateCheck(baudrate, testfunction) int
Checks which is the minimum and maximum baudrate where an answer is received
- Parameters:
baudrate (int) – baudrate from which the test should be start
testfunction (functionpointer) – awaitable function which will be called after setting new baudrate. The function should be return
True
orFalse
- Returns:
BaudrateCheckResult object with the min, max and mean baudrate
- Return type:
BaudrateCheckResult
Example usage:
As we need a testfunction which returns a
True
orFalse
, we must first define such a function. In this example, this is themyBaudrateTestfunction
.This function sends data and waits for a response. If a response is received, the result is
True
. At this point, you could also check the content of the received data (usegetInbufferData()
) for correctness and make the return value dependent on this.from testframework import * #TPignore async def myBaudrateTestfunction(): global rs485 rs485.discardInbuffer() data = (1,1,0,0,0,1,253,202) #data on which we expect a response rs485.sendData(data) return await rs485.waitForResponse(2000) #its passed if we got any answer async def BautrateTest(): global rs485 rs485 = ext.getRS485(0) result = await rs485.doBaudrateCheck(115200, myBaudrateTestfunction) report.putInfoValue("Minimum working baudrate", result.min) report.putInfoValue("Maximum working baudrate", result.max) report.putInfoValue("Mean working baudrate", result.mean)
Note
Use the
#TPignore
comment infront of a function to prevent the test software from recognising this function as a test function.
- sendRandomData(amount: int, seed: int) bool
Sends a given amount of random databytes.
Random data which is different for each test run could lead to different, non-reproducible results. To prevent this, this is a pseudo-random sequence. The same sequence of numbers is always generated with the same seed. The test thus remains reproducible. Different number sequences can be generated with different seeds.
- Parameters:
amount (int) – Number of bytes to be send
seed (int) – seed
- Returns:
True
if data was send,False
if there was a failure.- Return type:
bool
Example usage:
from testframework import * rs485 = ext.getRS485(0) rs485.setBaudrate(115200) rs485.setTermination(True) async def SpamTest(): global rs485 res = rs485.sendRandomData(8000, 1) report.checkBool("Send Random Data", res) #wait for sending finished + give the device some time to recover #(8000 bytes at 115200 baud need round about 700 Milliseconds) await sleep_ms(2000) data = (1,1,0,0,0,1,253,202) rs485.discardInbuffer() rs485.sendData(data) #now send correct data and check if we get an answer res = await rs485.waitForResponse(2000) report.checkBool("Answer Received?", res)
Note
sendRandomData
just puts the random data into the RS485 Module send-Buffer and returns. So you have to wait until transfer is finshed.You can calculate the needed transfer time: time = (amount * 10) / baudrate