B.2 Example using RS-485 mode
CRBasic Example 4: RS-485 mode example
'-----------------------------------------------------------------------
'Example use of the SDM-SIO1A.
'This example shows how to open the RS-485 serial port using an SDM-SIO1A.
'Data is sent from the data logger to the sensor.
'The program then sits in a loop until the SDM-SIO1A reports it has data
'available
'The data logger then retrieves the data and places it into a string
'The returned string is then converted to a float and an offset is applied
'-----------------------------------------------------------------------
Public
ChkValReturned
'value returned by the SerialInChk function
Public
AvailableData
'amount of data in the SDM-SIO1As buffer at present
Public
ReturnedData
As String
* 100
'string where the data from the data logger
'is stored
Public
ConvertedValue
As Float
'floating point value returned by sensor
SequentialMode
BeginProg
Const
SensorPort = 32
'Declare the serial port the sensor is set to
'The sensors address switch should be set to position 0
SDMSpeed
(30)
'Optionally set the SDMSpeed, not normally needed
Scan
(1000,mSec,0,0)
'Open serial port to RS-485 mode, 115200bps, 8-bit data, 1 stop bit, and
'no parity
'note that the 'SerialOpenFormat' parameter is 19 for RS-485 mode
SerialOpen
(SensorPort,115200,19,100,10000)
'open the serial port to
'the sensor
'Request data' will need to be replaced with the correct command for
'your sensor
SerialOut
(SensorPort,
"Request data"
,
""
,0,10)
'Send data to the sensor
'wait for the sensor to respond using a loop this time – this may be useful
'if there is not a predictable response from the sensor.
Do
ChkValReturned =
SerialInChk
(SensorPort)
'Get available data
AvailableData = ChkValReturned
AND
4095
'mask off the input pin flag
'(bit 16)
Loop Until
AvailableData <> 0
'wait until data is available
SerialIn
(ReturnedData,SensorPort,100,0,100)
'Get data from the sensor
SDM-SIO1A and SDM-SIO4A Serial Input/Output Modules
37