ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Standard functions / Windows functions / Serial/Parallel Ports functions
  • Example: Opening a port with a timeout
Example: Opening a port with a timeout
WEBDEV - Server codeAjax This example opens a serial port and sets the timeout.
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
// Timeout 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
// Timeout 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

Last update: 05/26/2022

Send a report | Local help