News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

HSerin Problem

Started by superpro, Jun 29, 2022, 02:35 AM

Previous topic - Next topic

superpro

IM USED, HRSin {10000 , All_On}, Char  ' Get a byte from serial port
          If Char =  "A"  Then PLAY
          DelayMS 50
BUT I USED If Char =  "AB"  Then PLAY............IS ERROR

WHAT IS PROBLEM?

JonW

You are only reading a single byte. To have multiple bytes (AB will be 2 bytes) then you need to create a buffer

keytapper

You should find the figure on the manual. When you expect for a string, it would be better to use the wait modifier. IIRC there could be also a timeout label to jump to, just in case you won't like to get the program stuck forever.
Unless that data will be used later in the process, so as JONW proposing, you'll need to collect the incoming byte in a buffer.
Ignorance comes with a cost

superpro


hello, how to establish a buffer, you will have an example.

top204

In your post, you have not given what device you are using or what the variable types are, so I am assuming you are using a standard 14-bit core type, with "Char" being a Byte variable.

The characters "AB" will not fit into a single Byte variable, and they are actually two seperate 8-bit, ASCII, values, and your program is only receiving a single byte into "Char", so it will never work, and the compiler will give an error message.

Below is a simple demo that shows how to receive 2 bytes, using standard variables and not arrays or Strings, and comparing them:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate how to receive two bytes from USART1 and compare them
' Written for the Positron8 compiler by Les Johnson
'
    Device = 16F877                             ' Tell the compiler what device is being compiled for
    Declare Xtal = 8                            ' Tell the compiler what speed the device will be operating at
'
' Setup USART1
'  
    Declare Hserial_Baud = 9600
    Declare HRSIn_Pin = PORTC.7
    Declare HRSOut_Pin = PORTC.6
'
' Create variables for the demo
'
    Dim bChar1 As Byte
    Dim bChar2 As Byte

'-----------------------------------------------------------------------
' The main program starts here
'
Main:
    HSerIn 2000, TimedOut, [bChar1, bChar2]         ' Receive 2 bytes from USART1, with a 2 second timeout to give time for the characters to be typed into a serial terminal
    If bChar1 = "A" And bChar2 = "B" Then           ' Are the two bytes containing what were expected?
        HSerOut ["Received characters A and B OK"]  ' Yes. So display an OK message
    Else                                            ' Otherwise...
        HSerOut ["Received Incorrect Characters"]   ' Display an error message
    EndIf
    Stop                                            ' Stop the program here, because the demo has done its job
'
' Jump here if the serial timed out
'
TimedOut:
    HSerOut ["Timed Out"]                           ' Send a "Timed Out" message to the serial terminal
    Stop

Run the above program and type in the characters "A" and "B" on the serial terminal and it will give the responding messages back on the serial terminal.

To do a comparison of: "If MyVar = "AB" Then", the "MyVar" variable will need to be a String type and received into it from the HSerin or HRsin commands.

It is very important to give more details in code snippets when asking for assistance, otherwise, it is just "best guesses" for the answers given, or, sometimes, answers will not be given, because people do not actually know what has been asked! :-) I am a strong believer in "An incorrect answer is worse than no answer at all", because it puts people on a different route that will lead to nowhere and give misleading results and opinions. You just have to look at the dreadful history of humans to see the effect of "guesses that are stated as facts, because the true answer was not known".


top204

#5
The program listing below shows one method of receiving ASCII characters and loading them into a String variable that will be terminated when it receives an ASCII control character (0 to 13).

String variables are extremely useful and extremely efficient in the Positron compilers. However, because of the very fragmented RAM in the old standard 14-bit core devices, and the "lack" of RAM in them, String variables are only supported with PIC18F devices, enhanced 14-bit core devices and PIC24 and dsPIC33 devices. And there are lots and lots and lots of them around. :-)

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Demonstrate a procedure that receives ASCII characters from USART1
' In the serial terminal, type the letters "ab" and hit the return button to send a Carriage Return (13)
' This will terminate the String and it can be compared
'
' Written for the Positron8 compiler by Les Johnson.
'
    Device = 18F25K20                               ' Tell the compiler what device to compile for
    Declare Xtal = 16                               ' Tell the compiler what frequency the device will be operating at
'
' Setup USART1 for the HRSin/HSerin and HRSout/HSerout commands
'
    Declare Hserial_Baud = 9600
    Declare HRSOut1_Pin  = PORTC.6
    Declare HRSIn1_Pin   = PORTC.7 
'
' Create a variable for the demo
'
    Dim Global_MyString As String * 20 Heap             ' Holds the ASCII text received via USART1
 
