Shop OBEX P1 Docs P2 Docs Learn Events
Requesting help with serial communication between Proto Board and Matlab (or other) — Parallax Forums

Requesting help with serial communication between Proto Board and Matlab (or other)

mneuertmneuert Posts: 9
edited 2013-07-01 10:26 in Learn with BlocklyProp
Hi all,

I'll preface this by saying that while I have a fair bit of programming in Matlab and Python under my belt, and some electronics knowledge, I'm very new to serial communication. It doesn't seem to complicated in principle, but I'm still getting used to all the jargon, etc.

I'm working on a project that uses an infra-red (IR) sensor as a trigger to cause a sound file to play on a computer. I'm pretty sure it's possible to do everything I want on the Proto Board itself: keep the sound files on an SD card, send sound signals using the proto board, etc. That is something I think would be very interesting to get into down the road, but for now, I'm just interested in:
  • having the IR trigger a voltage change
  • have that voltage change read by the board
  • have the board tell the computer the trigger has been activated, and
  • have the computer play a sound file
I've managed to build a working circuit that uses an IR LED as a source and a phototransistor as a sensor. The IR beam hitting the sensor corresponds to a low voltage state (binary state 0), while breaking the beam corresponds to a high voltage state (binary state 1).

I've written a program to load onto the Proto Board that reads these voltages on pin 0, and communicates them to the Parallax Serial Terminal.

Code here:
CON
_xinfreq = 5_000_000
_clkmode = xtal1+pll16x               'The system clock is set at 80MHz


OBJ
  pst: "Parallax Serial Terminal"     'call parallax serial terminal object


'PUB Main | T1, T2, TT
PUB Main


  pst.start(115200)             'start terminal


  dira[0]~                      'set P0 as an input


  repeat
    pst.str(string("the binary state of Pin 0 is",9))
    pst.Bin(ina[0],1)           'give the binary state of P0    note, 0- beam unbroken, 1- beam broken
    pst.str(string(10))
    pst.str(string(13))         'terminator bit is the line feed (LF) character


    'pst.Chars(pst#NL,1)
    'pst.Chars(pst#NL,1)
   waitcnt(clkfreq/2+cnt)      'waits 0.5s after completing the code to restart loop

I figured my next step is to try and reproduce the messages displayed on the Parallax Serial Terminal using a script in some language I'm familiar with. My first go-to has been Matlab, as that's what I'm most familiar with. I've worked through a bunch of the documentation, and have been trying this script:
s = serial('COM3','BaudRate',115200)
fopen(s)
out = fscanf(s);
fclose(s)
delete(s)
clear s

The problem I'm having is that Matlab doesn't read anything - it keeps timing out, and 'out' is being assigned an empty list. As I mentioned, I'm fairly new to serial communication, and apart from ensuring I'm using the correct COM port and baud rate, I really don't know where to start troubleshooting - i.e. is the problem on the Spin side, Matlab side, or both? I think I'm using the right terminating character, but I'm not sure...I really wish I could be of more help here...

Any help or advice would be much appreciated, and I'd be happy to provide any info that would help you in helping me ;)

I guess I should mention that, as I'm also familiar with Python, I'd be willing to try PySerial if someone else is more familiar with that.

Much thanks!

-Mark

Comments

  • kwinnkwinn Posts: 8,697
    edited 2013-04-14 11:53
    If the program outputs data to the computer using PST you know the propeller, serial connection, and computer are working so the only thing left is the receiving software on the PC end. I am not too familiar with Matlab or Python but I would suggest checking to see that you are using the correct comm port and the correct settings. On the receiving end make sure that there are no special characters or data format requirements such as start/sync characters or CR/LF etc. at the end of the message.
  • mneuertmneuert Posts: 9
    edited 2013-04-16 08:16
    Thanks kwinn,

    That helps a bit. I'll Look into the Matlab side of things for now - they have pretty good documentation, and a forum of their own.

    In the meantime, I'll leave this as unsolved in the hopes that someone with experience using Python/Matlab will see this and have some suggestions.
  • mneuertmneuert Posts: 9
    edited 2013-04-19 09:01
    Hello kwinn (or anyone else who may be able to help),

    I've been playing around a bit more on the Matlab side, and have found that you can display all serial port settings of a serial port object that you make, like this (note that many of the fields, e.g. terminator, are left blank in the output below because I didn't assign anything to them this tmie):
    ByteOrder: [ {littleEndian} | bigEndian ] 
        BytesAvailableFcn: string -or- function handle -or- cell array
        BytesAvailableFcnCount
        BytesAvailableFcnMode: [ {terminator} | byte ]
        ErrorFcn: string -or- function handle -or- cell array
        InputBufferSize
        Name
        ObjectVisibility: [ {on} | off ] 
        OutputBufferSize
        OutputEmptyFcn: string -or- function handle -or- cell array
        RecordDetail: [ {compact} | verbose ]
        RecordMode: [ {overwrite} | append | index ]
        RecordName
        Tag
        Timeout
        TimerFcn: string -or- function handle -or- cell array
        TimerPeriod
        UserData
        
        SERIAL specific properties:
        BaudRate
        BreakInterruptFcn: string -or- function handle -or- cell array
        DataBits
        DataTerminalReady: [ {on} | off ]
        FlowControl: [ {none} | hardware | software ]
        Parity: [ {none} | odd | even | mark | space ]
        PinStatusFcn: string -or- function handle -or- cell array
        Port
        ReadAsyncMode: [ {continuous} | manual ]
        RequestToSend: [ {on} | off ]
        StopBits
        Terminator
    

    Is there a way to display all this info on the parallax side (i.e. to the Parallax Serial Terminal), so I can see what the serial settings are on the Proto Board? This way I can compare more than just the Baud Rate and Com Port Number.

    Thanks!

    -MN
  • Mike GreenMike Green Posts: 23,101
    edited 2013-04-19 09:35
    Really the only thing relevant to the PST side of things is the Baud and that's determined by the parameter to pst.start, in this case 115200 Baud. The FullDuplexSerial I/O routines always use no flow control, no parity, 8 data bits, and 1 stop bit. These are not configurable. They're determined by the code itself.

    Extra stop bit time (like 1.5 or 2) is sometimes used when the Baud is high and the receiving end needs extra time to process the data before the next character starts. As long as the receiver has enough time, it doesn't matter how much time the transmitter uses for the stop bit(s). A PC, because it's fast and has lots of buffer space available, shouldn't ever need more than 1 stop bit.
  • mneuertmneuert Posts: 9
    edited 2013-07-01 10:26
    Much thanks, Mike.

    I ended up giving up on Matlab, and used the PySerial module in Python (link here), which is working. Setting the serial parameters was a piece of cake; the module is very well documented.

    Here is my code, in case anyone reading this would like to know how to communicate serially with their computer via PySerial.
    import serial
    ser = serial.Serial()
    ser.baudrate = 115200
    ser.port = 'COM3'
    ser.open()
    ser.flushInput()
    count = 0
    
    
    while True:
        ser.flushInput()
        output = ser.read(1)
        print output
        if count >= 10:
            ser.close()
            break
        elif output == '1':
            #print 'Beam at P0 has been broken'
            count += 1
            #print count
            continue
        elif output == '2':
            #print 'Beam at P1 has been broken'
            count += 1
            #print count
            continue
    
Sign In or Register to comment.