writing in binary to a serial port 
Author Message
 writing in binary to a serial port

Hi,

I need to write something in binary to the serial port..  The write()
method seems to take a string.  I'm having a bit of a problem trying
to figure out how I should convert my info into a string.

Basically, the data consists of 4 hex "bytes" - here they are:

1C 02 00 00

Now.. how would I write that with the serial port?

Here's what I'm trying right now, but it doesn't seem to work:

  dim m as MemoryBlock
  m = NewMemoryBlock(4)

  m.byte(0) = &h1C
  m.byte(1) = &h02
  m.byte(2) = &h00
  m.byte(3) = &h00

  MySerialPort.write(m.StringValue(0,4))

Am I doing something wrong?

Any ideas?  Thanks..



Wed, 07 Dec 2005 05:50:44 GMT  
 writing in binary to a serial port

Quote:

> Here's what I'm trying right now, but it doesn't seem to work:

>   dim m as MemoryBlock
>   m = NewMemoryBlock(4)

>   m.byte(0) = &h1C
>   m.byte(1) = &h02
>   m.byte(2) = &h00
>   m.byte(3) = &h00

>   MySerialPort.write(m.StringValue(0,4))

> Am I doing something wrong?

Nope.  Your code is exactly correct.  This code would also be equivalent:

  MySerialPort.Write ChrB(&h1C) + ChrB(&h02) + ChrB(0) + ChrB(0)

If that doesn't seem to work, it's not because of any fault in this code.

Cheers,
- Joe

,------------------------------------------------------------------.
|    Joseph J. Strout         Check out the Mac Web Directory:     |

`------------------------------------------------------------------'



Wed, 07 Dec 2005 05:55:42 GMT  
 writing in binary to a serial port
Thanks Joe, I got it to work finally.. Turns out that my packet was
not formed correctly.
I have another question for you (or anyone else) however...

If I want to assign a single byte to have a certain value, say &hfe,
is it just as safe to assign it the decimal equivalent of 254?  Ie,
are these lines equivalent:

  m.byte(0) = &hfe

versus

  m.byte(0) = 254

I guess I'm getting a little confused because I assume that integers
are 4 bytes in REALBasic, however, I only want to assign one byte..

So, are both ways equally "safe," or is one better than the other?

Thanks!



Fri, 09 Dec 2005 03:53:45 GMT  
 writing in binary to a serial port

Quote:

> If I want to assign a single byte to have a certain value, say &hfe,
> is it just as safe to assign it the decimal equivalent of 254?  Ie,
> are these lines equivalent:

>   m.byte(0) = &hfe

> versus

>   m.byte(0) = 254

Yes, &hFE and 254 are just two different ways of writing the same
number.  They are identical to the compiler; use whichever you prefer.

Quote:
> I guess I'm getting a little confused because I assume that integers
> are 4 bytes in REALBasic, however, I only want to assign one byte..

You are assigning only one byte, because that's what MemoryBlock.Byte
does.

Cheers,
- Joe

,------------------------------------------------------------------.
|    Joseph J. Strout         Check out the Mac Web Directory:     |

`------------------------------------------------------------------'



Fri, 09 Dec 2005 11:00:09 GMT  
 writing in binary to a serial port

Quote:
> You are assigning only one byte, because that's what MemoryBlock.Byte
> does.

Ahh - of course!  That makes sense.. ;-)

Thanks.



Fri, 09 Dec 2005 13:18:23 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. send binary data through serial port

2. binary through serial port

3. Sending pure binary out a serial port

4. Binary data over Serial Port

5. Serial Port Writing with CDD 3 DOS

6. How do I read/write serial port?

7. Writing bytes out of Serial Port

8. During serial port communication (writing/transmission) is...

9. Serial Port Write

10. Serial Port Write Problem

11. writing/reading from serial port

12. Serial port, flushes after write

 

 
Powered by phpBB® Forum Software