News:

Let's find out together what makes a PIC Tick!

Main Menu

Serial Formatting

Started by Dave-S, Jan 09, 2023, 02:32 PM

Previous topic - Next topic

Dave-S

Trying to send RS232 serial code to receive Data from a PylonTech Battery.
It requires it at 115200, using a USB/rs232 adapter connected to Serial commander it works OK.
The Pic I want to use is 18K47K40 at 64 MHz  and using HSerOut2 and a Rs232 to TTL adapter TTL connect to Pic and the rs232 side is sent to the Battery port, but is not working.   
The code is:
Clear BatArray
          HSerOut2 ["pwr",  13]
    Repeat
          HSerIn2 6000, RtimeoutR, [Bat_Char]       
          'SerOut PORTD.1, 84, ["Bat_Char =", Dec Bat_Char, 13]
          BatArray[Bat_Pos] = Bat_Char
          Inc Bat_Pos
    Until Bat_Pos = 80     
        For i = 0 To Bat_Pos
           SerOut PORTD.1, 84, [Dec BatArray[Bat_Pos]]
        Next

It just Times out, with the serout uncommented I get 3 bytes but make no sence and is different each time.
Is the code to slow to receive at 115200?
Any ideas why it does not work or how to test what is going on?

Thanks David

RGV250

Hi Dave,
Have you declared the baud rate as there may be a default which I am sure will not be 115200.

Bob

trastikata

Are you sure the output string from the battery is fixed exactly to 81 characters? Is it possible that the string is shorter and your loop times out before it reaches 81 characters.

Maybe there's a string end character which you can test for and exit the loop when the byte is received? 

P.s. I don't see it where you reset the loop counter?

Dave-S

Thanks for the replies.
Yes the Baud rate is set at 115200.
It returns the command sent then @ and then the data followed by $$.
It appears to be only sending 3 bytes, I get the "pwr" I send but nothing more.
I tried putting "$$" in the "Until" it comes up with compile error, Putting in a number less than the total data it should show that.
I have moved the timeout to above the sending of the array but it is not sending that, I do not know why.
    Bat_Pos = 0
    Clear BatArray
    HSerOut2 ["pwr", 13]
    Repeat
          HSerIn2 6000, RtimeoutR, [Bat_Char]        '3000, Rtimeout,
          SerOut PORTD.1, 84, ["Bat_Char = ", Bat_Char, 13]
          BatArray[Bat_Pos] = Bat_Char
          Inc Bat_Pos
      Until Bat_Pos = 36     
RtimeoutR:
        For i = 0 To Bat_Pos       'WHY IS it NOT SHOWING This
           SerOut PORTD.1, 84, [BatArray[Bat_Pos]]
        Next
        SerOut PORTD.1, 84, [13]
        SerOut PORTD.1, 84, ["Bat_Pos = ", Dec Bat_Pos, 13]
   

I get:
Bat_Char = p
Bat_Char = w
Bat_Char = r
RtimeoutR

Thanks David

trastikata

#4
In your loop change "Bat_Pos" to "i"

SerOut PORTD.1, 84, [BatArray[i]]


SerOut PORTD.1, 84, ["Bat_Char = ", Bat_Char, 13] is too slow to send the string between two consecutive incoming bytes at 115200 baud.

Add Until Bat_Pos = 36 Or Bat_Char = "$"

In your second loop change the upper boundary to Bat_Pos - 1

For i = 0 To Bat_Pos - 1



    Bat_Pos = 0
    Clear BatArray
    HSerOut2 ["pwr", 13]
    Repeat
          HSerIn2 6000, RtimeoutR, [Bat_Char]        '3000, Rtimeout,
          'SerOut PORTD.1, 84, ["Bat_Char = ", Bat_Char, 13]
          BatArray[Bat_Pos] = Bat_Char
          Inc Bat_Pos
    Until Bat_Pos = 36 Or Bat_Char = "$"     
RtimeoutR:
    For i = 0 To Bat_Pos - 1       'WHY IS it NOT SHOWING This
           SerOut PORTD.1, 84, [BatArray[i]]
    Next
    SerOut PORTD.1, 84, [13]
    SerOut PORTD.1, 84, ["Bat_Pos = ", Dec Bat_Pos, 13]

Dave-S

Thanks Great, got it working, it started displaying some info and I realised the array and "i" was not large enough.  Don't know how I missed the "i" in the For.

Thank you
David

keytapper

In my POV the 115200 has a big drift error to the wanted baud rate, I think it's 3.5%. The 57600 is much better. So to center a bit in such narrow time becomes pretty difficult and prone to reading garbage.
This is a story that's lasting 150 years, the CPU/MCU clocks are non convenient to use particular speed to accomplish with a multiple of 30, which was the original baud rate.
Ignorance comes with a cost

Mapo

I used 64MHz pic with baud rate up to 560Kbps which I convert to 115Kbps without problems, using pic hardware