Author Topic: Connect, send questions  (Read 2931 times)

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Connect, send questions
« on: May 16, 2011, 01:16:06 pm »
Roman,
I'm evaluating your component, and having a couple of problems. I need to connect to a device and send and receive ASCII data(strings). I use a loop to vary the baud rate to match the device settings. How can I determine when I actually get connected(question1)?

With another component, I send a command and read the response. SendString doesn't seem to work, as I get no response(question2).

 for I := low(FBaudRate) to high(FBaudRate) do
 begin
    ComPort.Active := false;
    ComPort.BaudRate := FBaudRate;
    try
      ComPort.Active := true;
      ComPort.SendString('AI2;');
      sleep(200);
     ComPort.SendString('IF;');
     if ComPort.TotalReceived > 0 then  <== always zero
     begin
         

The codes demo receives from the device, but doesn't respond when I use sendstring.  Help doesn't say if it'
s an ansistring or unicode string, I have tried both, no joy

Thanks, Mike

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Connect, send questions
« Reply #1 on: May 16, 2011, 04:48:58 pm »
Hello

Please replace

Code: [Select]
if ComPort.TotalReceived > 0
by

Code: [Select]
if ComPort.ReadyForRead > 0
and try again.

Roman Novgorodov
DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Connect, send questions
« Reply #2 on: May 16, 2011, 07:40:57 pm »
Sorry, no change. I think for some reason the write is failing.

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Connect, send questions
« Reply #3 on: May 17, 2011, 04:04:27 am »
Hello

I don't see your full code.

Possible nrComm works in asynchronous mode and all incoming data is available in OnAfterReceive event, therefore ReadyForRead is zero.

You need set to null following properties before begin your process:

ComPort.OnAfterReceive := nil;
ComPort.Terminal := nil;
ComPort.DataProcessor := nil;


Roman Novgorodov
DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Connect, send questions
« Reply #4 on: May 17, 2011, 03:32:35 pm »
I tried your suggestions, but still no joy. Perhaps I put them in the wrong place. Doesn't setting ComPort.OnAfterReceive := nil; disable it? I'll need it to work.

I've attached my .pas unit.

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Connect, send questions
« Reply #5 on: May 17, 2011, 05:01:42 pm »
Hello

Thank you for information.

I see in your source code that you still use OnAfterReceive event.
In this case, better if you will send initial commands and start timer for detect timeout, If during timeout you will get reply in OnAfterReceive event device is detected.

Also there is another strange thing.
You are trying repeat commands with different baud rate. Why? I don't understand your logic.

Do you understand that remote device will receive data anyway?
If you use wrong baud rate, device will receive wrong data but it can reply something or hung ups.

Can you describe device protocol? Possible I will find time and create more correct sample for detect device connection.

Roman Novgorodov
DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Connect, send questions
« Reply #6 on: May 17, 2011, 08:16:37 pm »
Yes, I need the onAfterReceive event. I don't understand why I need a timer.

As I said earlier, I loop through the baud rates to find the one the device is set for. It works very well with other components, some commercial, some home made. It's one less thing the user has to set.

If the device receives the wrong data, it simply ignores it.

The device sends data to the PC under two conditions. One is if a control on the device is changed, it automatically sends data. I can receive this OK in the onAfterReceive event.

The other method is to have the PC query the radio. The exchange is text strings and there are over 100 different query parameters.

A couple of examples:

The PC sends 'AI2;' to the radio, the radio responds with 'AI2;' (without the parens of course).

The PC sends 'IF;' to the radio, the radio responds 'IF00018150000     -000000 0006000001 ;'

Again I believe the problem is querying the radio, but I have no proof  :D

except nothing is returned.  I tried it without the baud rate loop in my program, and in the codes_demo example, and after sending, the onAfterReceive event does not fire.

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Connect, send questions
« Reply #7 on: May 18, 2011, 10:26:15 am »
Hello

Please try compiled demo from attachment.
Select correct serial port, click "Connect" and see messages in log memo.

Does demo work correctly?

Roman Novgorodov
DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Connect, send questions
« Reply #8 on: May 18, 2011, 11:21:24 am »
The demo does not work. I select the port, and click connect.

The log says

"Start device detection ...
Attepmt 1 Trying baud rate 4800 ..."

with an error message dialog

"Device Com7 open error!!!
 Access is denied."



Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Connect, send questions
« Reply #9 on: May 18, 2011, 11:33:00 am »
Hello

This error means that your serial port is busy.
It is already opened by someone.
Please select free serial port with connected your radio device.

Roman Novgorodov
DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Connect, send questions
« Reply #10 on: May 18, 2011, 02:38:11 pm »
My apologies Roman. There was another program using the port.

Now I get:

Start device detection ...
Attepmt 1 Trying baud rate 4800 ...
TIMEOUT!!! No reply from device.
Attepmt 2 Trying baud rate 9600 ...
TIMEOUT!!! No reply from device.
Attepmt 3 Trying baud rate 19200 ...
TIMEOUT!!! No reply from device.
Attepmt 4 Trying baud rate 38400 ...
TIMEOUT!!! No reply from device.
Device is NOT FOUND!

It should connect at 9600 baud.

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Connect, send questions
« Reply #11 on: May 18, 2011, 04:00:58 pm »
Hello

Possible something wrong in your description of device commands.
Or something wrong in serial port settings like parity, bit size etc.
Please open standard Windows tool Hyperterminal and connect to your COM7 with correct params and 9600 speed.
Enter 'AI2;'
Does device reply ?
Show me terminal screen.

Roman Novgorodov

DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Connect, send questions
« Reply #12 on: May 18, 2011, 06:26:29 pm »
I do not believe there is anything wrong with my description of device commands, or in serial port settings like parity, bit size etc.

My program works fine with other serial  components, as well as several other programs, 1 of which I have seen the source code for.

Hyperterminal returns data (just like our tests here) when a button on the radio is pressed. It does nothing when I type in AI2; or IF; Possibly because each character is sent individually, not as a string? Turning on local echo, I can see a 3-5 delay between being able to type the next character.

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Connect, send questions
« Reply #13 on: May 19, 2011, 05:11:12 am »
Hello

Thank you for your information.

Quote
The PC sends 'AI2;' to the radio, the radio responds with 'AI2;' (without the parens of course).
The PC sends 'IF;' to the radio, the radio responds 'IF00018150000     -000000 0006000001 ;'

Quote
Hyperterminal returns data (just like our tests here) when a button on the radio is pressed. It does nothing when I type in AI2; or IF;

Quote
I do not believe there is anything wrong with my description of device commands,

Your confidence is very good. :-)

It seems like you don not know complete specification of command interface of your device.

OK

There are many various situations:

Possible command should be terminated by #13,
Possible you typing too slowly in terminal,
Possible your device does not expect any commands at all and returns data on time intervals or button clicks.
Possible device expects initialization over RTS CTS signal changing.
... and etc.

You can try new version of demo from attachment.

If it does not work again, we need to know real data transmission between PC and device.

Quote
My program works fine with other serial  components, as well as several other programs,

You can download and run portmon tool.
After that please start your "workable solution".
portmon shows all data between your good APP and device.
Send to me log info of portmon

Roman Novgorodov
DeepSoftware
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.

Mike

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Connect, send questions
« Reply #14 on: May 19, 2011, 02:17:32 pm »
Roman -
Thanks for your efforts, sadly the new demo failed exactly like the other one.
Quote
Your confidence is very good. :-)

It's based on experience, not blind assumptions.

Quote
It seems like you don not know complete specification of command interface of your device.

I believe I do, again based on experience. The official specs may be found here. www.elecraft.com click on Manuals and Downloads, scroll down to K3 Programmer's Reference (pdf). See the section "Command Format".

Attached is the portmon log. See line 36 re: IF;.