RS-232 receive data

Just starting out? Need help? Post your questions and find answers here.
europa81
User
User
Posts: 24
Joined: Tue Oct 20, 2015 11:42 pm
Location: Southern Germany

Re: RS-232 receive data

Post by europa81 »

Ok, sorry: RTFM - the "*" stands for pointer. While I still do not understand why the name of an AllocatedMemory should be a pointer instead of a name of the allocated memory. Anyway, I used NewList in order to save the data received. And it works 50 % but there are two reasons why it should not work.

Code: Select all

EnableExplicit

#WindowColor=$F0F0F0

Procedure rs232()
 
  Protected RxByte.b
  NewList uavdata.b()
  
  While AvailableSerialPortInput(0)
    If ReadSerialPortData(0,@RxByte,1) = 1
      AddElement(uavdata())
    uavdata()=RxByte 
    EndIf
  Wend
  ForEach uavdata()
    SetGadgetState(1,uavdata())
    Debug uavdata()
        Next
EndProcedure

Define flags = #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
OpenWindow(0,0,0,450,350,"RS-232",flags)
WindowBounds(0,150,150, #PB_Ignore, #PB_Ignore)
SetWindowColor(0,#WindowColor)
TextGadget(0,5,10,48,20, "rx-data:", #PB_Text_Center)
TrackBarGadget (1, 55, 10, 305, 30, 0, 255)
If OpenSerialPort(0,"COM17", 57600,#PB_SerialPort_NoParity,8,1,#PB_SerialPort_NoHandshake,512,512)=0
  MessageRequester("ComPortError","ComPort not initialized")
  End
  EndIf
AddWindowTimer(0,0,50)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Timer: rs232()
    Case #PB_Event_CloseWindow:CloseSerialPort(0):End
  EndSelect
ForEver
The µController is sending Printbin 88;x; whereas the 88 stands for "X" and the variable (byte) X contains the Accelerometer value from a MPU9250 sensor which is read by the µController through I2B bus. Weird enough: RxByte receives negative values (how that - being a byte ??) and the "88" should actually be displayed by the Trackbar (it should flicker between 88 and the x-value), but it doesn't !

Now the compiler is happy, but me not !

<Thomas>
europa81
User
User
Posts: 24
Joined: Tue Oct 20, 2015 11:42 pm
Location: Southern Germany

Re: RS-232 receive data

Post by europa81 »

Ok, I figured parts of it:
(1) in PurBasic Bytes are -127 ~ 128. Go figure ! (different in other Basics). Ascii needs to be used.
(2) the Trackbar probably jumps to 88, but that happens so fast that it is not visible.
cheers
<Thomas>
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RS-232 receive data

Post by infratec »

Hi,

Code: Select all

EnableExplicit

#WindowColor=$F0F0F0

Procedure rs232()
  
  Protected RxByte.a
  NewList uavdata.a()
  
  While AvailableSerialPortInput(0)
    If ReadSerialPortData(0,@RxByte,1) = 1
      AddElement(uavdata())
      uavdata()=RxByte
      SetGadgetState(1, RxByte)
    EndIf
  Wend
EndProcedure

Define flags = #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget
OpenWindow(0,0,0,450,350,"RS-232",flags)
WindowBounds(0,150,150, #PB_Ignore, #PB_Ignore)
SetWindowColor(0,#WindowColor)
TextGadget(0,5,10,48,20, "rx-data:", #PB_Text_Center)
TrackBarGadget (1, 55, 10, 305, 30, 0, 255)
If OpenSerialPort(0,"COM17", 57600,#PB_SerialPort_NoParity,8,1,#PB_SerialPort_NoHandshake,512,512)=0
  MessageRequester("ComPortError","ComPort not initialized")
  End
EndIf
AddWindowTimer(0,0,50)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Timer: rs232()
    Case #PB_Event_CloseWindow:CloseSerialPort(0):End
  EndSelect
ForEver
You already figuered out that .a is needed :D

But than you mad a fallback to DOS and microcontroller way of thinking.
In windows and other multitasking OSs everything is event triggered.

In your case this means:
SetGadget(1, x) does not set the value immediately.
It shows the value first when the eventloop is done.
In the current way of use it only refreshes the internal value of the gadget
and when the eventloop is proccessed it shows the last value.
(maybe also some values in between, but that's only by luck)

One possible solution:
Use the communication in an own thread :wink:
And wait after SetGadgetState() that the main loop had showed it (Semaphore).
Or better let the mainloop show the value with a PostEvent.

Bernd
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RS-232 receive data

Post by infratec »

Example:

Code: Select all

EnableExplicit


CompilerIf Not #PB_Compiler_Thread
  MessageRequester("Info", "Enable Thread-Safe in compiler options!")
  End
CompilerEndIf

Enumeration #PB_Event_FirstCustomValue
  #Com_Event_RxBytes
  #Com_Event_TxBytes
  #Com_Event_RxComplete
  #Com_Event_Exit
EndEnumeration




Structure ComParameterStructure
  COM$
  Thread.i
  Semaphore.i
  Exit.i
EndStructure


Procedure ComThread(*Parameter.ComParameterStructure)
 
  Protected.a Byte
  Protected.i Port, RxLen, RxBytes, State
  Protected Rx$
 
  Port = OpenSerialPort(#PB_Any, *Parameter\COM$, 1200, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 128, 128)
  If Port
   
    Repeat
     
      RxLen = AvailableSerialPortInput(Port)
      If RxLen
        While RxLen
          If ReadSerialPortData(Port, @Byte, 1) = 1
            RxBytes + 1
            PostEvent(#Com_Event_RxBytes, 0, 0, Byte, RxBytes)
            WaitSemaphore(*Parameter\Semaphore)
            
            Debug Byte
            
          EndIf
          
          RxLen - 1
          
        Wend
      Else
        Delay(3)
      EndIf
     
    Until *Parameter\Exit
   
    CloseSerialPort(Port)
   
  EndIf
 
  PostEvent(#Com_Event_Exit)
 
EndProcedure




Define.i Event, Exit
Define ComParameter.ComParameterStructure

OpenWindow(0, 0, 0, 640, 180, "Com - Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)

ButtonGadget(0, 10, 10, 80, 25, "Connect")

TrackBarGadget (1, 55, 70, 512, 30, 0, 255)

CreateStatusBar(0, WindowID(0))
AddStatusBarField(100)
AddStatusBarField(100)


StatusBarText(0, 0, "disconnected", #PB_StatusBar_Center)
StatusBarText(0, 1, "Rx: 0", #PB_StatusBar_Center)

ComParameter\COM$ = "COM3"
ComParameter\Semaphore = CreateSemaphore()

Repeat
 
  Event = WaitWindowEvent()
 
  Select Event
    Case #Com_Event_RxBytes
      StatusBarText(0, 1, "Rx: " + Str(EventData()), #PB_StatusBar_Center)
      SetGadgetState(1, EventType())
      SignalSemaphore(ComParameter\Semaphore)
      
    Case #Com_Event_Exit
      ComParameter\Exit = #False
      SetGadgetText(0, "Connect")
      StatusBarText(0, 0, "disconnected", #PB_StatusBar_Center)
     
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
          If IsThread(ComParameter\Thread)
            ComParameter\Exit = #True
          Else
            ComParameter\Thread = CreateThread(@ComThread(), @ComParameter)
            If ComParameter\Thread
              SetGadgetText(0, "Disconnect")
              StatusBarText(0, 0, "connected", #PB_StatusBar_Center)
            EndIf
          EndIf
      EndSelect
     
    Case #PB_Event_CloseWindow
      If IsThread(ComParameter\Thread)
        ComParameter\Exit = #True
        If WaitThread(ComParameter\Thread, 1000) = 0
          KillThread(ComParameter\Thread)
        EndIf
      EndIf
      FreeSemaphore(ComParameter\Semaphore)
      Exit = #True
     
  EndSelect
 
Until Exit
(A modified version from the listing on page 1)

Bernd
Post Reply