SerialPort programming - Send and read data through SPL SerialPort channel


1. Bind existing COM2 SerialPort
  1. SPL application sample uses SerialPort COM2 to transfer SPL script to application.

  2. If you want to transfer your data through this COM2, you can do this by binding COM2.

  3. Select "Application" tab.

  4. 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
    
      
 

  1. Click "Send to MF Application" button or Press "F5" key.

  2. Executed result:




2. Write text data to SerialPort
  1. Use "Write(string)" method to send data to SerialPort.

  2. Type script as follows.

      
    COM2.Write("Hello World")
    
      


  3. Click "Send to MF Application" button or Press "F5" key.



  4. Emulator will log received data through SerialPort into log list. You can check this log from the Log tab.

  5. Select "Log" tab.

  6. Executed result:




3. Write byte[] data to SerialPort
  1. Select "Log" tab.

  2. Click "Clear (F4)" button to clear log list.

  3. Check "Display hex format for the SerialPort data"

  4. Select "Application" tab.

  5. Use "Write(byte[] buff)" method to send byte array data to SerialPort.

  6. In order to create byte array, you can use "Util.CreateArrayByte(length)" method.

  7. 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)
    
      


  8. Click "Send to MF Application" button or Press "F5" key.



  9. Emulator will log received data through SerialPort into log list. You can check this log from the Log tab.

  10. Select "Log" tab.

  11. Executed result:




4. Send data from emulator to application
  1. When you are using "COM2", you don't need to read data direct from serial port.

  2. Since "COM2" is used for SPL delivery channel, you can use the event of SPL engine instead of using "Read()" method.

  3. We already defined procedure "myProc1" in the section1, and this procedure was called automatically when some data is arrived through the serial port COM2.

  4. Select "Emulator" tab.

  5. 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)
    
      


  6. Click "Execute Script on the Emulator" button or Press "F5" key.



  7. Executed result will be shown as follows.