How to setup serial port in base workspace and access from function workspace?

조회 수: 2 (최근 30일)
Hello,
I would like to setup a serial port via s = serialport(port,baudrate) in the base workspace (initialization file) since serial port setup wont change during the life of the connection this really only needs to run once. However, I would like to call the serial port object from other function/functions workspace/workspaces. What are best practices to achieve this or is this even an acceptable practice at all?
Thank you,
Wess

답변 (1개)

dpb
dpb 2023년 8월 3일
Simply pass the returned serial port object to the function(s) where it's needed.
Best factorization would probably be to write a little function to call to do the initialization as well rather than just a bunch of constants buried inline in code; gives you the flexibility to change parameters if/when needed.
  댓글 수: 31
dpb
dpb 2023년 8월 8일
"you might be able to use textscan but for that kind of work it would also be common to use text manipulation to extract the pieces and convert them as needed, as that would tend to have lower overhead than using textscan()"
I hadn't thought necessarily of the former; wonder how the new(ish) extract() and patterns stuff might stack up. I've begun using them a fair amount for convenience, but not for anything that has an performance impact that would show; purely for convenience in writing some pattern matching.
Personally, I'd not think of textscan first, either, but would tend to drop straight down to sscanf for such low-level work.
But, there's the other end of starting with the highest level there is and then only worry about performance/optimization if/when that is shown to not be fast enough.
Walter Roberson
Walter Roberson 2023년 8월 8일
Early on, I tested the pattern-based routines, and at least at that time they were much slower than regexp() . But that might have changed.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interface-Based Communication에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by