SerialPortExample | yet complete example of an Android app | Android library
kandi X-RAY | SerialPortExample Summary
kandi X-RAY | SerialPortExample Summary
A simple, yet complete example of an Android app using UsbSerial library
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Called when the service is created
- Find the first USB device connected to the device
- Creates and registers the VISIBLE filter for the USB device
- Request user permission
- Start the RestbService
- Setup the filters for the USB device
- Start service
- Initializes the Activity
- Writes data through the serial port
SerialPortExample Key Features
SerialPortExample Examples and Code Snippets
Community Discussions
Trending Discussions on SerialPortExample
QUESTION
I am using UsbSerial (com.github.felHR85:UsbSerial:4.5) to stream Serial data to my app from the virtual serial port(over USB). The MainActivity
has a a handler that handles the messages from the from the USB Service, similar to the example provided:
https://github.com/felHR85/UsbSerial/blob/master/example/src/main/java/com/felhr/serialportexample/MainActivity.java
My App has several Activities, and many of them have views that should change if certain data comes across the serial port. E.g., a TextView
may need to update the text shown, a Button
should be enabled or disabled.
In order to manipulate the views outside of the onCreate
method in each activity, the easiest thing I have tried is to declare the views as private static
but I have seen in many places that this is a bad practice. I am setting the View references back to null as described here (under "2. Static Views") to, I think, avoid potential memory leaks: https://blog.nimbledroid.com/2016/09/06/stop-memory-leaks.html
I still don't like the fact that Lint in Android Studio indicates "Do not place Android context classes in static fields." and that this practice seems generally frowned upon.
Each activity has a public static boolean isActive
that gets set true
in onResume
and false
in onPause
The handler from the MainActivity
decides what to do with the incoming serial data depending on which activity is currently running (e.g. SecondActivity.isActive==true
). It then calls a public static
method from the current running activity (e.g. SecondActivity.updateViews(serialdata)
) which has access to the static View reference in order to do what it needs (e.g. myTextView.setText(serialdata)
)
If I shouldn't keep a static reference to a view, what are alternatives to achieving what I need, i.e. updating a view element in a SecondActivity
from the handler in the MainActivity
, while the SecondActivity
is already running?
ANSWER
Answered 2019-May-08 at 23:19The MainActivity has a a handler that handles the messages from the from the USB Service
This is not a good plan.
My App has several Activities, and many of them have views that should change if certain data comes across the serial port.
This illustrates why the not-good plan is not good. The data come from the serial port is not tied exclusively to MainActivity
... so why would MainActivity
be handling the USB service messages?
(it is also fairly likely that you should not have multiple activities here and instead have one activity with multiple fragments, but I'll leave that aside for now)
what are alternatives to achieving what I need
I don't know what is on the other end of the serial port that is causing the messages to be sent. For the purposes of this answer, I will call it a "thingy".
Step #1: Create a ThingyRepository
. This will be a class (with a singleton instance) that manages the USB serial connection and receive the messages. It will hold onto some representation of the current state based on past messages (ThingyState
) and it will update that state as new messages come in.
Step #2: Have ThingyRepository
expose some sort of "reactive API". That could be something modern, such as LiveData
or RxJava. It could be something older, like a callback-based API. Either way, you want ThingyRepository
to deliver updated ThingyState
objects to interested parties ("observers") when the state changes. So, ThingyRepository
converts USB serial messages into updated states, and it emits those states as needed.
Step #3: Have your UI observe ThingyRepository
, receive the ThingyState
objects, and update the UI based on those states. Ideally, you would use ViewModel
to mediate those communications, to deal with Android's configuration changes (screen rotations, etc.). But, if you wanted to stay "old-school", the activity could register a callback with your ThingyRepository
, where ThingyRepository
can call the callback with a fresh ThingyState
as the data changes based on the USB messages.
Now, you have decoupled the state changes from any single activity. Each activity can be responsible for figuring out what needs to change in its own UI based on the new state. And, you no longer need static views.
QUESTION
Edited:
I am working on a program that will receive data from serial port, the data will end in "\r\n". I am using Data_Received
event to receive the data from the serial port.
Below is my program, I am try to avoid to use Thread.Sleep and use thread to wait for the data fully receive, but the step always jump to the return "Error"
step.
I would like to know how to let the program to wait for the data fully receive before it jump to the return "Error"
step?
It will be fine if don't use thread and if someone have a better method to do.
...ANSWER
Answered 2018-Jan-04 at 11:38Please read:
If you must use .NET System.IO.Ports.SerialPort.
That is a blog post from Ben Voigt written 2014 on sparxeng.com.
Excerpt:
To put it mildly, System.IO.Ports.SerialPort was designed by computer scientists operating far outside their area of core competence. They neither understood the characteristics of serial communication, nor common use cases, and it shows. Nor could it have been tested in any real world scenario prior to shipping, without finding flaws that litter both the documented interface and the undocumented behavior and make reliable communication using System.IO.Ports.SerialPort (henceforth IOPSP) a real nightmare.
and
The worst offending System.IO.Ports.SerialPort members, ones that not only should not be used but are signs of a deep code smell and the need to rearchitect all IOPSP usage:
- The DataReceived event (100% redundant, also completely unreliable)
- The BytesToRead property (completely unreliable)
- The Read, ReadExisting, ReadLine methods (handle errors completely wrong, and are synchronous)
- The PinChanged event (delivered out of order with respect to every interesting thing you might want to know about it)
and
And the one member that no one uses because MSDN gives no example, but is absolutely essential to your sanity:
- The BaseStream property
Despite that blame from Ben Voigt, we are using the BytesToRead
property and the ReadExisting()
including Thread.Sleep()
. We avoid DataReceived()
and ReadLine()
. That was our workaround, we didn't know the BaseStream property as well.
For new projects I would recommend to use the BaseStream
property.
QUESTION
ANSWER
Answered 2017-Feb-22 at 08:35The correct approach may vary, but here is one simple to read data of known size:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SerialPortExample
You can use SerialPortExample like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the SerialPortExample component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page