I'm using java RXTX serial port implementation.
Scenario is that a surveillance device (combination of sensors) is sending its data in text mode but when there is a new image from its camera it is supposed to send that image (which is a simple jpeg file) in binary mode.
How do I handle this in run-time?
Most of devices implement a well known protocol to send data so whoever that is listening and knows this protocol can interpret this data. Tipically they send it in frames (most of times with a fixed length) that has some additional info such as status, data length, data type, etc. For instance something like this:
| 0010 | 0001 | 1101 | 0000 ... 0010 1101 | 0111 |
status type length data CRC
If this is the case you would have to contact the manufacturer to get the protocol documentation.
If it's not and this surveillance device is sending data all time then you can do this:
0xFF, 0xD8
then you are in presence of a JPEG picture as these bytes are the Start Of Image (SOI) marker. Then next data should be part of the image until you get this pair of bytes: 0xFF, 0xD9
which are the End Of Image (EOI) marker.The following examples are taken from official RXTX site and both of them read data as bytes: