Notifications
Clear all

[Solved] Hello from Northumberland, UK

42 Posts
6 Users
23 Reactions
2,003 Views
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1123
 

@swarlandrb

Well I have had a nice success,  no results for your serial comms as yet, but progress along that path.  I usually use the Thonny IDE to program the picos and even though the preference setting was set to allow multiple instances this has never worked on my mac.  ( I expect it works on PC's)

Spurred by wanting to have 2 pico's and 2 Thonny sessions working to check the serial comms it prompted me to actually google as to why I could not get 2 sessions.  Well I found there is a workaround (though no fix) in that all that is required is to activate the second session via the command line.  I'm well pleased I appear to have got that working, and many thanks for that motivation to solve it 😋 .

As you probably know its very possible when using uart serial to end up with blocking code whilst waiting for serial data to arrive if one is not careful how its coded.  We may have to take into account how the other code you have produced runs, but first up I hope just small demo programs sending and receiving data will get the ball rolling.  Its evening meal time shortly and I will settle down to having a play with this later.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1123
 

@swarlandrb

Here is a quick code doodle I did just to confirm my 2 picos were talking ok.  I use uart0 for both boards and pins 0 and 1 (tx rx) for both boards.  Of course don't forget that pico1 tx goes to the pico2 rx.

I also connected a wire to a ground pin on both boards.  

******************************

On pico 1 I put the following code:

----------------------------------------

from machine import UART, Pin
import time

uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))

while True:
    for x in range(20):
        uart0.write(str(x))
        print(x, 'was sent')
        while True:
            if uart0.any():
                response = uart0.read()
                if response.decode() == str(x):
                    print(response, ' data sent confirmed')
                    break
        time.sleep(5)

--------------------------------------------

***********************************

On pico 2 I put the following code

---------------------------------------------

from machine import UART, Pin
import time

uart0 = UART(0, baudrate=9600, tx=Pin(0), rx=Pin(1))

while True:
    if uart0.any():
        data = uart0.read()
        print('uart data received was', data)
        uart0.write(data)
    else:
        print('nothing found in uart')
    time.sleep(5)

------------------------------------------------

on pico 1 you should see:

0 was sent
b'0' data sent confirmed
1 was sent
b'1' data sent confirmed
2 was sent
b'2' data sent confirmed
3 was sent
b'3' data sent confirmed
etc

on pico 2 you should see:

uart data received was b'0'
nothing found in uart
uart data received was b'1'
nothing found in uart
uart data received was b'2'
nothing found in uart
uart data received was b'3'
nothing found in uart
uart data received was b'4'

 

So both programs dont wiz by too fast there are sleep times.

Of course not anywhere near the code we will end up with, but at least comms is confirmed.  So you may like to give it a go and then we can see where we should go from here.

This post was modified 2 years ago 2 times by byron

   
Lee G and SwarlandRB reacted
ReplyQuote
(@swarlandrb)
Member
Joined: 2 years ago
Posts: 11
Topic starter  

@byron 

 😀 That looks really good and is just what I am wanting.  It's too late for me to try it tonight and tomorrow is busy most of the day with a school governors meeting in the morning, code club in the afternoon and railway club in the evening, but you never know....  I am most grateful for your time and input, I'm a happy old man just now!


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7115
 

@swarlandrb Keep in mind the CANbus is what cars yse.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
SwarlandRB reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7115
 

@byron Perhaps my ignorance is showing here but is the case of build all the layers of a fully fleshed out protocol like the aforementioned CAN Bus, exchanging timing signals, exchange data packets with start and stop bit pattern and built in CRC checks to maintain data integrity plus knowledgenuts to ensure the packet got there I wonder why something higher up the chain cannot be used?

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
SwarlandRB reacted
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1123
 

@zander

I wondered that too, but to really know what could be done one would need to understand how the MERG CBUS works which seems to be an in-house board designed by the Model Electronics Railway Group.  I'm not up for getting involved in a deep dive into that, I can only help with the rpi pico programming to the extent I understand what @swarlandrb is asking to achieve.  

You may like to peruse MERG - https://www.merg.org.uk and see what you can make of their CBUS boards

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7115
 

@byron I suspect any serious system will need that communications both in and out will need to happen asynchronously. Some sort of assigning interrupt tasks per cpu might be a start.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7115
 

@byron When I move in fall 2023 I know there is a RR club there and I will be researching all the available commercial systems as well as the top m5 community systems and doing a conventional cost benefit study

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1123
 

@zander 

The need for asynchronous programming will depend on the code that will be run on the pico.  I suspect that in this case simple polling within a while loop will be sufficient.  However the micropython asyincio library is very good and we can use it if required.  I use async to ensure a robust mqtt comms on several projects. Async or simpler polling loops are often preferable to interrupts when using micropython, but using interrupt call backs are easy to use too.  Are your getting tempted to learn python/micropython yet? 😋 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 7115
 

