0
votes

I have one hardware which uses Asynchronous serial interface protocol, 9600 baud rate, #Start bits 1,#Data bits 8, #Stop bits 1, Parity None. When I am communicating my hardware with a python script using a cp2102 device that time hardware doesn't respond but when I try to communicate the same hardware with Arduino nano then it works properly.

Working Arduino code:

void volt_currentTest()
{
 Serial.write(0xDD);
 Serial.write(0xA5);
 Serial.write(0x03);
 Serial.write(0x00);
 Serial.write(0xFF);
 Serial.write(0xFD);
 Serial.write(0x77);

  while(c != 0x77)
  {
    if(Serial.available())
    {
      if((c = Serial.read()) != -1)
      {
           Serial.println(c , HEX);
        d[i] = c;
        i++;
      }
    }   
  }
}

Python code:

import serial
ser = serial.Serial(
    'COM9', 9600,   
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE
)

'''data = b'\xdd\xa5\x03\x00\xff\xfd\x77'
ser.write(data)'''

ser.write(b'\xDD')
ser.write(b'\xA5')
ser.write(b'\x03')
ser.write(b'\x00')
ser.write(b'\xFF')
ser.write(b'\xFD')
ser.write(b'\x77')

'''
ser.write('\xDD\r\n'.encode())
ser.write('\xA5\r\n'.encode())
ser.write('\x03\r\n'.encode())
ser.write('\x00\r\n'.encode())
ser.write('\xFF\r\n'.encode())
ser.write('\xFD\r\n'.encode())
ser.write('\x77\r\n'.encode())
'''

while True:
    response = ser.read()

    '''response = ser.readline()
      '''
     print(response)

So Anyone having an idea why it's not working .....

1
Think about posting on this site: arduino.stackexchange.com - Blundering Philosopher
Do you have an issue with your read or write ? does the HW on the other side receives the data ? - olricson
yes HW receives the data and according to that data it sends the reply and communication is in hex conversation only - Unknown

1 Answers

0
votes

You probably need to call ser.flush() after all the write()s, but before the 'while' loop.

Python's serial.Serial uses buffered I/O by default – all your writes wait until you call flush(), after which they're sent in one batch.