Need help with eg.SerialThread

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
miljbee
Experienced User
Posts: 146
Joined: Fri Mar 27, 2009 1:29 pm
Location: Orléans, France

Need help with eg.SerialThread

Post by miljbee » Fri Feb 26, 2010 9:13 am

Hello,

I have a power meter. It's a currencost envi (alias cc128), see here for details : http://www.currentcost.com/product-cc128.html

This power meter as a serial connection to output some data. Details of the data sent are there :
http://www.currentcost.com/cc128/xml.htm
It's really simple, there is nothing to write to the serail port, we just have to read the serial port.

When I got this device I wrote a small C# program to write down to files every bytes this device outputs. It has been really simple: one cc128 class with a serial port in it and a receive event that writes received data to files. After that, I have extended the C# prog to send event to eventghost based on the real time power consuption reported by the cc128.
I simply launch eg withe the -e cmd param from the c# prog.

Now, I think that it would be better to have an eg plugin to manage the data sent by this device.

So I searched a bit, I found this :
http://www.eventghost.org/docs/eg/eg.Se ... rialThread
And have explored a few other plugins that seem to read data from a serial port.

At this point, the project seems to me really simple. I quickly wrote something like this :

Code: Select all

eg.RegisterPlugin(
    name = "CC128",
    author = "miljbee+egCC128@gmail.com",
    version = "0.0.1",
    kind = "other",
    description = "This plugin generates events from the serial interface of the CC128. The CC128 is a power monitor. More info here : http://www.currentcost.com/product-cc128.html"
)

class CC128Plugin(eg.PluginBase):
	def __init__(self,port):
		self.port=port
		self.serialThread=None
		
	def __start__(self,port):
		self.port=port
		self.serialThread=eg.SerialThread()
		self.serialThread.SetReadEventCallback(self.OnReceive)
		self.serialThread.Open(port, 57600,'8N1')
		self.serialThread.Start()
	def __stop__(self):
		self.serialThread.Stop()
	
	def __close__(self):
		self.serialThread.Stop()
	
	def Configure(self, port=0):
		panel = eg.ConfigPanel(self)
		portCtrl = panel.SerialPortChoice(port)
		panel.AddLine("Port:", portCtrl)
		while panel.Affirmed():
			panel.SetResult(portCtrl.GetValue())
	
	def OnReceive(self, serial):
		buffer = ""
		while True:
			b = serial.Read(1, 0.1)
			if b == "\n":
				break
			buffer+=b
			[b]print buffer[/b]
		#Process the received data block stored in the buffer
It doesn't work, and I can't understand why !
the buffer gets filled with data, but not all chars send by the device are in the buffer. Further more, it seems that they are not in the order they are sent by the device.

as an exemple, if I expect to receive something like :
<msg>some data</msg>,
the buffer might contain something like :
<gme<m>

I am pretty sure I am missing something evident, but I can't get it. Also, note that I am new to python.

I have also tried the generic serial plugin, and it works out of the box. I won't use it, because I don't want to generate events each time I receive a data block.

Any help would be greatly appreciated.

Regards,
miljbee
TCP Events : A Better Network Event Sender/Receiver Plugin.
The Network Event Sender/Receiver in C#
Get events in EG from Google Calendar.

Post Reply