Java program simulating control-z being pressed

  • Thread starter Per Magnus L?vold
  • Start date
P

Per Magnus L?vold

Hi,
I have tried to figure out how to make my Java program simulate
control-z (^z) being pressed.
The program communicates over the serial port with a modem
/* sample code */
..
..
sp = (SerialPort) port.open("Test", 1000);
ps = new PrintStream(sp.getOutputStream());
..
..
public void sendBytes(String s) {
for (int i = 0; i < s.length(); i++) {
char b = s.charAt(i);
ps.write(b);
}
}
/* end sample code */

My question is this:
How can I make the Java program send the equivalent of control-z to
the modem?
Typing control-z in hyperterminal manually works, but I want the Java
program to do this. I have thought of using:
sendBytes(KeyEvent.CONTROL+"z");
But this doesn't work.

I find in another posting that "control-c has the numerical value of
3"
http://www.google.com/groups?hl=no&...r=&ie=ISO-8859-1&q=java+control-c+serial+port
....and someone tip'ed me that control-z then might be 26, but that
does not seem to be right.

Hope someone can help me! :)

Regards,
Per Magnus
 
T

Tor Iver Wilhelmsen

...and someone tip'ed me that control-z then might be 26, but that
does not seem to be right.

Yes, but most often you will need to send a CRLF pair as well, ie. the
bytes 26, 13, 10. The reason is that the modem or whatever you talk to
at the other end often will collect characters until a whole line has
been entered, then process that.

Also, remember to flush() the output buffer after writing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top