1. Brent Albrecht
  2. PowerBuilder
  3. Monday, 24 January 2022 14:46 PM UTC

I'm using PB 2021 build 2703 on Windows 10 to communicate with a serial device through the COM4: port, using CreateFile and WriteFile API calls.  Everything works great, but the device I'm communicating with requires a character value 0 in a lot of it's commands.  I'm passing a string value to WriteFile so am not able to include the character value of 0 in it.  The declaration of WriteFile is below 

Function Long WriteFile (long hFile, ref string lpBuffer, long nNumberOfBytesToWrite, ref long lpNumberOfBytesWritten, ref string lpOverlapped) Library "kernel32" alias for "WriteFile;Ansi"

I'm hoping somebody is able to tell me how to make this work ?

Brent Albrecht Accepted Answer Pending Moderation
  1. Tuesday, 25 January 2022 15:30 PM UTC
  2. PowerBuilder
  3. # 1

I changed my program to insert the character values into a byte array and then convert the byte array to a blob and pass it into the external function, and it's working now.  Thanks a lot for your help!

Comment
  1. John Fauss
  2. Tuesday, 25 January 2022 16:08 PM UTC
That's great news, Brent - Thanks for letting us know how you resolved the issue!
  1. Helpful
  1. Miguel Leeuwe
  2. Tuesday, 25 January 2022 18:10 PM UTC
Please mark as resolved.
  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Monday, 24 January 2022 21:18 PM UTC
  2. PowerBuilder
  3. # 2

Hi Bent;

  Try this example:  https://answers.sap.com/questions/11867509/readingwriting-to-com-ports.html

   Just substitute COM4 for COM1 in the able example (in your case) and for the file write operation use CHAR(0)

HTH

Regards ... Chris

Comment
There are no comments made yet.
John Fauss Accepted Answer Pending Moderation
  1. Monday, 24 January 2022 17:37 PM UTC
  2. PowerBuilder
  3. # 3

Hi, Brent -

By convention, the null (all binary zeroes) character is a terminator for the string.

Although I have not done what you are wanting to accomplish, I believe you will have to construct the data stream you are wanting to write in a PB Blob variable and pass that to the WriteFile WinAPI function. This might be tedious, as you may have to convert the string data to ANSI as you build the Blob contents, since I'm unsure whether PB will perform the Unicode 16LE to ANSI conversion on the contents of a Blob that your External Function Declaration is requesting via the ";ansi" keyword.

An alternative to consider would be to use a PB Character array and pass the array to the WriteFile API function with the ";ansi" keyword included. The Character datatype in PB is limited to Unicode only, but specifying ";ansi" may allow it to be converted. I'm not 100% positive this will work flawlessly, as the zero-character may mess things up. You'll have to experiment.

Best regards, John

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.