UDP Communication #2

  • Follow


I am trying to send some data to UDP port in ascii format. The file that I read contains ascii characters which will be sent. Here is the some part of my code:
*****************************
fid = fopen('record_001586.txt', 'r');
F = fread(fid, 'uchar')';
Host = input('Remote Host IP: ','s');
Port = input('Remote Port: ');
echoudp('on',Port);
u = udp(Host,Port);
fopen(u);
fprintf(u, '%c', (F(1:10)));        %F(1:10) = [161 160 65 0 0 0 64 65 66 67];
fscanf(u);                 % answer is [161 160 65]
echoudp('off')
fclose(u);
********************************
As seen from the answer, fscanf read data untill 0 (NULL).
But I also want to send zeros to UDP port in ascii format.

Any help will be appreciated.
Thanks in advance,
Uğur BOZKURT
0
Reply U 7/22/2010 12:50:06 PM

U?ur wrote:
> I am trying to send some data to UDP port in ascii format. The file that 
> I read contains ascii characters which will be sent. Here is the some 
> part of my code:
> *****************************
> fid = fopen('record_001586.txt', 'r');
> F = fread(fid, 'uchar')';
> Host = input('Remote Host IP: ','s');
> Port = input('Remote Port: ');
> echoudp('on',Port);
> u = udp(Host,Port);
> fopen(u);
> fprintf(u, '%c', (F(1:10)));        %F(1:10) = [161 160 65 0 0 0 64 65 
> 66 67];
> fscanf(u);                 % answer is [161 160 65]
> echoudp('off')
> fclose(u);
> ********************************
> As seen from the answer, fscanf read data untill 0 (NULL).
> But I also want to send zeros to UDP port in ascii format.

Please check the Terminator and DatagramTerminateMode properties of the udp 
object. I do not have that toolkit and the documentation does not make clear 
what the default values are. For your purposes you probably want 
DatagramTerminateMode to be set to 'on', in which case Terminator would be 
ignored.
http://www.mathworks.com/access/helpdesk/help/toolbox/instrument/fscanf.html

Your code has

fscanf(u);

but says in the comment that "answer is [161 160 65]". How did you check that 
those were the values received? Did you try

t = fscanf(u);
disp(0 + t);     %adding 0 converts to decimal values from characters

I think it is possible that the NUL bytes might be in the received string. If 
you had used

t = fscanf(u);
fprintf(1,t);

then t would be in the format position of the fprintf operation and formats 
are considered to terminate at a NUL.
0
Reply roberson (2852) 7/22/2010 7:37:54 PM


> Please check the Terminator and DatagramTerminateMode properties of the udp 
> object. I do not have that toolkit and the documentation does not make clear 
> what the default values are. For your purposes you probably want 
> DatagramTerminateMode to be set to 'on', in which case Terminator would be 
> ignored.
> http://www.mathworks.com/access/helpdesk/help/toolbox/instrument/fscanf.html
> 
> Your code has
> 
> fscanf(u);
> 
> but says in the comment that "answer is [161 160 65]". How did you check that 
> those were the values received? Did you try
> 
> t = fscanf(u);
> disp(0 + t);     %adding 0 converts to decimal values from characters
> 
> I think it is possible that the NUL bytes might be in the received string. If 
> you had used
> 
> t = fscanf(u);
> fprintf(1,t);
> 
> then t would be in the format position of the fprintf operation and formats 
> are considered to terminate at a NUL.

I experienced the code again as you said and the result didnt change. If I use '%d' inside fprintf, I can get zeros and the rest as decimal values. But I dont want to send decimal values. I need to send characters rather than numbers. In this case I wrote   '%c'   inside fprintf and I could only get some data untill NULL which corresponds "¡ A" that equals char([161 160 165]).
I tried different termination modes and datagram modes, but it didnt worked.
There must be a way to achive this.
Please help!!!
0
Reply U 7/23/2010 12:09:06 PM

"U?ur " <uguboz08@student.hh.se> wrote in message 
news:i2c0p2$jt5$1@fred.mathworks.com...
>
>> Please check the Terminator and DatagramTerminateMode properties of the 
>> udp object. I do not have that toolkit and the documentation does not 
>> make clear what the default values are. For your purposes you probably 
>> want DatagramTerminateMode to be set to 'on', in which case Terminator 
>> would be ignored.
>> http://www.mathworks.com/access/helpdesk/help/toolbox/instrument/fscanf.html
>>
>> Your code has
>>
>> fscanf(u);
>>
>> but says in the comment that "answer is [161 160 65]". How did you check 
>> that those were the values received? Did you try
>>
>> t = fscanf(u);
>> disp(0 + t);     %adding 0 converts to decimal values from characters
>>
>> I think it is possible that the NUL bytes might be in the received 
>> string. If you had used
>>
>> t = fscanf(u);
>> fprintf(1,t);
>>
>> then t would be in the format position of the fprintf operation and 
>> formats are considered to terminate at a NUL.
>
> I experienced the code again as you said and the result didnt change. If I 
> use '%d' inside fprintf, I can get zeros and the rest as decimal values. 
> But I dont want to send decimal values. I need to send characters rather 
> than numbers. In this case I wrote   '%c'   inside fprintf and I could 
> only get some data untill NULL which corresponds "� A" that equals 
> char([161 160 165]).
> I tried different termination modes and datagram modes, but it didnt 
> worked.
> There must be a way to achive this.
> Please help!!!

FSCANF and FPRINTF are specialized functions for 7 bit ASCII communication. 
What you describe sounds like 8 bit binary communication which is handled by 
FREAD and FWRITE.  Try the following:

>> echoudp('on',30000);
>> u=udp('localhost',30000);
>> fopen(u);
>> fwrite(u,char([161,160,65,0,0,0,64,65,66,67]));
>> tmp = char(fread(u, u.BytesAvailable,'char'))'

tmp =

??A   @ABC

>> whos
  Name      Size            Bytes  Class    Attributes

  tmp       1x10               20  char
  u         1x1               242  udp

>> uint8(tmp)

ans =

  161  160   65    0    0    0   64   65   66   67



0
Reply Trent 7/23/2010 2:00:30 PM

> FSCANF and FPRINTF are specialized functions for 7 bit ASCII communication. 
> What you describe sounds like 8 bit binary communication which is handled by 
> FREAD and FWRITE.  Try the following:
> 
> >> echoudp('on',30000);
> >> u=udp('localhost',30000);
> >> fopen(u);
> >> fwrite(u,char([161,160,65,0,0,0,64,65,66,67]));
> >> tmp = char(fread(u, u.BytesAvailable,'char'))'
> 
> tmp =
> 
> ??A   @ABC
> 
> >> whos
>   Name      Size            Bytes  Class    Attributes
> 
>   tmp       1x10               20  char
>   u         1x1               242  udp
> 
> >> uint8(tmp)
> 
> ans =
> 
>   161  160   65    0    0    0   64   65   66   67
> 
> 

It seems OK and I hope it will work.
Thanks for your help.
0
Reply Ugur 7/26/2010 5:34:04 AM

4 Replies
364 Views

(page loaded in 6.235 seconds)

Similiar Articles:













7/26/2012 5:17:40 AM


Reply: