SerialPortUtil | Android串口通信工具
kandi X-RAY | SerialPortUtil Summary
kandi X-RAY | SerialPortUtil Summary
Android串口通信工具
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Initializes the tv_hello
- Sets the device path
- Returns output stream
- Gets an InputStream for this object
- Send an error message
- Set whether to output to output
- Send data to the serial port
- Sets the time out in milliseconds
- Sets the Baudrate for the serial port
- Creates the SerialPortService
- Reads all available drivers from the terminal
- Get the names of all the devices in the system
- Convert a byte array to a hex string
- Get the names of all devices
- Closes the serial port
- Close the port
- Main entry point
- Converts a hex string to a byte array
- Converts an array of strings to a hex string
SerialPortUtil Key Features
SerialPortUtil Examples and Code Snippets
Community Discussions
Trending Discussions on SerialPortUtil
QUESTION
I've a small android library which handles a serial port, it has basic functionality like open, read, write and close.
I have made an applications that uses this library to write on the serial port and read the responses, within this application there is a thread that periodically opens the serial port asks for the status get the response and close the serial port.
I want to protect the serial communication in a way that if the main thread opens the communication the secondary thread that only checks the status can not open it and wait for the main thread to finish.
...ANSWER
Answered 2019-Feb-21 at 14:56How can I tell the checker thread that the communication is already opened and make him wait until the main thread finish.
I don't fully understand your code but the critical thing with threads and locking is to make sure that all threads are calling code that is synchronized
on the same object instance.
If I synchronize an object, there still is a window when the object is not synchronized.
Not if you use the same instance of the object. Making each of the public
methods in SerialChannel
synchronized
will make sure that only 1 thread can be using the object at once.
I suspect that your real problem is not about protecting the SerialChannel
object but more about race-conditions between the threads. They need to make multiple calls to the methods and they can block each other or interleave in an improper manner.
You can get around this with a couple of changes. You can make the send(...)
and receive(...)
methods auto-opening. Threads would just call send()
or receive()
which in turn would internally call open()
if the fileInputStream
or fileOutputStream
was null
. The thread would be inside of a synchronized
so this would not be interrupted by another thread.
Another completely different model to consider would be to have one thread reading from the serial port and another writing to it that are dedicated to that task -- they would be built into the SerialChannel
object. They would share data with the external threads using a read BlockingQueue
and a write BlockingQueue
. Then the serial port is opened early in your application which starts the IO threads and the external threads never worry about the IO. They just put()
and take()
from the queues. I typically do this (for example) when reading and writing to the console.
Hope something here helps.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SerialPortUtil
You can use SerialPortUtil 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 SerialPortUtil 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