0 votes
by (120 points)

I’m using Terminal Emulation to send Text to a server Using Scripting.Send(“XXX”) Getting the answers in an Event handler callback function , most of the times I get the replay correctly, but from time to time I get the text I sent, or the text + the remote server replay together in the callback func, I’m Using C# to do this process
And using the latest Terminal Emulation version

Any idea way that might happened?

2 Answers

0 votes
by (15.2k points)


From the description you provided the whole process is not clear to me. Which method do you use to receive the response to the command? Are you using Scripting.Process() method? Which event handler are you using? Are you using Scripting.Terminal.DataReceived event to read the responses? And is there any reason to use Scripting.Send("XXX") prior to Scripting.SendCommand("XXX")?

I'll try to describe the differences.

Scripting.Send("XXX") sends the "XXX" string to the server and that's it. But, Scripting.SendCommand("XXX") appends a NewLine sequence to the end (default is \r, but can be changed to \r\n using scripting.Terminal.Options.NewLineSequence), so the server receives "XXX{NewLine}". Then the Scripting.SendCommand("XXX") method try to receive "XXX{NewLine}" as a confirmation that the server correctly received a XXX command. Basically, it receives the first line of the response. Most servers usually respond with the same string as a confirmation that it receive the data (and this response is the string which a human user can see in a terminal screen when typing a command).

You should also consider using Scripting.ReadUntil(...) or Scripting.WaitFor(...), because those methods give you more control over the received data than the Scripting.Process(). The reason is that the server can send response divided to an unknown number of packets and Scripting.Process() combined with Scripting.Terminal.DataReceived event gives you data divided into those packets. On the other hand, Scripting.ReadUntil(...) or Scripting.WaitFor(...) receive data until a condition specified in theirs parameter is met. Additionally, those methods join the received data in many packets to one string. Please note that the packet is always received as a whole.If the condition is satisfied in the middle of the processed packet, Scripting.ReadUntil(...) or Scripting.WaitFor(...) returns processed data and the remaining data leave untouched for another call of Scripting.ReadUntil(...) or Scripting.WaitFor(...).

by (120 points)
Hi, and thanks for your answer, it clarify a lot of things to me, Im using scripting.Send and then immediately Scripting.Process(200), and the event i use is OnDataReceived i have 2 issues

1.  from time to time i get the command i send, back  as a replay to the OnDataReceived
2. the Scripting.Process always wait the full time, if i  Scripting.Process(200) it will wait 200 ms before it get back to the main process and if ill Scripting.Process(2000) it will return after 2 seconds, according to the documentation it should return when data received or when the int i send to the function passes (time)
I have a question: if ill use the redUntil, will it pass control when data finished to arrive?
0 votes
by (15.2k points)

Hi, thank you for more clarified process you have.

"1. from time to time I get the command i send, back as a replay to the OnDataReceived"
This is actually correct behaviour. As I wrote earlier, the server sends back the commands as a confirmation that it receive it correctly. And also, this response is usually shown to the user who typed the command. These are also received data and so they appear in DataReceived event.

"2. the Scripting.Process always wait the full time..."
This is really strange. I wrote a test to see how it behaves and the Scripting.Process(60 * 1000) returned immediately after a packet is received. But, as I wrote earlier, the packet does not necessarily contains the whole response. What duration do you get from this sample code? (Please, try it without handling DataReceived event completly.)

// send command
scripting.Send("sample command\n");

// process response
int start = Environment.TickCount;
scripting.Process(60*1000); // one minute

// print Process duration
int duration = Environment.TickCount - start;

Additionally, the DataReceived event is raised within the Process method, so its processing time is added to processing time of Scripting.Process() itself.

"Will ReadUntil pass control when data finished to arrive?"
This is not so easy to answer. It will return control immediately when you read until "some string" and "some string" appears in received data. Scritping.ReadUntil(...) receives more than one packet (internally it calls Process() in a loop, so DataReceived event is also raised) until it receives the condition provided in its parameter. But, there are some conditions that are used for tracking time. This is not your case, so we may assume that: Yes, it will return immediately.