• Post Reply Bookmark Topic Watch Topic
  • New Topic

Event when data on serial port changes?  RSS feed

 
Niko Will
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an external hardware interface connected to my COM4 port. My Problem is that this device is sending its status permanently and I only want to get an event if the status is changed. I tried to extend the existend SerialPortEvent class but without source code it wasn't possible to add the new things I need. Has anyone an idea?

btw: apologize my terrible englisch
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A separate thread as a wrapper?

It would catch the noise from the com-port, and if the data isn't changed, drop it silently.
Else it would inform a class, which registered at it.
 
Niko Will
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know much about wrapper but i've heard it already. Do you have a documentation about "how to make a wrapper" or something like this? Maybe even in german but english is also fine.
 
Henry Wong
author
Sheriff
Posts: 22866
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Niko Will:
I don't know much about wrapper but i've heard it already. Do you have a documentation about "how to make a wrapper" or something like this? Maybe even in german but english is also fine.


Don't access the serial port object/API directly. Create a new class that will be the only object that accesses the object. This new class creates a new thread that reads the ports, throws out the noise, and stores the result somewhere.

An API can be created so that the data, without the noise, is passed back to your program's other threads.

Hope this helps,
Henry
 
Niko Will
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I read the serial port permanently, my cpu ussage is about 17%. I think it wouldn't be less if I put it in a thread and read the port permanently, isn't it? But I will try this maybe it helps. Can you tell me how i can get the result from another thread? Because when the thread is running I can't call other methods in the thread class and I think with wait and notify it doesn't work well. Sure I can declare a variable that is static so that it can be read by other threads, but then I don't know when there is a new result arrived.
 
Henry Wong
author
Sheriff
Posts: 22866
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niko,

If I read the serial port permanently, my cpu ussage is about 17%. I think it wouldn't be less if I put it in a thread and read the port permanently, isn't it?


No. It probably would not. Unfortunately, there is no real way around it, it your serial port is sending you junk, you either have to fix the hardware or have your program read it. The purpose of starting another thread is not to save CPU, but to make sure that the rest of your program get a fair share of it.

Can you tell me how i can get the result from another thread? Because when the thread is running I can't call other methods in the thread class and I think with wait and notify it doesn't work well. Sure I can declare a variable that is static so that it can be read by other threads, but then I don't know when there is a new result arrived.


I would suggest picking up a very good book on threading. (Java Threads, from O'Reilly, has just been updated for the latest JDK in its third edition...

Data passing, and the synchronization of it, is not simply done without a basic understanding of what you are doing. As for sending the data when it arrives, you could use a "listener" pattern in your program design. There is a sample of that as well in the book.

Henry
 
Niko Will
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice idea with the book, but I'm from Germany and reading a whole book in English would be very difficult for me to understand. I know how a thread works and what advantages I have through a thread, but the communication between two threads is not clear for me. I already thought that I have to use a listener, but I find no good documantation about creating one.

But it looks like that I have to read a whole English written book :roll:
 
Henry Wong
author
Sheriff
Posts: 22866
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niko,

Sorry about that... If I remember correctly, besides english, Java Threads are only in French and Japanese (although both are for older editions).

My publisher may kill me for this, but... feel free to take a look at http://examples.oreilly.com/jthreads3/ for the example code for the book. Unfortunately, the documentation is in the book, but the early examples should be easy to figure out. Look at the CharacterListener and CharacterSource class to start, and you should be able to figure out how listeners work in the example. (not complete theory, but good enough)

As for basic synchronization skills, it's your choice. I don't know the German book market enough to recommend another book.

Henry
 
Niko Will
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much. I have no time to take a look on the site yet, but I can't find another book about threads in German, so I will think about buying this book. Maybe this is a reason for your publisher that he probably doesn't kill you
[ October 02, 2004: Message edited by: Niko Will ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!