'--------------------------------------------------------------------
' The main program starts here
' Receive ASCII values from a serial terminal and compare what is received
'
Main:
    If String_Get() = 1 Then                            ' Was the String received without problems?       
        If Global_MyString = "ab" Then                  ' Yes. So are the received characters what were expected?
            HRSOutLn "Received ab OK"                   ' Yes. So display an OK message
        Else                                            ' Otherwise...
            HRSOutLn "Error. Incorrect Characters: ",   ' \ Display an error message
                     Global_MyString                    ' / And the characters received
        EndIf
    Else                                                ' Otherwise. The procedure returned false (0)
        HRSOutLn "Error. Timed Out"                     ' So transmit an error message
    EndIf
   
'--------------------------------------------------------------------
' Receive ASCII text via USART1 and fill a String with the characters.
' Exit when a terminator is received.
' Input     : None
' Output    : Loads the global variable "Global_MyString" with the data received
'           : Returns 0 if the serial timed out or overflowed the String, else returns 1 for an OK receive
' Notes     : A terminator is any value under 14. i.e. An ASCII Control Character
'           : Has a 3 second timeout to give time for the characters to be typed in a serial terminal
'
Proc String_Get(), Bit
    Dim bIndex As Byte                                  ' Create a variable to act as an index to the String's elements
    Dim bChar As Byte                                   ' Create a byte variable to hold the value received
       
    bIndex = 0                                          ' Start at element 0 in the String variable to load into
    Repeat                                              ' Create a loop to receive characters
        HSerin 3000, TimedOut, [bChar]                  ' Receive a byte in bChar with a 3 second timeout
        If bChar <= 13 Then                             ' Is the Byte an ASCII terminator?
            Result = 1                                  ' Yes, So return a 1 result, for OK
            Global_MyString[bIndex] = 0                 ' Add a null to terminate the String "Global_MyString"
            ExitProc                                    ' Exit the procedure
        EndIf
        Global_MyString[bIndex] = bChar                 ' Load the character received into "Global_MyString"
        Inc bIndex                                      ' \
    Until bIndex > Bound(Global_MyString)               ' / Move up the String, but do not overflow it
TimedOut:                                               ' Jump here if the serial timed out
    Result = 0                                          ' Give a return of 0 (false)
    Clear Global_MyString                               ' Clear the String "Global_MyString", so it does not have any characters in it
EndProc

The String_Get procedure could have a few changes made to it, so it returns the String loaded via USART1, instead of a global String variable, or uses ByRef in a parameter and loads the String sent to it as the parameter, but the above method is simpler to understand and follow, and does its job well.

Also, even though the String_Get() procedure looks quite large, it produces the assembler code listed below, which is very fast and compact for what it does:
String_Get
    clrf String_GetbIndex,0
_lbl__19
    movlw 11
    movwf GENH,0
    movlw 184
    movwf GEN,0
    rcall __hrsin1_with_timeout__
    bnc String_GetTimedOut
    movwf String_GetbChar,0
    movlw 14
    cpfslt String_GetbChar,0
    bra _lbl__23
    bsf _B__VR1,0,0
    clrf PRODL,0
    lfsr 0,Global_MyString
    movf String_GetbIndex,W,0
    movff PRODL,PLUSW0
    return 0
_lbl__23
    lfsr 0,Global_MyString
    movf String_GetbIndex,W,0
    movff String_GetbChar,PLUSW0
    incf String_GetbIndex,F,0
    movlw 18
    cpfsgt String_GetbIndex,0
    bra _lbl__19
String_GetTimedOut
    bcf _B__VR1,0,0
    lfsr 0,Global_MyString
    movlw 21
_pblb__31
    clrf POSTINC0,0
    decfsz WREG,F,0
    bra _pblb__31
    return 0


Giuseppe

#6
Great examples Top204 thanks for your sharing

superpro

MUCHAS GRACIAS TOP204, les comento que tengo un sistema de radio frecuencia pero requería darle mas peso a la comunicación, con 2 byte, hace muchos años la tenia pero ahora estoy retomando algunos proyectos con proton.
se agradece a nuestro amigo, por la respuesta. dejo las pruebas.

Include "Proton_4.Inc"

    Dim bChar1 As Byte
    Dim bChar2 As Byte               
    Clear bChar1
    Clear bChar2
    DelayMS 100
    TRISB = 0
    TRISA = 0
           
     All_Digital = TRUE   
Loop1:
HSerIn 2000, TIMEDOUT, [bChar1, bChar2]         ' Receive 2 bytes from USART1, with a 2 second timeout to give time for the characters to be typed into a serial terminal
If bChar1 = "#" And bChar2 = "A" Then PLAY
    GoTo Loop1                       ' Do it all over again

TIMEDOUT:
'    PORTB = 7                       ' Error - no character received
''    DelayMS 500                     ' Blink all LEDs
''    PORTB = 0
''    DelayMS 500
    GoTo Loop1
   
PLAY:
PORTA.0=0
DelayMS 100
PORTA.0=1
DelayMS 10
GoTo Loop1