Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:40 27 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Sending binary data via COM (PicoMite)

Author Message
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 172
Posted: 09:03am 25 Oct 2023
Copy link to clipboard 
Print this post

Hi,

I am trying to send binary data via a COM port. The data contains zeros and it appears that the sending stops if a zero occurs in the data (because a zero does not make sense in a string?).

This is my example code, it sends position commands to a servo controller:
Option Base 0
Option Explicit
Option Escape
Option Default None

SetPin GP5, GP4, COM2
Open "COM2:57600" As #5
Pause 500
Print #5, "\&84\&01\&70\&2E"
Print Lof(#5)

Pause 1000
Close #5


If the second byte (=the servo number) is not zero (e.g., \&01), it works: The respective servo moves; Lof(#5) = 252, indicating that the whole sequence was put into the out buffer.

If the second byte is zero (\&00), the servo does not move and Lof(#5) = 255, suggesting that the sequence was incomplete?

How do you send zero-containing binary data? I am sure, I am missing something.
Any help is appreciated.

Best
Thomas
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9122
Posted: 09:43am 25 Oct 2023
Copy link to clipboard 
Print this post

Try chr$(0) for the zero character or use bin2str$ to create the string
Edited 2023-10-25 19:47 by matherp
 
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 172
Posted: 10:02am 25 Oct 2023
Copy link to clipboard 
Print this post

  matherp said  Try chr$(0) for the zero character or use bin2str$ to create the string


Wow - that works. Did not expect that to be qualitatively different from \&00, that why I did not think of trying it

Thanks!
 
pwillard
Senior Member

Joined: 07/06/2022
Location: United States
Posts: 292
Posted: 12:36pm 26 Oct 2023
Copy link to clipboard 
Print this post

A "0" and a '\00' are very different with regards to the ASCII encoding performed with serial data transfers.  For example, the ASCII NUL character '\00' is used to denote the end of the string in C or C++. When a programmer uses a "0" (Ascii character for 0) it is treated as 0x30 in hexadecimal number notation. The decimal equivalent is 48.
Edited 2023-10-26 22:38 by pwillard
 
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 172
Posted: 12:58pm 26 Oct 2023
Copy link to clipboard 
Print this post

  pwillard said  A "0" and a '\00' are very different with regards to the ASCII encoding performed with serial data transfers.  For example, the ASCII NUL character '\00' is used to denote the end of the string in C or C++. When a programmer uses a "0" (Ascii character for 0) it is treated as 0x30 in hexadecimal number notation. The decimal equivalent is 48.


Yes, thanks. I see.

In the manual it says "\&hh The byte whose value is given by hh… interpreted as a hexadecimal number". Hence, I expected "\&00" = Chr$(0). And indeed:

> option escape
> ? Hex$(Asc(Chr$(0)), 2), Hex$(Asc("\&00"), 2)
00      00
> ? Asc(Chr$(0)) = Asc("\&00")
1


But in my example (see first post) it was not equivalent.

Best
Thomas
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9122
Posted: 03:28pm 26 Oct 2023
Copy link to clipboard 
Print this post

The OPs' original code should have worked but it looks like the parser has a bug. I'll look at it for the next RC
 
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 172
Posted: 02:56pm 27 Oct 2023
Copy link to clipboard 
Print this post

  matherp said  The OPs' original code should have worked but it looks like the parser has a bug. I'll look at it for the next RC


Thanks!
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024