Shop OBEX P1 Docs P2 Docs Learn Events
Issues receiving serial commands — Parallax Forums

Issues receiving serial commands

mike2mike2 Posts: 8
edited 2014-10-12 22:10 in Propeller 1
A lot of the code I've been finding online for sending and receiving data is using spin code.

I have a c project I'm working on in which I need to send and receive data to a python program over usb.

Here's my code. Does anyone know why it's not working?
#include <stdio.h>
#include <string.h>
#include <propeller.h>
#include <simpletools.h>


int main(void)
{
   // Wait some time
   waitcnt((CLKFREQ+CNT)*5);
   
   char buffer[300];
   int buflen = 300;
   
   serial *term;
   simpleterm_close();
   term = serial_open(31,30,0,57600);
   writeStr(term,"give_data\r");
   getStr(buffer, buflen);   
   waitcnt((CLKFREQ+CNT)*2);
   writeStr(term,"Data received.\r");  


  return 0;
}

Here's my python I run immediately after loading c code into ram using simpleide
import time
import serial


print "Communicating with propeller over COM4"


s = serial.Serial(
    port='COM4',\
    baudrate=57600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
    timeout=0)


while True:
    data = s.read(9999)
    if len(data) > 0:
        print 'Received:'+ data
        if "give_data" in data:
            print "Sending data to the propeller..."
            s.write("Here is some data\r")
    time.sleep(1)
s.close()

Output
C:\Users\mike\Desktop>python scom.py
Communicating with propeller over COM4
Received:give_data
Sending data to the propeller...
_

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2014-10-12 16:36
    FWIW, I can't see how getStr would be connected to term. I'd have used readStr instead.
  • mike2mike2 Posts: 8
    edited 2014-10-12 19:08
    Using readstr I get garbled data back...
    readStr(term, buffer, buflen); 
    
    C:\Users\mike\Desktop\>python scom.py
    Communicating with propeller over COM4
    Received:give_data
    Sending data to the propeller...
    Received:H½ii&#9570;ët°
    


    If I use the parallax serial terminal I can use C read functions like scanf, but I need to get python working.

    Same result running this on a linux host
    ubuntu@ubuntu:~$ sudo python scom.py
    Communicating with propeller over /dev/ttyUSB0
    Received:give_data
    Sending data to the propeller...
    Received:H&#65533;ii&#65533;&#65533;t
    
  • kuronekokuroneko Posts: 3,623
    edited 2014-10-12 20:04
    Just ran the following program, and after I send some response from the terminal I get the Data received message.
    #include <stdio.h>
    #include <string.h>
    #include <propeller.h>
    #include <simpletools.h>
    
    
    int main(void) {
        // Wait some time
        waitcnt(CLKFREQ*3 + CNT);
       
        char buffer[300];
        int buflen = 300;
       
        serial *term;
        simpleterm_close();
        term = serial_open(31, 30, 0, 115200);
        writeStr(term, "give_data\r");
        readStr(term, buffer, buflen);   
        waitcnt(CLKFREQ*2 + CNT);
        writeStr(term, "Data received [");
        writeStr(term, buffer);
        writeStr(term, "]\r");
    
        return 0;
    }
    
    terminal output:
    give_data
    [COLOR="#FF8C00"]user data from terminal[/COLOR]
    Data received [user data from terminal]
    
    Your delays look slightly off but shouldn't have an effect on the transfer as such. You could try a simple loopback first where the prop simply echos the incoming data.
  • mike2mike2 Posts: 8
    edited 2014-10-12 20:28
    I changed to use fdserial instead of serial and the problem resolved. I think it might be an issue with python.
    #include <stdio.h>
    #include <string.h>
    #include <propeller.h>
    #include <simpletools.h>
    #include <simpletext.h>
    #include "fdserial.h"
    
    
    int main(void)
    {
       // Wait some time
       pause(3000);
       
       char buffer[300];
       int buflen = 300;
       
       serial *term;
       simpleterm_close();
       term = fdserial_open(31,30,0,57600);
       writeStr(term,"give_data\r");
       readStr(term,buffer, buflen);   
       writeStr(term,"Data received.\r"); 
    
    
      return 0;
    }
    
    
    C:\Users\mike\Desktop>python scom.py
    Communicating with propeller over COM4
    Received:give_data
    Sending data to the propeller...
    Received:Here is some data
    Data received.
    
    
    
  • kuronekokuroneko Posts: 3,623
    edited 2014-10-12 21:21
    Just wondering, as serial is running unbuffered can you increase the number of stopbits on the host side and see if it makes a difference. If it's still too fast at least I'd expect slightly different garbage.
  • mike2mike2 Posts: 8
    edited 2014-10-12 22:10
    Your prediction is right on that. If you change the stop bit it changes the garbage that gets spewed back.

    [code]
    C:\Users\mike\Desktop>python kuronekotest.py
    Communicating with propeller over COM4
    Received:give_data
    Sending data to the propeller...
    Received:Hn,
Sign In or Register to comment.