Serial Port Examples

Example 1. Read a string from COM1.

result = SerialPort.Open("COM1:", 9600, 8, SP_NOPARITY, SP_ONESTOPBIT, SP_HANDSHAKE_OFF)

IF result <> 0

{

    Form.MessageBox("Error", "Could not open port", 0)

    EXIT

}

WHILE result == 0

{

    str = SerialPort.ReadString("COM1:", 10000)

  IF str <> NULL

    {

        edit_control1 = edit_control1 & str

        IF str == ""

        {

            result = -2

        }

    }

    ELSE

    {

        result = -1

    }

}

result = SerialPort.GetLastError("COM1:")

str = "Error when reading message " & result

Form.MessageBox("Exiting", str, 0)

SerialPort.Close("COM1:")

Note:

This code segment opens COM1 first and reads the string from this port. It will display any errors while opening, reading and closing COM1.