PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • Example: Opening a port with a time-out
Example: Opening a port with a time-out
WINDEVWEBDEV - Server codeWindows MobileAjaxUser code (UMC) This example is used to open a serial port and to manage the time-out.
PortNum is int = 1 // Port number
// String to write onto the port
StringToWrite is string = "String to write"
SizeWritten is int // Size of the string that was written
SizeEntryBuffer is int = 5000 // Size of buffer for the read operations
SizeOutputBuffer is int = 5000 // Size of buffer for the write operations
// Time-out of sRead/sWrite between two characters (in ms)
PortTimeout is int = 100

// Open the port
IF sOpen(PortNum, SizeEntryBuffer, SizeOutputBuffer, PortTimeout) = False THEN
// Process the opening error
Error("Error while opening COM" + PortNum)
ELSE
  // Configure the port
IF sParameter(PortNum, 9600, 0, 8, 0, True, True, True) = False THEN
// Process the configuration error
Error("Error while configuring COM" + PortNum)
ELSE
LOOP
  // Write onto the port
  SizeWritten = sWrite(PortNum, StringToWrite)
  // Check the write operation
IF SizeWritten = 0 AND StringToWrite <> "" THEN
// Nothing was written while the string to write is not empty
// Check the output buffer
IF sInExitQueue(PortNum) < SizeOutputBuffer THEN
// The output buffer is not full: device problem
IF YesNo("Unable to write onto the port" + ...
 "COM" + PortNum,...
 "Check whether a device is ready" + ...
 "and properly connected.", ...
 "Do you want to retry?") = False THEN
BREAK // Cancel the write operation
END
ELSE
// The output buffer is full
// Time-out in order for the output buffer to be cleared
Multitask(200)
// Check the output buffer again
IF sInExitQueue(PortNum) >= SizeOutputBuffer THEN
// The output buffer is full
IF YesNo("Unable to write onto the port" + ...
  "COM" + PortNum, ...
  "Check whether a device is ready" + ...
  "and properly connected.", ...
  "Do you want to retry?") = False THEN
  BREAK // Cancel the write operation
END
END
  END
ELSE
IF SizeWritten = Length(StringToWrite) THEN
// Everything was written
BREAK
// Otherwise, only a part was written
// But we wrote something, so let's try again
END
END
END
END
  // Close COM1
  sClose(PortNum)
END
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment