0

I am trying to sent data to an Arduino Nano via serial using python, but it doesn't work yet. Is there a way to ensure that what I am trying to send was sent? (This is in order to understand where the problem is - in the sending, or in the receiving).

The python code:

import serial
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
ser.write("testing".encode())
try:
    while 1:
        response = ser.readline()
        print (response)
except KeyboardInterrupt:
    ser.close()

The Arduino code:

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    int incoming = Serial.read();
    Serial.print("Character recieved: ");
    Serial.print(incoming, DEC);
  } 
}
  • 1
    Several ways, however why not just show your code? There may be an obvious mistake. – joan Mar 6 '16 at 12:37
  • @joan I have added the code – Yigal Mar 6 '16 at 12:48
  • Try ser.writeline("testing".encode()) or ser.write("testing\n"). It's not unusual for output to be buffered internally ny Linux until a new line is seen. – joan Mar 6 '16 at 14:15
  • Already tried, but it didn't work. this is why I want to know if there is a way to check if the data was sent - want to get where the problem exactly is – Yigal Mar 6 '16 at 14:39
2

There are several ways to monitor the transmitted data.

A logic analyser would let you see the signal and should be able to decode the actual serial data.

piscope will let you see the signal (the pattern of start, stop, and data bits).

piscope serial data

The above shows "hello\n\r" at 9600 bps captured by piscope.

The pigs utility will let you decode the data being transmitted on TXD.

The simplest for your purposes is probably the pigs utility.

The following commands will display the data being transmitted.

$ sudo pigpiod # start daemon

$ pigs slro 14 9600 8 # monitor 14 at 9600 baud 8 data bits

$ echo "hello" >/dev/ttyAMA0

$ pigs slr 14 100
7 104 101 108 108 111 13 10

$ pigs -a slr 14 100
0

$ echo "hello" >/dev/ttyAMA0

$ pigs -a slr 14 100
7 hello\x0d\x0a
| improve this answer | |
  • If I want to check if I received an answer using pigs, I just need to do beforehand: pigs slro 14 9600 8 , and then: pigs -a slr 15 100 ? – Yigal Mar 9 '16 at 11:42
  • Yes, the first command just starts monitoring GPIO 14 for serial traffic (8 bits, 9600 baud). The second command (14 not 15) checks to see what has been received on the GPIO and consumes up to 100 characters from the buffer. – joan Mar 9 '16 at 12:31
  • I meant int the comment to the RX monitoring (was wrong with the first 14, supposed to be 15), instead of the TX monitoring you demonstrated. Is it: pigs slro 15 9600 8 , pigs -a slr 15 100, for the RX monitoring – Yigal Mar 9 '16 at 12:56
  • Yes, use the Broadcom number of the GPIO you want to monitor. So 14 for the TXD signal and 15 for the RXD signal. If you are not sure of the baud rate use piscope to measure the bit lengths. – joan Mar 9 '16 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.