@byron NO, justy ny bias showing through,We had to read many thousands of high speed to low speed lines  just in NorthAmerica We had similar plants in Europe and Asia, We did have 6 prceossors, but about 20 or 30 multithreaded processes to run. Some linkprocesss like the NYC stock exchange got top priority While Winnipeg got lowest.

That is just a little background that biases my design approach.

Since you are emulating life and death circumstances, the data exchange must be near instantaneous and 5 sigma accurate or better. My approach since there are 2 processors on each board is to dedicate the critical communication tasks to a cpu with highest priority along with very low priority tasks and on the second cpu all the rest with the possible exception of having any downstream cpu0 hi priority task output on the second cpu at a high or highest priority.

One word of caution however, knowing the execution profile of all your tasks and processes is crucial. I have seen what appear to be very low priority tasks get changed to medium or even hi because they only used a few micro seconds and then ran on their own for many milli. It is complex, and requires knowledge of the application, high level software, OS software, and hardware. Good Luck

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
(@swarlandrb)
Member
Joined: 2 years ago
Posts: 11
Topic starter  

@byron 

Hi again, I have had a chance to test your code and setup and it all works great. I love the simplicity of it all.  I have decided not to have the acknowledgement from Pico 2, the operator will know darn well whether the route has been set or not, so why complicate things, although I will need to flag up a fault. I think this will be perfect for my needs as Pico2 needs to just sit and wait until it receives an input, do a couple of lines of code and sit back and wait again, especially without the acknowledgement requirement.  My next task will be to rejig the code to make Pico 2 activate the appropriate pin: I'm really looking forward to getting that done, that's tomorrows task when the snow is on the ground.

Many thanks @byron for your patience and assistance, you have been a star.

 

cheers,

Roger


   
ReplyQuote
(@swarlandrb)
Member
Joined: 2 years ago
Posts: 11
Topic starter  

@zander My needs are very simple and with @byron help, I can now achieve my aims.  Many thanks for your interest.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1123
 

@swarlandrb

Posted by: @swarlandrb

I have decided not to have the acknowledgement from Pico 2, the operator will know darn well whether the route has been set or not, so why complicate things,

Keeping things as simple as possible is what I try to achieve too.  I was thinking of mentioning that in your setup I really don't see that the uart serial comms would fail as long as your wires dont shake loose or perhaps some other code slip up causes the pico to freeze at some point, and I too think the acknowledgement is superfluous.  And as, in the example waiting for the acknowledgment is blocking code, a different approach may be required and we are saved from more complicated coding.

Just a note which may be of interest, the uart.any() has a quick look at the uart buffer and returns the number of bytes it finds or 0 if nowt and as such it does not block.  The uart.read() can be called with the number of bytes to read (e.g. uart.read(2) and that can mean the read will block until the specified number of bytes has been found in the buffer or until a timeout is reached. (the timeout can be specified).  Calling uart.read() without specifying the number of bytes will mean it reads the number of bytes it can find in the buffer and will return them after the timeout period.

As you have seen the default timeout is quite small and in the example it returns seemingly immediately. But if this is a concern then a good approach is to use uart.any() to return the number of bytes available and then to use uart.read(x) with the number of bytes as reported.  The caution on this would be could you be reading an incomplete sequence of bytes at the point where uart.any() had a peek.

Well that note was just to give you a fuller picture, and probably you will not need to be concerned with this.

Good to hear you are well on the way to getting your project to a good conclusion. 

PS Do keep that snow up north as the edge of the snowy bit was getting worryingly close to Norfolk on the weather map 😎 

This post was modified 2 years ago by byron

   
ron bentley reacted
ReplyQuote
(@swarlandrb)
Member
Joined: 2 years ago
Posts: 11
Topic starter  

@byron 

Thanks for the explanation, it all helps the understanding of why something has or hasn't worked.  I forgot to ask, in the printouts, what is the significance of the letter 'b' that shows with all the numbers and I suppose I should ask how it gets there as it certainly doesn't get put there by design.

We'll do our best keep Norfolk clear of snow, but the wind is from the North for the next few days, if a polar low decides to barrel down the North Sea, look out.  Our oldest friends live on the southern outskirts of Norwich and we'll be with them at the end of May, we love the area and lived just off Fife's Lane, Norwich for a couple of years when Coltishall was active in the early 1970s.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1123
 
Posted by: @swarlandrb

what is the significance of the letter 'b' that shows with all the numbers and I suppose I should ask how it gets there as it certainly doesn't get put there by design.

In a word - unicode.  But as this is a frequently asked question I point you to some better explanations than I can probably give on the internet.  For example look at 

https://towardsdatascience.com/byte-string-unicode-string-raw-string-a-guide-to-all-strings-in-python-684c4c4960ba

and if you don't like that explanation then there are many others.   You might like to complement this with a search on 'python .decode'  and 'how to remove b' in python'   

Have fun.  And it does indeed seem we could be in for some Northerlies, and a need to watch out for some frosty nights.  But for me its no good excuse to stay inside play as I'v got a few indoor jobs about the house I must get on with.


   
ReplyQuote
Page 2 / 3