in over my head ascii

N

nephish

Hello there,
i need to write a script that can transfer info back and forth with a
data server at so-and-so ip.

i have Programming Python, which covers socket programming. So thats
cool. But what i need to know how to do is make a message in ascii that
is what the server is looking for.

for example , to send a request, the first three bytes have to be ascii
"STX"
then there has to be 4 bytes that show the length of the message
then the actual message
then the last three bytes have to be "ENX"

also all int variables must me unsigned long-int (32 bit).

so here is what i need to know,
1 how do i make something in ascii bytes to send off to this server ?
2 how can i make sure something is a 32bit unsigned long int. ?

If the answers i am looking for are pretty straightforward, please let
me know., If they are long and complicated, please point me to some
good documentation .

thanks for all,
sk
 
D

Dennis Lee Bieber

for example , to send a request, the first three bytes have to be ascii
"STX"

Do you mean the literal string of S T X, or three occurrences of the
STX control code?

For the latter, suggest you look at ord(), chr(), and hexadecimal
string literals (escape sequence in the reference).
then the last three bytes have to be "ENX"
I'm not familiar with an ENX control code... ETX I've seen...
also all int variables must me unsigned long-int (32 bit).
Read up on struct...
so here is what i need to know,
1 how do i make something in ascii bytes to send off to this server ?

Any non-Unicode Python string is simple bytes...

--
 
T

Terry Reedy

for example , to send a request, the first three bytes have to be ascii
"STX"
then there has to be 4 bytes that show the length of the message
then the actual message
then the last three bytes have to be "ENX"

also all int variables must me unsigned long-int (32 bit).

so here is what i need to know,
1 how do i make something in ascii bytes to send off to this server ?

Python strings are full 8-bit byte strings. Whether you should retrict
bytes to ASCII bytes only depends on the situation. According to what you
said above, the server wants the number in binary rather than as ascii
digits.

Other than that, send "STX", the number, your message, and "ENX"
2 how can i make sure something is a 32bit unsigned long int. ?

Look at the struct and/or array modules. I suspect that you may have to
pay attention to whether the server want the binary in big or little endian
format.

Terry Jan Reedy
 
N

nephish

i think like byte 1 = 'S'
byte 2 = 'T'
and byte 3 = 'X'

still new at this, and thanks for the references

-sk
 
N

nephish

ok, part of what i have to do is know how many bytes will be sent. in
ascii is it one byte per character ?
like "password" would be 8 bytes long?
 
M

mensanator

ok, part of what i have to do is know how many bytes will be sent. in
ascii is it one byte per character ?
like "password" would be 8 bytes long?

Yes, but when someone says ASCII STX, they usually mean
the single byte control character. This is a non-printable character
that you can create with chr(2). And ETX would be chr(3) (as
mentioned by DLB, there is no such thing as ASCII ENX).
 
N

nephish

i know that those characters exist, the docs say that the server does
not want the special "ETX" and "STX" characters, but the 3 ascii
characters "STX" and "ENX" i am not sure why.
 
M

mensanator

i know that those characters exist, the docs say that the server does
not want the special "ETX" and "STX" characters, but the 3 ascii
characters "STX" and "ENX" i am not sure why.

What do you bet the server software was written by someone
who thought ASCII STX meant literally the characters "STX"?

I've seen stupider things.
 
B

Blair P. Houghton

What do you bet the server software was written by someone
who thought ASCII STX meant literally the characters "STX"?

Wouldn't explain the "ENX" instead of "ETX".
I've seen stupider things.

I give it 25% probability of being what you said, and 75% probability
that they didn't want to send any characters that couldn't be printed
in plaintext.

I've seen literally dozens of this kind of roll-your-own serial
protocol in the past few years, and they're all indicative that no two
are alike. Ever read the ARINC 429 documents? They'll curl your toes.

--Blair
"It's protocols all the way down."
 
D

Dennis Lee Bieber

Wouldn't explain the "ENX" instead of "ETX".

Unless they also thought STX was "STart X" and matched by "ENd X";
rather than the "TX" being a common abbreviation for "Transmission"..
--
 
N

nephish

i'm not sure, but its kinda been a pain in the hinder for a newbie like
me to find it in their docs.
thanks for all the help guys, let you know how it goes
 
N

nephish

ok, i am stuck again.

from the docs, the byte stream is supposed to look like this:

'S' 'T' 'X' [length indicator] [message type] [message] 'E' 'N' 'X'

the length indicator it says is a four byte integer number of a value N
( N would be how long the message body is )

the message type comes from a table in the docs. In my case, the
message type is 200. This is also supposed to be sent as a 4 byte
value.

so..... how do i make 200 occupy 4 bytes ?
 
S

Steve Holden

ok, i am stuck again.

from the docs, the byte stream is supposed to look like this:

'S' 'T' 'X' [length indicator] [message type] [message] 'E' 'N' 'X'

the length indicator it says is a four byte integer number of a value N
( N would be how long the message body is )

the message type comes from a table in the docs. In my case, the
message type is 200. This is also supposed to be sent as a 4 byte
value.

so..... how do i make 200 occupy 4 bytes ?

You use the struct module and make sure you specify the correct
"endianness" - bet you didn't bargain for all this when you started!

regards
Steve
 
B

Blair P. Houghton

so..... how do i make 200 occupy 4 bytes ?

Did you double-check that they didn't want "0200" in ascii
in the message?

As in:

STX0200................ENX

Because that's always possible. And, if you're _lucky_, they designed
the innards of the message so that "ENX" can never appear at random.
Though with a length parameter, you shouldn't have to worry about
that...though with a length parameter the ENX is redundant...

Argh!

I'm having flashbacks.

--Blair
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,219
Latest member
KristieKoh

Latest Threads

Top