Hi All,
I am trying to receive data across the serial port. This is my first attempt and it works ok. I haven't gone down the Cattlerustler route just yet as i've tried to do it using Kernel32 instead. I'm going to try Cattlerustlers DBP_SerialPortMulti_exp.dll plugin in a minute and see if it works better.
Since I couldn't find a suitable answer on the forum I thought I would post this anyway and see if anyone knew the answer.
When I read from the serial port it pauses the program for a period of a second or so - obviously not good since it is supposed to be doing other things as well.
Here is the code - much thanks for any help.
#constant c_KERNEL32 1
load dll "\windows\system32\kernel32.dll", c_KERNEL32
global g_hPort as dword : `The state of the serial port, setup or not setup, -1 means that the port could not be setup
global g_SERIALPORT_Port_Output_String$ as string : ` Used to capture the data received on the serial port as a string
#constant g_Read_Port_Timer_Max 2000
global g_Read_Port_Timer as integer : g_Read_Port_Timer = timer()
global g_TEMPINT1 as integer : g_TEMPINT1 = -1
g_hPort = SERIAL_PORT_Setup("COM7")
Main_Program()
end
function Main_Program()
exit_condition = -1
repeat
SERIAL_PORT_Read(g_hPort)
cls
print "Read Port Timer : " + str$(g_Read_Port_Timer)
print "System Timer : " + str$(timer())
print "Read Port Counter : " + str$(g_TEMPINT1)
print g_SERIALPORT_Port_Output_String$
if scancode() = 16
end
endif
until exit_condition = 1
endfunction
function SERIAL_PORT_Setup(r_CommPort$)
local l_comm_handle as integer
`g_hPort = CreateFile(r_CommPort$, GENERIC_READ || GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
`2. Open the Port
l_comm_handle = call dll(c_KERNEL32, "CreateFileA", r_CommPort$, 0x80000000, 0, 0, 3, 0x80, 0)
if l_comm_handle = -1
cls
print "Error: Could not retrieve handle for COM port. (Port in use or invalid?)"
wait key
end
endif
`3. Set up the Port
`Build comm port parameters and apply them. See: http://msdn.microsoft.com/en-us/library/aa363143%28v=VS.85%29.aspx
dcb_buf = alloc zeroed(80) `Allocate memory to create DCB structure (com port parameters)
result=call dll(c_KERNEL32,"BuildCommDCBA","baud=9600 parity=M data=8 stop=1", dcb_buf) `Build structure with selected options
free dcb_buf
endfunction l_comm_handle
function SERIAL_PORT_Read(r_hPort)
local l_ReadPort$ as string
char_buf = alloc zeroed(4+255) `dword for returned num of characters read, 255 bytes max space for character data
if timer() > g_Read_Port_Timer + g_Read_Port_Timer_Max
result = call dll(c_KERNEL32, "ReadFile", r_hPort, char_buf+4, 255, char_buf, 0) `Call readfile to get up to 255 characters from serial port
g_Read_Port_Timer = timer()
inc g_TEMPINT1, 1
endif
if peek dword(char_buf)>0 `If returned some characters
l_ReadPort$ = g_SERIALPORT_Port_Output_String$ + peek string(char_buf+4, peek dword(char_buf)) `then add them to the serial chunk data
if l_ReadPort$ <> ""
g_SERIALPORT_Port_Output_String$ = l_ReadPort$
endif
endif
free char_buf
endfunction