UPDATE
Thank you!Using the suggested libraries, I updated the code. The comms in both directions for simple test data are working, though I can't seem to convert the packet received by the Arduino into a string correctly. Here is the output I see from my python terminal; I'd expect to receive back the first character of what was sent (i.e.: the "0", then "1"), rather than just the initialized value of "S".
SENT: 0.20
RCVD: S
SENT: 0.41
RCVD: S
SENT: 0.61
RCVD: S
SENT: 0.82
RCVD: S
SENT: 1.02
RCVD: S
SENT: 1.23
RCVD: S
SENT: 1.43
RCVD: S
SENT: 1.64
RCVD: S
Python code
import time
from pySerialTransfer import pySerialTransfer as txfer
if name == 'main':
try:
link = txfer.SerialTransfer('/dev/cu.usbmodem14201')
link.open()
time.sleep(2) # allow some time for the Arduino to completely reset
base = time.time()
while True:
time.sleep(0.2)
s = '%.2f' % (time.time() - base)
l = len(s)
for i in range(l):
link.txBuff[i] = s[i]
link.send(l)
while not link.available():
if link.status < 0:
print('ERROR: {}'.format(link.status))
response = ''
for index in range(link.bytesRead):
response += chr(link.rxBuff[index])
print('SENT: %s' % s)
print('RCVD: %s' % response)
except KeyboardInterrupt:
link.close()
**Arduino code**
#include "SerialTransfer.h"
char str[1000];
SerialTransfer myTransfer;
void setup()
{
str[0] = 'S';
str[1] = '\n';
Serial.begin(115200);
myTransfer.begin(Serial);
}
void loop()
{
// send bytes
myTransfer.txBuff[0] = str[0];
myTransfer.sendData(1);
delay(100);
if(myTransfer.available())
{
// receive bytes
byte bytes_to_read = myTransfer.bytesRead;
for(byte i = 0; i < bytes_to_read; i++)
str[i] = myTransfer.rxBuff[i];
str[bytes_to_read] = '\0';
}
else if(myTransfer.status < 0)
{
Serial.print("ERROR: ");
if(myTransfer.status == -1)
Serial.println(F("CRC_ERROR"));
else if(myTransfer.status == -2)
Serial.println(F("PAYLOAD_ERROR"));
else if(myTransfer.status == -3)
Serial.println(F("STOP_BYTE_ERROR"));
}
}
Thank you!