Page 1 of 1

Error using Serial com and widow timer

Posted: Thu Oct 27, 2022 4:54 pm
by msteffes
Hello All,
Trying to get familiar with the serial comm and streaming input data techniques. I have a feeling I am not understanding ReadSerialPortData(Com, *Buffer,availDat) . Some issue with the buffer and the "Length" parameter in the ReadSerialPortData command.
Receiving error:
ERROR: Overflow in a string memory block.

Code: Select all


Com = OpenSerialPort(#PB_Any, "COM11", 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake,1024, 1024)
If Not Com
    MessageRequester("Error", "Can not open COM port!")
    CloseSerialPort(Com)
  End
EndIf


availDat.i = 0
Buffer.s = ""
*Buffer = @Buffer


If OpenWindow(0, 0, 0, 400, 100, "Serial Read Timer Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    AddWindowTimer(0, 1, 50) 
    
     Repeat
        Event = WaitWindowEvent()
    
        If Event = #PB_Event_Timer And EventTimer() = 1
            If AvailableSerialPortInput(Com)
                availDat  = AvailableSerialPortInput(Com)
  >>> error here              ReadSerialPortData(Com, *Buffer,availDat) 
                Debug "*********Read Data************"
                Debug PeekA(*Buffer)
                Debug PeekA(*Buffer + 1)
                Debug PeekA(*Buffer + 2)
                Debug PeekA(*Buffer + 3)
                Debug PeekA(*Buffer + 4)
                Debug Buffer.s
            EndIf
      EndIf    
      
    Until Event = #PB_Event_CloseWindow
 EndIf
 
Example of incoming data packet repeated approximately every 10ms .
10 7 59 0 190

I get six packets and the program crashes with the Error.

Re: Error using Serial com and widow timer

Posted: Thu Oct 27, 2022 5:23 pm
by mk-soft
A Empty String have not a memory buffer
You must allocate memory buffer

Not testet ...

Code: Select all


Com = OpenSerialPort(#PB_Any, "COM11", 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake,1024, 1024)
If Not Com
    MessageRequester("Error", "Can not open COM port!")
    CloseSerialPort(Com)
  End
EndIf


availDat.i = 0
Buffer.s = ""
*Buffer = AllocateMemory(2048)


If OpenWindow(0, 0, 0, 400, 100, "Serial Read Timer Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

    AddWindowTimer(0, 1, 50) 
    
     Repeat
        Event = WaitWindowEvent()
    
        If Event = #PB_Event_Timer And EventTimer() = 1
            If AvailableSerialPortInput(Com)
                availDat  = AvailableSerialPortInput(Com)
                ReadSerialPortData(Com, *Buffer,availDat) 
                Debug "*********Read Data************"
                
                Buffer = PeekS(*Buffer, availDat, #PB_Ascii)
                Debug Buffer.s
            EndIf
      EndIf    
      
    Until Event = #PB_Event_CloseWindow
 EndIf
 

Re: Error using Serial com and widow timer

Posted: Thu Oct 27, 2022 5:46 pm
by msteffes
Thanks that worked.
I also tried the string assignment a with a single space > Buffer.s = " " < without the allocation and that worked as well. Using the allocation is more clear.

What I found interesting is you did not directly assign the address as I did.

your code:
Buffer.s = ""
*Buffer = AllocateMemory(2048)

My code:
Buffer.s = ""
*Buffer = @Buffer
*Buffer = AllocateMemory(2048)

Is this not needed because the name of the pointer and the buffer are the same?

Re: Error using Serial com and widow timer

Posted: Thu Oct 27, 2022 6:41 pm
by mk-soft
msteffes wrote: Thu Oct 27, 2022 5:46 pm Is this not needed because the name of the pointer and the buffer are the same?
buffer.s and *buffer are different variables

String's memory is managed internally by PB.
A variable of type String is a pointer to a string. The amount of memory required for a string is managed internally by PB and therefore the address to the string can change at any time.
You can have the address to the string given to you, but you may NOT access or write over the length of the current string directly.

Code: Select all

;-TOP

Structure ArrayOfChar
  c.c[0]
EndStructure
  
Global sVal.s
Global *pString.ArrayOfChar

; Get pointer to string, Its NIL
*pString = @sVal
Debug "Pointer to String: " + *pString


sVal = "" ; Empty String
; Get pointer to string, Its Pointer to empty String
*pString = @sVal
Debug "Pointer to String: " + *pString + " Char[0] = " + *pString\c[0]

sVal = #Empty$ ; Empty String
; Get pointer to string, Its Pointer to empty String
*pString = @sVal
Debug "Pointer to String: " + *pString + " Char[0] = " + *pString\c[0]

sVal = #Null$ ; Nothing
; Get pointer to string, Its Pointer to Nothing
*pString = @sVal
Debug "Pointer to String: " + *pString

sVal = "0123456789" ; String
; Get pointer to string, Its Pointer to String
*pString = @sVal
Debug "Pointer to String: " + *pString + " Char[0] = " + *pString\c[0]

sVal = sVal + Space(100) ; Add String
; Get pointer to string, Its NEW Pointer to String
*pString = @sVal
Debug "Pointer to String: " + *pString + " Char[0] = " + *pString\c[0]

Re: Error using Serial com and widow timer

Posted: Sat Aug 17, 2024 6:45 pm
by Erolcum
It gives the same pointer in V6.11
https://r.resimlink.com/CNfijunRxpZL.png

Re: Error using Serial com and widow timer

Posted: Sat Aug 17, 2024 7:13 pm
by PeDe
I suspect this has to do with the fact that PB reserves some memory for strings. With only one variable, there is enough free memory behind the variable so that the required memory for the variable only needs to be increased.
However, if there is a second variable that is used, for example, a new memory area will probably have to be used for the first variable.
You can test this by inserting a second variable. In my case, the addresses are then different.

Peter

Code: Select all

...
Global sVal2.s = Space(100) ; New second String.

sVal = sVal + Space(100) ; Add String
; Get pointer to string, Its NEW Pointer to String
*pString = @sVal
Debug "Pointer to String: " + *pString + " Char[0] = " + *pString\c[0]

Re: Error using Serial com and widow timer

Posted: Sat Aug 17, 2024 7:22 pm
by Erolcum
you are right but you should define globals on the top of the program :D :lol:

Re: Error using Serial com and widow timer

Posted: Sat Aug 17, 2024 8:09 pm
by infratec
Some peoble also write Procedures in the middle of the code. :cry: