hello all,
once again looking collective knowledge of forum solution problem.
communicating between raspberry pi (python 3.4) , arduino uno, or should say, trying.
here should happen....
send command string arduino "f25000,3500" new line character. uno reads byte-by-byte , echos pi. arduino processes data further , waits new command.
works first time (first run on pi's program, , boot on arduino. if re-run program on , on again, works fine.
however, when pi sends 2 or more commands uno things strange. instead of sending command - sends odd characters, possibly more should. earlier, first command fine, second, third , on commands turn jiberish.
first command working, seems though goes on between commands messes serial data.
things have noticed
on first communication, send 12 bytes , 12 bytes (not counting lf)
checking buffer amount 12 on first command
data being echoed uno identical data sent
on second communication, send 12 byte , 18 bytes back
checking buffer amount 18 on second command
data being echoed uno jiberish
have tried many different things no avail, however...
assuming had serial port , perhaps buffer data on pi side...
tried flushing buffers - didn't help.
tried opening port - sending data - closing port repeating - didn't help.
running without uno serial port monitor active - didn't help.
running without uno connected usb on computer (power adapter instead - no help.
suspect problem on pi side have not been able pinpoint issue.
sure hope can me out on one.
in advance,
chris d
python 3.4 code
arduino code (only relevant sections)
once again looking collective knowledge of forum solution problem.
communicating between raspberry pi (python 3.4) , arduino uno, or should say, trying.
here should happen....
send command string arduino "f25000,3500" new line character. uno reads byte-by-byte , echos pi. arduino processes data further , waits new command.
works first time (first run on pi's program, , boot on arduino. if re-run program on , on again, works fine.
however, when pi sends 2 or more commands uno things strange. instead of sending command - sends odd characters, possibly more should. earlier, first command fine, second, third , on commands turn jiberish.
first command working, seems though goes on between commands messes serial data.
things have noticed
on first communication, send 12 bytes , 12 bytes (not counting lf)
checking buffer amount 12 on first command
data being echoed uno identical data sent
on second communication, send 12 byte , 18 bytes back
checking buffer amount 18 on second command
data being echoed uno jiberish
have tried many different things no avail, however...
assuming had serial port , perhaps buffer data on pi side...
tried flushing buffers - didn't help.
tried opening port - sending data - closing port repeating - didn't help.
running without uno serial port monitor active - didn't help.
running without uno connected usb on computer (power adapter instead - no help.
suspect problem on pi side have not been able pinpoint issue.
sure hope can me out on one.
in advance,
chris d
python 3.4 code
code: select all
# routine send command arduino , execute # cannot repeatedly within loop time import sleep import serial import binascii err_cnt = 0 #start code open serial port ser = serial.serial("/dev/ttys0",9600, timeout =1) print("hello, waiting data...") if ser.isopen(): print(ser.name, "is open") print(ser) # create & send command arduino def send_command_to_pi(command_string, steps, sps): global err_cnt command_string = command_string ser.write(command_string.encode('ascii')) sleep(.2) val1 = steps + "," ser.write(val1.encode('ascii')) sleep(.2) val2 = sps check_val = command_string + val1 + val2 val2 = val2 + "\n" ser.write(val2.encode('ascii')) print("sent") sleep(.1) buffer = ser.inwaiting() print("buffer ",buffer) try: indata = bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) indata = indata + bytes.decode(ser.read()) print("s",check_val) print("r",indata) print("cmds ",x, " errors ",err_cnt) except: err_cnt += 1 print("error count =",err_cnt) sleep(1) #first command sent arduino print("------------------------") command_string = "f" steps = "25000" sps = "3500" send_command_to_pi(command_string, steps, sps) #wait long enough activity done on arduino sleep(20) #second command sent arduino print("------------------------") command_string = "r" steps = "25000" sps = "3500" send_command_to_pi(command_string, steps, sps)
arduino code (only relevant sections)
code: select all
... serial.begin(9600); // set serial library @ 9600 bps ... // fetch commands serial port if there if (serial.available() > 0) { inbyte = serial.read(); if (inbyte != 10 || inbyte != 13) { serial.write(inbyte); // echo pi sent in command } // input if letter, number, =,?,+ typed! if ((inbyte >= 65 && inbyte <= 90) || (inbyte >= 97 && inbyte <= 122) || (inbyte >= 48 && inbyte <= 57) || inbyte == 44 || inbyte == 61 || inbyte == 63) { serial_command.concat(inbyte); }// end serial.available // process command when nl/cr entered: // handle incomming commands raspberry pi if (inbyte == 10 || inbyte == 13) { inbyte = 0; // respond command: if (serial_command.startswith("f")) //forward motion command { temp1 = serial_command.substring(1, serial_command.indexof(',')); temp2 = serial_command.substring(serial_command.indexof(',') + 1); temp1.tochararray(carray, 10); cmd_steps = atol(carray); temp2.tochararray(carray, 10); target_sps = atol(carray); digitalwrite(leftwheeldir,l_fwd); digitalwrite(rightwheeldir,r_fwd); end_move_alert = 1; steps = 0; target_steps = cmd_steps; steps_to_go = cmd_steps; acc_flag = 1; command = 0; //response pi //serial.println(command); //serial.println(cmd_steps); //serial.println(target_sps); } else if (serial_command.startswith("r")) // reverse motion command { temp1 = serial_command.substring(1, serial_command.indexof(',')); temp2 = serial_command.substring(serial_command.indexof(',') + 1); temp1.tochararray(carray, 10); cmd_steps = atol(carray); temp2.tochararray(carray, 10); target_sps = atol(carray); digitalwrite(leftwheeldir,l_rev); digitalwrite(rightwheeldir,r_rev); end_move_alert = 1; steps = 0; target_steps = cmd_steps; steps_to_go = cmd_steps; acc_flag = 1; command = 1; //response pi //serial.println(command); //serial.println(cmd_steps); //serial.println(target_sps); else { if (!serial_command.equalsignorecase("")) { //serial.println("bad"); } } serial_command = ""; } } }// end main loop
are uart both running @ same voltage level? iirc, pi 3.3v, arduino 5v. might issue there.
raspberrypi
Comments
Post a Comment