473,407 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Serial port problems

hi,

I am using Win XP and writing an application to check POS devices
(printer and cash drawer).In the application I need to choose the port
which the device is connected to. If the port chosen is wrong or the
device is off power, a communication error message should be
displayed.
I am using FileOutputStream and FileInputStream to send and receive
data from the ports. When parallel port is used, I got the feedback
when the printer is off(exception caught) or the drawer status
changes. My problem is that for serial port, I can't get any feedback
at all. No exception is caught and it hangs when I try to read in data
from the COM port.
Please help me on how to get feedback from serial ports.How do i know
when the device is off?
Thanks in advance.
Below is part of my code:
private void Check(String device, String port) {
//port="COM1" or "LPT1"
FileOutputStream outputWriter = null;
FileInputStream inputReader = null;
static byte[] check = {(byte)16, (byte)4, (byte)1};
static byte[] testprint
={(byte)29,(byte)40,(byte)65,(byte)50,(byte)48,
(byte)48,(byte)50};

try{
outputWriter = new FileOutputStream(port);

if(device.equals("drawer")){
outputWriter.write(check);
inputReader = new FileInputStream(port);
int input = 0;
input = inputReader.read();
inputReader.close();
inputReader = null;
if (input==18)
System.out.println("Drawer is opened");
else //22
System.out.println("Drawer is closed");

}

else if(device.equals("printer"))
outputWriter.write(testprint);

outputWriter.close();
outputWriter = null;

} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"Communication Error");
}

}
Jul 17 '05 #1
1 5412
mimisam wrote:
I am using FileOutputStream and FileInputStream to send and receive
data from the ports. When parallel port is used, I got the feedback
when the printer is off(exception caught) or the drawer status
changes. My problem is that for serial port, I can't get any feedback
at all. No exception is caught and it hangs when I try to read in data
from the COM port.


Why on earth wuld you use FileInputStream and FileOutputStream for
talking to serial and parallel ports? Are you trying to use them
against PRN/LPTx and COMx?

This is not how you should be communicating with serial and parallel
devices. If you want to do that, look at the Java Communications API,
which provides its own classes far talking directly to the
serial/parallel hardware, allowing you to set the necessary
communication parameters (speed, bits per byte, stop bits, handshaking,
etc.).

To download and for more details, see:

http://java.sun.com/products/javacomm/

HTH!

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org
Jul 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Blaine HIlton | last post by:
I'm trying to send data over the serial port using PHP and Serproxy. I downloaded Serproxy from http://www.lspace.nildram.co.uk/freeware.html. From the manual page at...
1
by: Tamir Khason | last post by:
PLEASE HELP, I already seat on it a vouple of days, but no solution yet. Someone, Please help!!! I have to manage some device, working with RS422 (have a converter). From one place it working...
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
38
by: shussai2 | last post by:
Hi, I am trying to access Serial Port in XP. I am using Dev-C++ IDE that uses Mingw as a compiler. I just want to know how I can open up serial port on COM1 and write some data. I have searched...
7
by: davetelling | last post by:
I'm a newbie that is still struggling with OOP concepts & how to make things work they way I want. Using Visual C# Express, I have a form in which I added a user control to display a graph, based...
0
by: =?Utf-8?B?SmFtZXNB?= | last post by:
I am trying to use the code shown in the VB2005 Help on how "To receive strings from the serial port". The code was cut and pasted and it gives no errors when "debugged" in VB2005, but it simply...
0
by: dcm684 | last post by:
I am communicating with a microcontroller device and when I try to send a zero character my computer ends up sending a space instead. Occasionally, the computer will end up sending the '0' instead....
2
by: colin | last post by:
Hi, Im having a tiresome amount of trouble with using a bluetooth serial link. The receiving end is a bluetooth-rs232 module conected to my embeded system. The PC has a little usb bluetooth...
3
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.