SPL SerialPort programming - Read data through SPL SerialPort channel


This tutorial is continued from the previous tutorial.


5. Send data from emulator to application through COM2
  1. SPLConsoleEmulator send SPL script to application through COM2.

  2. In order to send variable length of data to application through serial port, it sends length of data array first.

  3. This is a data protocol between SPL emulator and application.

       Send 4 bytes of data bytes length : 0x01 0xXX 0xXX 0x00
       wait 100 ms
       Send data bytes

  4. And SPL MF emulator supports special method named "SPLWrite" which includes above protocal so that you can send data bytes direct without sending size information.

       portname.SPLWrite(byte[] buff)
       portname.SPLWrite(string text)

  5. You can use this "SPLWrite" method as follows.



      
    buff = Util.CreateArrayByte(2)
    buff[0] = 0xF0
    buff[1] = 0xF1
    
    COM2.SPLWrite(buff)
    
      


  6. Click "Send Script" button to send SPL script to emulator.

  7. SPL engine will execute procedure "proc1" automatically when a data is received through the serial port.

  8. Below shows the result the execution of procedure "proc1".




6. Send data without using "SPLWrite" method
  1. In case of sending data without using "SPLWrite" method, you need to implement below protocol on the emulator.

       Send 4 bytes of data bytes length : 0x01 0xXX 0xXX 0x00
       wait 100 ms
       Send data bytes

  2. Below script is a sample for this protocal.



      
    sizeBuff = Util.CreateArrayByte(4)
    sizeBuff[0] = 0x01
    
    length_of_send = Util.ToUShort(2)
    sizeBytes = Util.UShortToBytes(length_of_send)
    
    sizeBuff[2] = sizeBytes[0]
    sizeBuff[3] = sizeBytes[1]
    
    COM2.Write(sizeBuff)
    
    wait 100
    
    buff = Util.CreateArrayByte(2)
    buff[0] = 0xF0
    buff[1] = 0xF1
    
    COM2.Write(buff)
    
      


  3. Click "Send Script" button to send SPL script to emulator.

  4. SPL engine will execute procedure "proc1" automatically when a data is received through the serial port.

  5. Below shows the result the execution of procedure "proc1".