;Program that read an analog voltage from an arduino with serial port ;the arduino send 4bytes without CR +LF Enumeration #Win #Port #Sec #text #font EndEnumeration ;-------------------------------------------------------------- Port$ = 'COM7' ;depends on computor ;------------------------------------------------------ Declare Read_Arduino() ;this work Declare Read_Arduino1();this do not work ;------------------------------------------------------------------------- OpenWindow(#Win, 0, 0, 500, 300, ', #PB_Window_SystemMenu | #PB_Window_ScreenCentered) LoadFont(#font,'caladea',14, #PB_Font_Italic|#PB_Font_Bold) TextGadget(#text,100,100,550,40,' ') SetGadgetFont(#text,FontID(#font)) AddWindowTimer(#Win, #Sec, 500) ;read every 500 ms OpenSerialPort(#Port, Port$, 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 64, 64) Delay(1000) ;wait to init the port Repeat Event = WaitWindowEvent() If Event = #PB_Event_Timer And EventTimer() = #sec ;if timer event read arduino Read_Arduino1() EndIf Until Event = #PB_Event_CloseWindow ;--------------------------------------------------------------- Procedure Read_Arduino1() ;this drive me crazy,I can not understand pointers ptr.i=0 ;counter recv.l C.c k.i Repeat While AvailableSerialPortInput(#port) >0 ReadSerialPortData(#port, @Recv + Ptr, 1); @recv =adres to place the data ,I suppose ? Ptr + 1 ;incr the adres to put the next char Wend Until Ptr = 4 ;now all chars are read Debug recv ;nothing show in the debugwindow ? ;How to read the data that start at adres @recv ? For n=0 To 3 ;c=PeekC(recv) ;nothing ;c=PeekC(*recv+n);nothing ; K=recv+n ;nothing ;text=*(recv+n) ;I try so many ohter things but I do not understand it do not work ;Why is it not possible the get the chars from adres @recv ? text=text+c Next SetGadgetText(#text,'VOLTAGE ='+ text + ' V') EndProcedure ;----------------------------------------- Procedure Read_Arduino() ;this works fine buffer.b text.s L.i ;length of the text text=' While AvailableSerialPortInput(#port) > 0 If ReadSerialPortData(#port, @Buffer, 1) ; Read Byte Text = Text + Chr(Buffer) EndIf Wend l=Len(text) text=Left(text,4) ;show only 4 chars SetGadgetText(#text,'VOLTAGE ='+ text + ' V') EndProcedure End
|