Serial data help

This is the place for queries that don't fit in any of the other categories.

Serial data help

Postby ben021681 » Thu Jun 06, 2013 4:24 am

I am trying to capture serial data from a wireless receiver.

The code I am using is:
Code: Select all
import serial
import time
s = serial.Serial('/dev/ttyS0',9600)

while 1:
   if s.inWaiting():
      val = s.read(s.inWaiting())
      val2 = " ".join("{0:d}".format(ord(c)) for c in val)
   print val2



prints (Small Sample of Data):

    28 5 0 0 16 49
    28 5 0 0 16 49
    28 5 0 0 16 49
    114 18 1 253 10 149 0 249
    164 194 0 0 65 0 0 48
    60 60 105
    114 18 1 253 10 149 0 249
    164 194 0 0 65 0 0 48
    62 62 109
    114 18 1 253 10 149 0 249
    164 194 0 0 65 0 0 48
    63 63 111
    28 5 3 0 16 52
    114 26 192 250 209 64 0 249
    164 194 0 0 60 23 66 151
    192 0 71 166 151 101 0 16
    47 47 249
    114 26 192 250 209 64 1 253
    10 149 0 1 60 23 66 151
    192 0 71 166 151 101 0 16
    42 42 46
    114 26 192 250 209 64 0 249
    164 194 0 0 60 23 66 151
    192 0 71 166 151 101 0 16
    46 46 247
    28 5 3 0 16 52
    28 5 0 0 16 49
    28 5 0 0 16 49
    114 26 192 247 223 110 0 249
    164 194 0 0 60 21 66 136
    184 82 66 100 69 35 0 16
    62 62 174
    114 26 192 247 223 110 1 253
    10 149 0 1 60 21 66 136
    184 82 66 100 69 35 0 16
    59 59 231
    114 26 192 250 209 79 0 249
    164 194 0 0 60 23 66 136
    230 102 70 71 90 195 0 16
    53 53 82
    114 26 192 250 209 79 1 253
    10 149 0 1 60 23 66 136
    230 102 70 71 90 195 0 16
    41 41 121
    114 26 192 250 209 79 0 249
    164 194 0 0 60 23 66 136
    230 102 70 71 90 195 0 16
    52 52 80 114 26 192 250 209
    79 1 253 10 149 0 1 60
    23 66 136 230 102 70 71 90
    195 0 16 41 41 121 114 26
    192 250 209 79 0 249 164 194
    0 0 60 23 66 136 230 102
    70 71 90 195 0 16 55 55
    86 114 26 192 250 209 79 1
    253 10 149 0 1 60 23 66
    136 230 102 70 71 90 195 0
    16 41 41 121


This code is working (sort of) but the receiver is picking up data that is from some other wireless device of unknown type so I am unable to specify how many bytes belongs in the string.

The strings are supposed to be:

    114 26 192 250 209 79 0 249 164 194 0 0 60 23 66 136 230 102 70 71 90 195 0 16 52 52 80

    114 26 192 250 209 79 1 253 10 149 0 1 60 23 66 136 230 102 70 71 90 195 0 16 41 41 121

and discard anything else such as

    28 5 0 0 16 49

    28 5 3 0 16 52


I tried this:

Code: Select all
import serial
import time
import re
s = serial.Serial('/dev/ttyS0',9600)

while 1:
   if s.inWaiting():
      val = s.read(s.inWaiting())
      val2 = " ".join("{0:d}".format(ord(c)) for c in val)

   if re.match('114 26', val2):
      newval2 = " ".join("{0:d}".format(ord(c)) for c in val)
      print newval2


But it gets stuck in a loop and starts printing the data over and over.

    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253
    114 26 192 250 209 79 1 253

can anyone help me with this?

Thanks!
Last edited by ben021681 on Thu Jun 06, 2013 3:19 pm, edited 3 times in total.
ben021681
 
Posts: 4
Joined: Thu Jun 06, 2013 3:50 am

Re: Serial data help

Postby octopez » Thu Jun 06, 2013 8:58 am

Please use code tags! Thank you.
Greetings and all the best,
Octopez

Strange women lying in ponds distributing swords is no basis for a system of government.
User avatar
octopez
 
Posts: 25
Joined: Fri Feb 08, 2013 11:26 pm

Re: Serial data help

Postby ben021681 » Thu Jun 06, 2013 2:14 pm

octopez wrote:Please use code tags! Thank you.


Done!
ben021681
 
Posts: 4
Joined: Thu Jun 06, 2013 3:50 am

Re: Serial data help

Postby MichelFJM » Thu Jun 06, 2013 2:52 pm

Hello

In your loop, your second test should be in the first test block.
MichelFJM
 
Posts: 19
Joined: Wed May 22, 2013 1:41 pm

Re: Serial data help

Postby ben021681 » Thu Jun 06, 2013 3:07 pm

I am sorry but I am not sure what you are asking. Please let me know and I will do. Thanks!
ben021681
 
Posts: 4
Joined: Thu Jun 06, 2013 3:50 am

Re: Serial data help

Postby MichelFJM » Fri Jun 07, 2013 1:05 pm

Hello

On my computer I obtain nothing with this but I think that :
Code: Select all
import serial
import time
import re
s = serial.Serial('/dev/ttyS0',9600)
while 1:
   if s.inWaiting():
      val = s.read(s.inWaiting())
      val2 = " ".join("{0:d}".format(ord(c)) for c in val)
      if re.match('114 26', val2):
        newval2 = " ".join("{0:d}".format(ord(c)) for c in val)
        print newval2

is closer to what you want.
MichelFJM
 
Posts: 19
Joined: Wed May 22, 2013 1:41 pm

Re: Serial data help

Postby ben021681 » Fri Jun 07, 2013 5:23 pm

Here is the situation:

When I use:

Code: Select all
import serial
import time
s = serial.Serial('/dev/ttyS0',9600)
while 1:
   if s.inWaiting():
      val = s.read(s.inWaiting())
      val2 = " ".join("{0:d}".format(ord(c)) for c in val)
      print val2


I am able to get the serial data from the receiver with no problems.

The problem I am running into is that the data being received has different lengths depending on the device type transmitting the data.

If I look at a string such as:

114 26 192 250 209 79 1 253 10 149 0 1 60 23 66 133 19 51 70 79 35 175 0 16 45 45 53

The first byte of 114 tells me this is one of my devices.
The second byte tells me the length of the string excluding the last byte (last byte is the checksum) making this message 27 bits long.

Now when I look at the string:

114 18 1 253 10 149 0 249 164 194 0 0 65 0 0 48 64 64 113

The second byte is 18 meaning the message is 19 bytes long.


But there is also a string that looks like:
28 5 5 0 16 54
And is irrelevant and not needed so is to be ignored.

While reviewing the raw data from the serial device the strings are mashed together (I believe because the serial timeout before the string is completed) and I get something that looks like:

114 26 192 250 209 79 1 253 10 149 0 1 60 23
66 133 19 51 70 79 35 175 0 16 45 45 53 114 26 192 250 209
79 0 249 164 194 0 0 60 23 66
133 19 51 70 79 35 175 0 16 55 55 10
28 5 5 0 16 54

I know I need to do something like read(size=27), read(size=19)
But I am unsure how to write the code.
I can see that the serial data must be read, look for the first byte for 114, look for the second byte and see if it is ether 27 or 19 bytes long and then print the string in its correct length and ignore anything else that is not in this format.

My goal once this has been accomplished is to then assign each byte in the string a value so it can then be written to MySQL.

I hope this makes my goal a little clearer.

Any and all help is appreciated.
Thanks!
ben021681
 
Posts: 4
Joined: Thu Jun 06, 2013 3:50 am

Re: Serial data help

Postby MichelFJM » Mon Jun 10, 2013 7:10 am

Hello

Based on what I understood, I would propose the loop :
Code: Select all
while 1:
   if s.inWaiting():
      port = s.read(1)
      number = s.read(1)
      message = s.read(ord(number)-1)
      if ord(port)==114 :
          val2 = " ".join([str(ord(c)) for c in port+number+message])
          print val2
      else :     
          print 'discared ',str(ord(port)),port+number+message

But don't now if it is sinful.
MichelFJM
 
Posts: 19
Joined: Wed May 22, 2013 1:41 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron