I am trying to get data from the serial port. So far I have tried using Kernel32 and the Cattlerustler plugin.
The data I am receiving comes in the form of a channel number (i.e 01) followed by a record number (i.e 11) followed by a value of
unknown length
I tried the kernel32 method from the windows library first using code like this:
l_comm_handle = call dll(c_Kernel32DLL, "CreateFileA", r_CommPort$, 0x80000000, 0, 0, 3, 0x80, 0)
l_mem_DCB_Buffer = alloc zeroed(80)
result = call dll(c_Kernel32DLL,"BuildCommDCBA","baud=9600 parity=M data=8 stop=1", l_mem_DCB_Buffer)
free l_mem_DCB_Buffer
[set timeouts - all working fine, nothing to see here]
char_buf = alloc zeroed(604)
result = call dll(c_Kernel32DLL, "ReadFile", r_hPort, char_buf + 4, 600, char_buf, 0)
The problem is that the data on the serial port is of an unknown length. That code requires me to set the length (in the example the length is set arbitrarily at 600).
I don't think I can use that method without knowing the number of characters of data waiting to be picked up.
2. That is a pain since it is my preferred method. I found that if I grab the result from that kernel32 method and save the string to a txt file all the carriage returns between the records and channels are respected and I keep all the data structure - i.e I don't have to write a function to try and work out the length of the data between the channel and record numbers.
3. So becuase that didn't work I opted to try Cattlerustlers plugin.
Using this, and not knowing the amount of data on the port I opted to to read each character and add it to a string then look for the start of file and end of file markers (&& and !! respectively). I then wrote the code to work out the position of all the channels and records relative to their sequence in the data.
The very big problem is that the data I read from the port occasionally has missing characters.
I set it up like so:
global g_SETPORT_BaudRate as integer : g_SETPORT_BaudRate = 9600
global g_SETPORT_DataBits as integer : g_SETPORT_DataBits = 8
global g_SETPORT_Parity as integer : g_SETPORT_Parity = 0
global g_SETPORT_Stopbits as integer : g_SETPORT_Stopbits = 1
SPM_AddPort r_CommPort$, g_SETPORT_BaudRate, g_SETPORT_DataBits, g_SETPORT_Parity, g_SETPORT_Stopbits
g_SERIALPORT_ACTIVE = 1
g_SERIALPORT_ACTIVE_ID$ = r_CommPort$
global g_SETPORT_HandShake_Constant as integer : g_SETPORT_HandShake_Constant = 0
global g_SETPORT_Receive_Buffer_Size as integer : g_SETPORT_Receive_Buffer_Size = 4096
global g_SETPORT_Write_Buffer_Size as integer : g_SETPORT_Write_Buffer_Size = 2048
global g_SETPORT_RequestToSend_Enable as integer : g_SETPORT_RequestToSend_Enable = 0
global g_SETPORT_Read_TimeOut as integer : g_SETPORT_Read_TimeOut = 50
global g_SETPORT_Write_TimeOut as integer : g_SETPORT_Write_TimeOut = 50
global g_SETPORT_ReceiveBytesThreshold as integer : g_SETPORT_ReceiveBytesThreshold = 1
global g_SETPORT_DataTerminalReady as integer : g_SETPORT_DataTerminalReady = 0
SPM_ConfigurePort r_CommPort$, g_SETPORT_HandShake_Constant, g_SETPORT_Receive_Buffer_Size, g_SETPORT_Write_Buffer_Size, g_SETPORT_RequestToSend_Enable, g_SETPORT_Read_TimeOut, g_SETPORT_Write_TimeOut, g_SETPORT_ReceiveBytesThreshold, g_SETPORT_DataTerminalReady
SPM_Openport r_CommPort$
I cannot use this method if I am reasonably regularly (like on in every 60 characters) missing one. When it occurs in the channel and record section I have no way of knowing where the data starts and stops between them.
Anyway, my request is this - please can anyone help with either method.
I need help either with the code in DB to read how many characters are ready to be retrieved using Kernel32 and "readfile", or help with what I am doing wrong in setting up the port. Cattlerustlers plugin allows the timeouts to be set as well as the receive threshold, read buffer size etc.
On the sending system the only options are baud, databits, parity and stopbits. The flow control on the sending system is set to hardware.
Thats all I have, and i've been on it now for 2 days solid. My project has hit a brick wall until I solve this puppy.
Much thanks for any help.