1

I am trying to use MSMQ to send a NMEA message to a C# sever. This NMEA message (string) is to be base64 encoded and then sent as a byte array.

The problem I am running into is that in MSMQ my byte array is showing as:

03 00 00 00 00 00 00 00 ........
3C 00 00 00 00 00 00 00 <.......
01 00 00 00 00 00 00 00 ........
03 00 00 00 00 00 00 00 ........
3F 00 00 00 00 00 00 00 ?.......
01 00 00 00 00 00 00 00 ........
03 00 00 00 00 00 00 00 ........
78 00 00 00 00 00 00 00 x.......

Oppose to what I'm looking for:

3C 3F 78 6D 6C 20 76 65 <?xml ve
72 73 69 6F 6E 3D 22 31 rsion="1
2E 30 22 3F 3E 0D 0A 3C .0"?>..<
62 61 73 65 36 34 42 69 base64Bi

A sample of my code:

# base64 encode the message
b64_nmea = base64.b64encode(nmea)

# Adding xml prolog.. used by the server
b64_nmea = '<?xml version="1.0"?>..<base64Binary>' + b64_nmea + '==</base64Binary>'

# creates a byte array from b64_nmea
bytearray_nmea = bytearray(b64_nmea)

# Then to send the message:
qinfo = win32com.client.Dispatch("MSMQ.MSMQQueueInfo")
computer_name = os.getenv('COMPUTERNAME')
qinfo.FormatName = "direct=os:" + computer_name + "\\Private$\\test"
queue = qinfo.Open(2, 0)
msg = win32com.client.Dispatch("MSMQ.MSMQMessage")
msg.Label = "test"
msg.Body = bytearray_nmea
msg.Send(queue)
queue.Close()

My question is: where do these null values come from and are they caused by an error in the byte array creation or is my method for dispatching to MSMQ going wonky?

I have tried quite a few variations with bytearray().. the above code is going back to one of my less "extreme" attempts. Any insight is appreciated.

This problem can be solved by using subprocess.Popen() on an exe that queues my messages, however it would be great to have a solution using only Python.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Infoblack
  • 51
  • 5

1 Answers1

1

Sprinkle some print statements to observe what python is doing.

These work fine,

import base64
nmea="hello, world"
print len(nmea)
b64_nmea = base64.b64encode(nmea)
print len(b64_nmea)
print b64_nmea
b64_nmea = '<?xml version="1.0"?>..<base64Binary>' + b64_nmea + '==</base64Binary>'
print len(b64_nmea)
print b64_nmea
print bytearray(b64_nmea)

Your problem is somewhere in the following,

#send the message:
computer_name = os.getenv('COMPUTERNAME')

You call win32com.client.Dispatch twice, with different MSMQ instances. The msg initialization seems sane. The qinfo calls seem sane. You might want to check that the syntax is msg.Send(queue) (could be queue.Send(msg)?).

These seem reasonable,

qinfo = win32com.client.Dispatch("MSMQ.MSMQQueueInfo")
msg = win32com.client.Dispatch("MSMQ.MSMQMessage")

These lines seem safe,

qinfo.FormatName = "direct=os:" + computer_name + "\\Private$\\test"
msg.Label = "test"
msg.Body = bytearray_nmea

I would look closer at these (first),

queue = qinfo.Open(2, 0)
msg.Send(queue)
queue.Close()
ChuckCottrill
  • 4,360
  • 2
  • 24
  • 42