SDM-SIO1A and SDM-SIO4A Serial Input/Output Modules
22
6.3.2.2 Example using RS-485 Mode
CRBasic Example 6-2. 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 datalogger to the sensor.
'The program then sits in a loop until the SDM-SIO1A reports it has data
'available
'The datalogger 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 datalogger 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
'Convert string to float
ConvertedValue = ReturnedData
'add an offset to the returned floating point value
ConvertedValue = Converte 100
SerialClose
(SensorPort)
'Close the serial port to the sensor
'this places the SDM-SIO1A into its lowest power mode
Next Scan
EndProg