Error using Serial com and widow timer

Just starting out? Need help? Post your questions and find answers here.
msteffes
New User
New User
Posts: 6
Joined: Thu Oct 27, 2022 4:35 pm

Error using Serial com and widow timer

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Error using Serial com and widow timer

Post 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
 
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
msteffes
New User
New User
Posts: 6
Joined: Thu Oct 27, 2022 4:35 pm

Re: Error using Serial com and widow timer

Post 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?
User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Error using Serial com and widow timer

Post 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]
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Erolcum
User
User
Posts: 51
Joined: Fri Jun 07, 2024 10:45 am
Location: Turkiye
Contact:

Re: Error using Serial com and widow timer

Post by Erolcum »

It gives the same pointer in V6.11
https://r.resimlink.com/CNfijunRxpZL.png
You may visit my new Purebasic blog here..
:arrow: https://erolcum-github-io.translate.goo ... r_pto=wapp
PeDe
Enthusiast
Enthusiast
Posts: 278
Joined: Sun Nov 26, 2017 3:13 pm

Re: Error using Serial com and widow timer

Post 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]
User avatar
Erolcum
User
User
Posts: 51
Joined: Fri Jun 07, 2024 10:45 am
Location: Turkiye
Contact:

Re: Error using Serial com and widow timer

Post by Erolcum »

you are right but you should define globals on the top of the program :D :lol:
You may visit my new Purebasic blog here..
:arrow: https://erolcum-github-io.translate.goo ... r_pto=wapp
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Error using Serial com and widow timer

Post by infratec »

Some peoble also write Procedures in the middle of the code. :cry:
Post Reply