I have one RPI which is connected throug serial console to some other device, and I am able to access to that device through microcom when logged in to RPI
nice microcom -s 115200 -p /dev/ttyUSB3
Once I use command above I am "in" the device and I can perform some actions. It's linux box. However I would like to automate actions executed on linux box (ttyUSB3) So I tried with Jsch. I connect to RPI through ssh and then execute commands there but for some reason I am not able to execute commands on linux box, all commands are executed on RPI.
Here is my java code:
logger.info("Connecting to RPI")
JSch jsch = new JSch();
session = jsch.getSession(USER, HOST);
session.setPassword(PASSWD);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(60 * 1000);
try {
ChannelExec channel = session.openChannel("exec");
channel.setErrStream(System.out, true)
channel.setCommand("nice microcom -s 115200 -p /dev/ttyUSB3\ntouch mytestfile.txt");
channel.connect();
} finally {
session.disconnect();
}
On RPI I can see that it started microcom procces, but this file "mytestfile.txt" gets created under RPI and not on my linux box.
Any ideas?
---------------------UPDATE AFTER FIXING PREVIOUS ISSUE --------------------
I now run into another trouble with another device. That another device doesn't echo and has this local line editing thing where when I type each character gets sent to it(server). And I need to send for example "SOUT001" command, so this:
ChannelExec channel = session.openChannel("exec");
channel.setErrStream(System.out, true)
//thanks to https://stackoverflow.com/questions/35748974/does-jsch-session-own-error-stream-when-it-is-set-and-no-other-thread-can-use
channel.setCommand("nice microcom -s 9600 -p /dev/ttyUSB0");
OutputStream out = channel.getOutputStream();
channel.connect();
out.write(("SOUT001\n").getBytes());
out.flush();
Doesn't work.
When I use
nice microcom -s 9600 -p /dev/ttyUSB0
On RPI and then put "SOUT000" into clipboard and then paste it to terminal and then hit enter, it executes correctly.
Here is putty explanation of local echo and local line editing
4.3.8 ‘Local echo’
With local echo disabled, characters you type into the PuTTY window are not echoed in the window by PuTTY. They are simply sent to the server. (The server might choose to echo them back to you; this can't be controlled from the PuTTY control panel.)
Some types of session need local echo, and many do not. In its default mode, PuTTY will automatically attempt to deduce whether or not local echo is appropriate for the session you are working in. If you find it has made the wrong decision, you can use this configuration option to override its choice: you can force local echo to be turned on, or force it to be turned off, instead of relying on the automatic detection.
4.3.9 ‘Local line editing’ Normally, every character you type into the PuTTY window is sent immediately to the server the moment you type it.
If you enable local line editing, this changes. PuTTY will let you edit a whole line at a time locally, and the line will only be sent to the server when you press Return. If you make a mistake, you can use the Backspace key to correct it before you press Return, and the server will never see the mistake.
Since it is hard to edit a line locally without being able to see it, local line editing is mostly used in conjunction with local echo (section 4.3.8). This makes it ideal for use in raw mode or when connecting to MUDs or talkers. (Although some more advanced MUDs do occasionally turn local line editing on and turn local echo off, in order to accept a password from the user.)
Some types of session need local line editing, and many do not. In its default mode, PuTTY will automatically attempt to deduce whether or not local line editing is appropriate for the session you are working in. If you find it has made the wrong decision, you can use this configuration option to override its choice: you can force local line editing to be turned on, or force it to be turned off, instead of relying on the automatic detection.
Do you maybe have any insights regarding this?
Thanks!
------------------------ UPDATE ----------------------------------------
It was carriage return. Everything works fine with:
ChannelExec channel = session.openChannel("exec");
channel.setErrStream(System.out, true)
//thanks to https://stackoverflow.com/questions/35748974/does-jsch-session-own-error-stream-when-it-is-set-and-no-other-thread-can-use
channel.setCommand("nice microcom -s 9600 -p /dev/ttyUSB0");
OutputStream out = channel.getOutputStream();
channel.connect();
out.write(("SOUT001\r").getBytes());
out.flush();
out.write(("SOUT000\r").getBytes());
out.flush();
Thanks!