From: U?ur on
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
From: Dinesh Iyer on
Hello Ugur,
You can try the following code

fwrite(u, F, 'uint8');
data1 = fread(u, 100, 'uint8'); % This might give u a warning, but u can ignore it
data2 = char(data1)'

Hope this helps.

Regards,
Dinesh


"U?ur " <uguboz08(a)student.hh.se> wrote in message <i29eic$9rq$1(a)fred.mathworks.com>...
> 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&#287;ur BOZKURT
From: Walter Roberson on
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.
From: U?ur on

> 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!!!
From: Trent Jarvi on

"U?ur " <uguboz08(a)student.hh.se> wrote in message
news:i2c0p2$jt5$1(a)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