String with Chr(0)

Just starting out? Need help? Post your questions and find answers here.
mcsalsa
User
User
Posts: 17
Joined: Thu Sep 28, 2006 1:20 pm

String with Chr(0)

Post by mcsalsa »

Dear All,

We are programming a small application that sends a string to the Serial Port, we are forced to send a string and some values of the string are chr(0).

For input we found the solution by reading "bytes" using "ReadSerialPortData(COMID, @bBuffer, 1)" ... is there any solution for sending a string?

Thanks in advance,
Last edited by mcsalsa on Tue Dec 10, 2013 9:42 am, edited 1 time in total.
User_Russian
Addict
Addict
Posts: 1519
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: String with Chr(0)

Post by User_Russian »

As a buffer, use an array rather than a string.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: String with Chr(0)

Post by netmaestro »

The array idea is good, alternatives would be allocated memory or a fixedstring (which can contain 0 bytes).
BERESHEIT
mcsalsa
User
User
Posts: 17
Joined: Thu Sep 28, 2006 1:20 pm

Re: String with Chr(0)

Post by mcsalsa »

Thank you very much!! I will try it and let you know.
infratec
Always Here
Always Here
Posts: 7581
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: String with Chr(0)

Post by infratec »

The answer is simple:

Code: Select all

WriteSerialPortData()
Bernd
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: String with Chr(0)

Post by IdeasVacuum »

If you have to cheat to get it to work, you can substitute Chr(0) with another character, so long as the receiving app (is there a receiving app?) can decipher.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: String with Chr(0)

Post by skywalk »

mcsalsa wrote:We are programming a small application that sends a string to the Serial Port, we are forced to send a string and some values of the string are chr(0).
You cannot have a string in PB where 'some' values = Chr(0). PB strings end at Chr(0). Build your output string in a buffer and write data just as you are reading data.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: String with Chr(0)

Post by blueznl »

skywalk wrote:
mcsalsa wrote:We are programming a small application that sends a string to the Serial Port, we are forced to send a string and some values of the string are chr(0).
You cannot have a string in PB where 'some' values = Chr(0). PB strings end at Chr(0). Build your output string in a buffer and write data just as you are reading data.
Not entirely true. You can build a string inside a fixed length string, but you cannot use build in string commands, as they all terminate on the chr(0) character.

But yes, if effect, using regular string commands you cannot create anything containing a chr(0).
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
BorisTheOld
Enthusiast
Enthusiast
Posts: 542
Joined: Tue Apr 24, 2012 5:08 pm
Location: Ontario, Canada

Re: String with Chr(0)

Post by BorisTheOld »

blueznl wrote:But yes, if effect, using regular string commands you cannot create anything containing a chr(0).
The lack of OLE string support has been the single biggest problem in our conversion from PowerBasic to PB.

In the real world, Chr(0) exists as a real character in most data sent to, and received from, devices. Manipulating such data is a non-issue with OLE strings.

Unfortunately, PB memory and string support is tied too closely to null-terminated strings. So after spending several hundred man-hours trying to build our own OLE-style string support into PB, I finally decided that we should do what we did for file I/O support -- use the powerful OLE features that already exist in PowerBasic and FreeBasic.

We're now in the process of creating a series of macros to link to OLE code in the two dynamic libraries we originally set up for file I/O -- a PowerBasic "support.dll" for Windows, and a FreeBasic "support.so" for Linux.
For ten years Caesar ruled with an iron hand, then with a wooden foot, and finally with a piece of string.
~ Spike Milligan
Post Reply