javax.comm - should reads and/or writes to serial port be synchronized?

Hi, lets say I have code that does:

open "COM1"

inStream = port.getInputStream();

outstream = port.getOutputStream()

start thread1

start thread2

thread1:

while(true)

{

sleep(2)

outStream.write(0x01);

}

thread2:

while(true)

{

sleep(3)

outStream.write(0x02);

}

Do I need to synchronize the threads' calls to write()? Also, if I create a third thread that reads from COM1's input stream, do I need to synchronize all three threads to prevent in/out data from being corrupted?

Thanks for your help

-Ben

[628 byte] By [dark_venika] at [2007-10-1 2:59:37]
# 1
You need to tie in a SerialEvent. Synchronization depends on your application...Of course the two thread model you have probably needs synchronization.Why do you have two thread writing to a comm port?
Martin3a at 2007-7-8 15:37:58 > top of Java-index,Administration Tools,Sun Connection...
# 2

Good point - I guess this would almost never be needed. More specifically - I'm writing a program which talks to some custom hardware via the serial port. The hardware box sends a conituous stream of data to the computer, and can also recieve commands (such as set parameter = value). My java program has a thread which continuously reads the incoming data from the port. In the mean time, other parts of my program will occasionally write to the port in order to send commands. So I guess a more useful question is - will the following code be problematic in terms of synchronization? Or is it ok to have one thread reading the port at the same time as another thread is reading?

open "COM1"

inStream = port.getInputStream();

outstream = port.getOutputStream()

start thread1

start thread2

thread1:

while(true)

{

sleep(2)

outStream.write(0x01);

}

thread2:

while(true)

{

sleep(3)

byte data = outStream.read();

}

Thanks

-Ben

dark_venika at 2007-7-8 15:37:58 > top of Java-index,Administration Tools,Sun Connection...