5 posts
Posted 19 November 2013 - 04:38 PM
Im working on programs that send data between computers, but by redstone (not rednet). The transmitter turns binary code into pulses on a wire, and the receiver reads the pulses. This is working fine. I just need to make an encoder/decoder (to use the alpha numeric system). Part of that would be to save the binary code, 8 at a time, and convert it. But how can i program it to save 8 bits(pulses ) at a time?
I have the binary output on the screen. For example, 01010001001101100011100100100001. How could i get the computer to remember the first 8 numbers (so it could then be converted), and then do the same for the next 8.
Im sorry if im not so clear. It can be complicated and im trying to make it simple. If anyone has any questions, feel free to ask.
504 posts
Location
Seattle, WA
Posted 19 November 2013 - 07:42 PM
If you can section the binary string that is recorded into a byte of 8 bits, then you can use string.byte (tonumber (yourByte, 2)), to convert it to a number and then a its character equivalent.
For example, if you have your byte being the number 5, 00000101, then the code would look like this:
local myByte = "00000101"
local function getCharacterFromBinaryByte (sByte)
return string.byte (tonumber (sByte, 2))
end
Edited on 19 November 2013 - 06:44 PM
5 posts
Posted 22 November 2013 - 07:36 AM
How would i go upon the receiving computer looking at it in sections though (as opposed to just throwing the bits on the screen, 1 bit at a time)? Because right now, its set up to transmit and receive at a 2hz clock cycle. Every 0.5 seconds that the TX cable is lit up, it reads the data cable.
—————————–
if rs.testBundledInput(DataSide, 1) == true
and rs.testBundledInput(DataSide, 2) == true
then print("1")
end
if rs.testBundledInput(DataSide, 1) == false
and rs.testBundledInput(DataSide, 2) == true
then print("0")
end
————————
This is only part of the receive code. After this, it waits, looks at the receive line, and if powered it runs this again.
131 posts
Posted 22 November 2013 - 12:06 PM
local function getCharacterFromBinaryByte (sByte) -- Function from Grim Reaper.
return string.byte (tonumber (sByte, 2))
end
local bits = {} -- Table to store received bits.
if rs.testBundledInput(DataSide, 1) == true and rs.testBundledInput(DataSide, 2) == true then
bits[#bits + 1] = 1 -- Store a 1.
end
if rs.testBundledInput(DataSide, 1) == false and rs.testBundledInput(DataSide, 2) == true then
bits[#bits + 1] = 0 -- Store a 0.
end
if #bits % 8 == 0 then -- When a full byte has been received, print its character and clear the bit storage table.
local stringRepresentation = table.concat(bits)
print(getCharacterFromBinaryByte(stringRepresentation))
for i = 1,8 do
bits[i] = nil
end
end
EDIT: You may need to change this
local stringRepresentation = table.concat(bits)
to this
local stringRepresentation = string.reverse(table.concat(bits))
Edited on 22 November 2013 - 11:21 AM