SerialPort programming - Send data through SerialPort


1. Add a new COM3 SerialPort
  1. Select "Application" tab.

  2. Add script as follow in order to add a new SerialPort COM3.

      
    clear
    
    SerialPort    COM3
    	/PortName:COM3
    	/BaudRate:9600
    
      
 

  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.

      
    COM3.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. Use "Write(byte[] buff)" method to send byte array data to SerialPort.

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

  3. Type script as follows.

      
    buff = Util.CreateArrayByte(5)
    buff[0] = 0x31
    buff[1] = 0x32
    buff[2] = 0x33
    buff[3] = 0x34
    buff[4] = 0x35
    
    COM3.Write(buff)
    
      


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



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

  6. Select "Log" tab.

  7. Executed result:




4. Display received data as Hex format in the Emulator
  1. You can see the receive data as hex format in the emulator.

  2. Select "Log" tab.

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

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


  5. Select "Application" tab.

  6. Type script as follows.

      
    clear
    
    buff = Util.CreateArrayByte(3)
    buff[0] = 0x20
    buff[1] = 0x21
    buff[2] = 0x22
    
    COM3.Write(buff)
    
      


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



  8. Select "Log" tab.

  9. Check the hex format data from the log list.