Requesting help with serial communication between Proto Board and Matlab (or other)
mneuert
Posts: 9
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:
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:
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:
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
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 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
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.
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):
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
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.
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.