Recent

Author Topic: how can i delete '#' in byte careter?  (Read 9256 times)

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
how can i delete '#' in byte careter?
« on: September 01, 2010, 06:39:42 am »
Hi,
I have data from deveice. It send me #2 and i want to use it 2. How can i delete # carecter?
when i write
Code: [Select]
var
  b: byte;
  s, ss: string; 
begin
   ss  :=  buf[1];
result is null

when i write
Code: [Select]
   ss  :=  Copy(buf,1,1);   

result is #2
i want to see 2
how can i do it?

BlueIcaro

  • Hero Member
  • *****
  • Posts: 810
    • Blog personal
Re: how can i delete '#' in byte careter?
« Reply #1 on: September 01, 2010, 10:12:56 am »
SS is a string, in Pascal, they are arrays, which start in 1.

SS[1] have #
ss[2] have 2

So try:

Quote
ss  :=  Copy(buf,2,Length(ss));

/BlueIcaro

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4542
  • I like bugs.
Re: how can i delete '#' in byte careter?
« Reply #2 on: September 01, 2010, 10:23:05 am »
ss  :=  Copy(buf,2,Length(ss));

Not quite but this should work:
Code: [Select]
var
 s1, s2: string;
...
 s1 := '#2';
 s2 := Copy(s1, 2, Length(s1));
( or s2 := Copy(s1, 2, MaxInt); )

String indexes start from 1. The last Count parameter can be bigger than the actual length.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how can i delete '#' in byte careter?
« Reply #3 on: September 01, 2010, 01:48:07 pm »
Code: [Select]

function TFrmMain.TestForDeleteSharp: string;
var
 s1,s2 : string;

    {
    Buffer come from Lnet with
    LTelnetClient.GetMessage(buffer)
     }
     begin
  s2 := Copy(buffer, 2, Length(buffer));
end;

                    
when i use code s2 is null.
result is null
« Last Edit: September 01, 2010, 01:54:33 pm by ahmetnurideniz »

bobc

  • New Member
  • *
  • Posts: 41
Re: how can i delete '#' in byte careter?
« Reply #4 on: September 01, 2010, 08:22:09 pm »
#2 is a single character, ASCII code 2, a control character.

Since Pascal does not use escape sequences for non printable characters like C does, non-printing characters are represented as # followed by a number.

I think what you are trying to do is convert a character to an integer, and then perhaps convert the number to a string.

Code: [Select]
var
   b: byte;
   s, ss: string;
begin
  s := #2;
  b := ord(s[1]);   // convert to a byte value = 2
  ss := inttostr (b); // convert binary to decimal string = '2'
...

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how can i delete '#' in byte careter?
« Reply #5 on: September 02, 2010, 12:02:54 am »
it is working. thanks bobc.
i test some of numbers is the result is true?
My test numbers are
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,20,31,33,35,126,127,128,200, ,10000,20000,25000,3000,60000]
this are working. for example when plc send 60000 i can read it 60000 my program.
 but
1000 and 65499 is not working.
when plc send 1000 i read it 131 in my program.
when plc send 1000    i see '#232#3#13#10' when i run F7 LTelnetClient buffer
when Plc send me 1000 i see ' \350\003\r\n' from wireshark

« Last Edit: September 02, 2010, 02:34:04 am by ahmetnurideniz »

bobc

  • New Member
  • *
  • Posts: 41
Re: how can i delete '#' in byte careter?
« Reply #6 on: September 02, 2010, 08:42:42 pm »
It appears your plc is sending binary data in little endian order followed by CRLF (#13#10). So 232 + 3*256 = 1000.

To convert that to a useful value you need something like

Code: [Select]
var
   i: integer;
begin
  i := ord (s[1]);
  i := i + 256* ord (s[2]);

The problem is I can't see how you can tell how many bytes are the binary value. Maybe you need to strip off the CR LF then convert whatever is left to a number. Its a funny way to implement a protocol.
« Last Edit: September 02, 2010, 08:48:58 pm by bobc »

ahmetnurideniz

  • Full Member
  • ***
  • Posts: 110
  • As you sow, you shall reap.
    • Big Student Web Site
Re: how can i delete '#' in byte careter?
« Reply #7 on: September 03, 2010, 12:08:41 am »

The problem is I can't see how you can tell how many bytes are the binary value. Maybe you need to strip off the CR LF then convert whatever is left to a number. Its a funny way to implement a protocol.

i have a c# example code for my device.
Code: [Select]
/*
                   * Discard the trailing "\r\n> ".
                     */
                    int datasize = recvd - 4;
                    switch (datasize)
                    {
                        case 1:
                            // Byte
                            Byte b8 = m_buffer[0];
                            resultL.Text = b8.ToString();
                            break;
                        case 2:
                            // Word
                            UInt16 b16 = BitConverter.ToUInt16(m_buffer, 0);
                            resultL.Text = b16.ToString();
                            break;
                        case 4:
                            /*
                             * Int32 veya Float bir değer. Biz Int32 olduğunu varsayacağız.
                             *
                             * Int32 or Float, we'll assume Int32
                             */
                            UInt32 b32 = BitConverter.ToUInt32(m_buffer, 0);
                            resultL.Text = b32.ToString();
                            break;
                        default:
                            MessageBox.Show("Unknown data size");
                            break;
                    }
                    echo = false;
                    break;
 

bobc

  • New Member
  • *
  • Posts: 41
Re: how can i delete '#' in byte careter?
« Reply #8 on: September 05, 2010, 12:09:01 pm »
Hmm, I think there is a bug in the C# code. "\r\n" is 2 bytes, not 4, unless there is something I am missing. Otherwise it makes sense.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8783
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: how can i delete '#' in byte careter?
« Reply #9 on: September 06, 2010, 08:00:44 am »
Quote
"\r\n" is 2 bytes, not 4, unless there is something I am missing. Otherwise it makes sense.
I don't know how C# treats character, if it's like Java, then each character is 2 bytes instead of 1 (Unicode).

 

TinyPortal © 2005-2018