Jump to content

Noob Help TCP HEX please


Recommended Posts

Hello, folks, I've been reading the help files and forums for a couple of days now.

I'm very sure this language can do what I want. I've managed to do parts of it myself in two other languages and failed in a few more. I'm here because I absolutely can't stand dot net, I don't want to deploy it on every machine in my network, etc.

I've done this is VB. I'd just like a few pointers in how to get a jump start in this language. What crosses me up is figuring out when the documentation and the programs are talking about hex, and binary, and datatypes, what is actually going on. Because on the disk, in memory, and across the wire, it's all binary anyway, isn't it? So, to the task ....

I have a black box. It talks TCP, has an IP address and a very simple server that listens on a port.

In order to get it to respond, it wants, for example:

First Byte: 0xAA

Second Byte: 06

Third Byte: 0x01

Fourth Byte: 0x01

Fifth Byte: 18

etc

The documentation states that the values with 0x are hex (of course), and the others are decimal.

I can do pretty much all of it myself, just want to know how to format what kind of variables or array to get the bitstream arranged properly going out on the wire.

In VB, it looked like this (hacked up to demonstrate it):

Dim myByteArray(lengthdeclaredelsewhere) as Byte //Explicitly, an array of bytes

Dim myByte0 as Byte = &HAA //Separated the bytes out for less typing during manipulation elsewhere

Dim myByte1 as Byte = 6

etc,

myByteArray(0)=myByte0 // Put the bytes into the array after setting their values

myByteArray(1)=myByte1

etc,

ClientSocket.Send(myByteArray) // Send the array out over the wire(with incomprehensible back flips elsewhere to make it work)

The ONLY reason I tried VB was because of explicit type declarations that allowed me to work on the data one byte at a time. This was after trying a few other basic-like languages and finding other obstacles. Like not enough control over TCP (that stopped me on one), or no bit shift operators etc (yes, I figured out the math and wrote a CRC checker in another! Then gave up because I couldn't get past the "no datatypes" obstacle). So I waded in .... and spent weeks trudging through dot net swamp trying to figure out how to do the simplest things .... But I have a real job too. I loathe dot net. I refuse to install it across our company. NO!

This nice language has everything I need. I just need to un-confuse myself on how to exactly perform the task of getting the data arranged the way I want it. I would like to be able to not have any confusion in my own mind about what the 1's and 0's are going out to my black box. It's an industrial controller. It uses 1's and 0's. To it, a byte is 8 bits and a word is 16 bits. That stuff I can handle. Ambiguousity in data types, I can't figure out.

For instance, function _StringToHex "returns an Hex String". Is this a string of ascii characters representing the hex values? If I send this, do the 1's and 0's going out represent the actual hex value, or ascii characters that read as hex? If I put in "A", and send it, does the other end get 1010, 10, or the ascii value for "A"?

Help, please!

Thanks,

--97T--

Link to comment
Share on other sites

Look up StringSplit in the Help File. Use a one byte per element array. Loop thru the array and write to your output file, with conversion and delimiters as required.

Let's turn that into code:

$file = @ScriptDir & "\data.csv"
$splitdata = StingSplit($recvieddata, "")
For $i = 1 to $splitdata[0]
   FileWrite($file, Asc($splitdata[$i])&",")
Next

#)

Link to comment
Share on other sites

use the BinaryString type $byte1= $chr(0xAA) ...

You can just concatenate yours bytes

TCP functions support this type and can be send as is :whistle:

OK, I'm wallowing around in this again.

In the example above, is $chr supposed to be the chr function, and there was a typo, or is it a variable used in some unusual way?

Assuming it was meant to say chr(0xAA) .....

Is it necessary when concatenating bytes to do this conversion? Why wouldn't the following work?

$byte1 = 0xAA ;hex --( is it necessary to store it as chr(0xAA), and why?)

$byte2 = 6 ;decimal

;Then

$outputstring = $byte1 & $byte2 ;concatenate

$outputstream = Binarystring($outputstring) ;convert

$TCPSent = TCPSend($MainSocket, $outputstring) ;send

;Or simplify it by combining functions where it will let me but not all functions let me do that

And, assuming that the example given used the chr() function because it is necessary,

then how do I get the program to read back or store the information in decimal form?

In other words, how do I un-convert from the ascii symbol for 0xAA to decimal 170, or 0xAA?

I'd like to be able to read what I'm sending for testing purposes.

I've tried hex, dec, and reading the help file.

If I'm recieving, and the first byte I recieve is 0xAA, will the program read that as decimal 170?

Thanks for your help, again! Sorry for being such a newbie at this language.

--97T--

Link to comment
Share on other sites

I answered my own question. It has to use the format Binarystring(chr($yournumber1) & chr($yournumber2)) because if you don't, it sends the value for a string. I guess.

I thought I'd put this in here in case anyone else tries to do this, or in case I forget!

Anyway, I got my first packet assembled and sent to the box, and the box responded with a packet.

Tomorrow, I have to figure out how to disasemble that little bugger, and then I'll be on my way.

I'm going to get some sleep now!

Thanks, you guys.

--97T--

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...