To read data from a barcode scanner on a serial COM port using Windows API here is a shortened example:
// you need to declare some external API functions// and some structures// it is nothing complex - just simple google for it
// open the COM portCONSTANT long EV_RXCHAR = 0
CONSTANT long GENERIC_READ = 2147483648
CONSTANT long GENERIC_WRITE = 1073741824
CONSTANT long FILE_SHARE_NONE = 0
CONSTANT long OPEN_EXISTING = 1
CONSTANT long PURGE_TXABORT = 1
CONSTANT long PURGE_RXABORT = 1
CONSTANT long PURGE_TXCLEAR = 1
CONSTANT long PURGE_RXCLEAR = 1
CONSTANT long FF_OVERLAPPED = 0
if li_comPort = 1 then
ls_port = "COM1:"
ls_port_description = "COM1: 9600,N,8,1"
elseif li_comPort = 2 then
ls_port = "COM2:"
ls_port_description = "COM2: 9600,N,8,1"
elseif li_comPort = 3 then
ls_port = "COM3:"
ls_port_description = "COM3: 9600,N,8,1"
elseif li_comPort = 4 then
ls_port = "COM4:"
ls_port_description = "COM4: 9600,N,8,1"
else
li_comPort = 0
messagebox("Port Error","An invalid com port was passed to the open port function")
end if
setnull(ll_null)
li_comPort = CreateFileW(ls_port, GENERIC_READ, FILE_SHARE_NONE, ll_null, OPEN_EXISTING, FF_OVERLAPPED, ll_null)
IF li_comPort < 0 THEN
// error
else
// Set the port variables
ll_return_cd = BuildCommDCBW(ls_port_description,str_DCB)
if ll_return_cd = 0 then
// error
else
ll_return_cd = SetCommState(li_comPort,str_DCB)
end if
// Setup the Comm mask
if SetCommMask(li_comPort, EV_RXCHAR) = FALSE then
// error
end if
// Setup the buffers
if SetupComm(li_comPort, 4096, 4096) = FALSE then
// error
end if
// Flush the Port's buffer
if FlushFileBuffers(li_comPort) = FALSE then
// error
end if
// Purge the Port
if PurgeComm(li_comPort, PURGE_TXABORT + PURGE_RXABORT + PURGE_TXCLEAR + PURGE_RXCLEAR) = FALSE then
// error
end if
// Setup Timeouts
SetComTimeout(1500)
end if
MessageBox ("Information", "Scan now!")
// readls_buffer = blob (space(ll_read_length))
lst_overlapped.internal = 0
lst_overlapped.internalhigh = 0
lst_overlapped.offset = 0
lst_overlapped.offsethigh = 0
lst_overlapped.hevent = 0
lb_return = ReadFile(li_comPort, lblob_buffer, ll_read_length, ll_bytes_read, lst_overlapped)
ls_text = string (lblob_buffer, EncodingAnsi!)
// closeCloseHandle(li_comPort)