how to read a string from the serial port

Just starting out? Need help? Post your questions and find answers here.
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

how to read a string from the serial port

Post by ludoke »

Problem :how to read the incoming string from the arduino??
I have received always chinese chars !!!
Arduino code this work with the terminal from arduino
send a string with # as endmarker ,arduino answer with the string I send
// Variabelen definieren
String TotaleString;
String EindeKarakter;
void setup() {
Serial.begin(9600); // opens the seriele port, 9600 bps.
}
void loop() {
If (Serial.available() >0) { // if data received
char InkomendeKarakter = Serial.Read(); // read the received byte
TotaleString += InkomendeKarakter; // make a string
EindeKarakter += InkomendeKarakter; // geef inkomende byte een string zodat deze gecontroleerd kan worden.

If (EindeKarakter == "#") { // if # replace it to a return
TotaleString.replace('#', '\r');
Serial.println("Ik heb ontvangen: " + TotaleString); // send the received string back to the PC
; }
TotaleString = ""; //clear the string
EindeKarakter = ""; // Zet de check van het "einde" karakter weer op nul
}
;--------------------------------------------------------------------------------------------------------------------

Code: Select all

Enumeration
  #win
  #port
  #zenden
  #ontvangen
  #invoer
EndEnumeration
Global EventID.l
Global Serial_in.s="                   ";received string
Global Serial_out.s  ;transmitted string with # endmarker for arduino
Global data_in.i   ;length of data in serialbuffer
Global Quit.b
Global Verbonden.b=0  ;1 if connected
Global Port$         ;port for arduino
;-----------------------------------------------------------------
Declare zenden()
Declare find_port()
;---------------------------------------------------------------------------------------
OpenWindow(#win,0,0,500,324,"serial test",#PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget) 

ButtonGadget(#zenden,22,25,120,30,"zend") 
TextGadget(#ontvangen,22,250,300,30,"ontvang:", #PB_Text_Center | #PB_Text_Border)
StringGadget(#invoer,22,180,300,30,"input#") ;input a string with # endmarker

find_port()   ;look where the arduino is
       Debug port$
    OpenSerialPort(#port, port$ , 9600,#PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1, 1) ;reopen port
Repeat                                          
  EventID = WaitWindowEvent()   
If EventID = #PB_Event_Gadget   
    Select EventGadget()
        Case #zenden    ;button zenden pressed
          zenden()
           Data_in= AvailableSerialPortInput(#port)  ;anwers from the arduino
            If data_in > 0        
               Serial_in = Space(data_in)    ;                                                                  
               ReadSerialPortData(#port, @serial_in,data_in)
         ;"@serial_in " memoryadres where "serial_in" string with length "data_in" is this wright ?
               ;temp$ = PeekS(@serial_in, data_in)  ; 
               temp$=serial_in
             
              ; SetGadgetText(#ontvangen,temp$)
               SetGadgetText(#ontvangen,serial_in)
               
               ;question :how to get serial_in  ????    
               ;i have received always chinese chars !!!  
               
              ; For n=0 To data_in
               ;  temp$= Chr(@serial_in +n)
               ;  Next
               EndIf  
               Debug temp$
          Debug verbonden          
    EndSelect           
EndIf

If EventID = #PB_Event_CloseWindow           
  Quit = 1                                   
EndIf                                      
Until Quit = 1                               
CloseSerialPort(#port)  
End
;------------------------------------------------------------------------------------------
Procedure find_port()
  n.i 
  port.s
  res.i
For Port_nr = 6 To 7  ;tet only 6 and 7
  Port = "COM"+Str(port_nr)     
   OpenSerialPort(#port, port , 9600,#PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1, 1) 
    If IsSerialPort(#port)<>0                   ;is port ok
       WriteSerialPortString(#port,"1#")        ;write something
       Delay(100)
       Data_in = AvailableSerialPortInput(#port)            ;test data        
       If data_in > 0          
          Serial_in = Space(data_in)                                                                      
          ReadSerialPortData(#port, @serial_in,data_in)               ;read data ,length
             Verbonden = 1
             serial_in = " "
              Port$ = "COM"+Str(port_nr)
             Break         
        EndIf            
     CloseSerialPort(#port)
EndIf
Next
EndProcedure
;------------------------------------------------------------------------------------------
Procedure zenden()

serial_out.s=GetGadgetText(#invoer)
WriteSerialPortString(#port,serial_out)     ; 
Delay(100)
EndProcedure
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: how to read a string from the serial port

Post by kenmo »

Try

Code: Select all

temp$ = PeekS(@serial_in, data_in, #PB_Ascii)

Code: Select all

DataSection
  AsciiString:
  Data.a 'H', 'e', 'l', 'l', 'o', '!' ; text in 8-bit ASCII format
  AsciiEnd:
EndDataSection

Debug PeekS(?AsciiString, ?AsciiEnd - ?AsciiString, #PB_Ascii)
Debug PeekS(?AsciiString, ?AsciiEnd - ?AsciiString) ; by default, expects 16-bit Unicode characters
ludoke
Enthusiast
Enthusiast
Posts: 153
Joined: Fri Jul 08, 2016 5:35 pm
Location: Essen (Belgium)

Re: how to read a string from the serial port

Post by ludoke »

thanks,it works now
Post Reply