| |
SerialPort programming - Send and read data through SPL SerialPort channel
1. Bind existing COM2 SerialPort
- SPL application sample uses SerialPort COM2 to transfer SPL script to application.
- If you want to transfer your data through this COM2, you can do this by binding COM2.
- Select "Application" tab.
- Add script as follow in order to bind existing SerialPort COM2.
| |
clear
BindSerialPort
/TargetName:COM2
/Procedure_OnReceive:myProc1
procedure myProc1
buff = value.ReceivedBytes
print "Received data -> " + Util.BytesToHex(buff)
end
| |
|
|
|
|
- Click "Send to MF Application" button or Press "F5" key.
- Executed result:
2. Write text data to SerialPort
- Use "Write(string)" method to send data to SerialPort.
- Type script as follows.
| |
COM2.Write("Hello World")
| |
|
- Click "Send to MF Application" button or Press "F5" key.
- Emulator will log received data through SerialPort into log list. You can check this log from the Log tab.
- Select "Log" tab.
- Executed result:
3. Write byte[] data to SerialPort
- Select "Log" tab.
- Click "Clear (F4)" button to clear log list.
- Check "Display hex format for the SerialPort data"
- Select "Application" tab.
- Use "Write(byte[] buff)" method to send byte array data to SerialPort.
- In order to create byte array, you can use "Util.CreateArrayByte(length)" method.
- Type script as follows.
| |
buff = Util.CreateArrayByte(5)
buff[0] = 0x31
buff[1] = 0x32
buff[2] = 0x33
buff[3] = 0x34
buff[4] = 0x35
COM2.Write(buff)
| |
|
- Click "Send to MF Application" button or Press "F5" key.
- Emulator will log received data through SerialPort into log list. You can check this log from the Log tab.
- Select "Log" tab.
- Executed result:
4. Send data from emulator to application
- When you are using "COM2", you don't need to read data direct from serial port.
- Since "COM2" is used for SPL delivery channel, you can use the event of SPL engine instead of using "Read()" method.
- We already defined procedure "myProc1" in the section1, and this procedure was called automatically
when some data is arrived through the serial port COM2.
- Select "Emulator" tab.
- Type script as follows on the emulator's editor box.
| |
buff = Util.CreateArrayByte(5)
buff[0] = 0x31
buff[1] = 0x32
buff[2] = 0x33
buff[3] = 0x34
buff[4] = 0x35
COM2.SPLWrite(buff)
| |
|
- Click "Execute Script on the Emulator" button or Press "F5" key.
- Executed result will be shown as follows.
|