salve a tutti,
ho un apparecchio con uscita seriale che riesco a leggere con questo adattatore RS422 to TTL (MAX490ESA).
l'apparecchio ha tre fili, due dei quali sono alimentazione 12v ed il terzo il segnale in uscita.
ho collegato il grd dell'apparecchio al A e il cavo segnale al B dell'adattatore
apparecchio------------------------ RS422/TTL ---------- arduino
grd ------------------------------------ A. TX -------------- RX
sgl ------------------------------------ B. RX --------------TX
12V ----- +12v NC X 5V ------------- 5V
NC Y GRD ---------- GRD
con questo sketch:
void setup() {
Serial.begin(115200);
delay(1000);
// speed, mode, rx, tx
Serial2.begin(9600, SERIAL_8N1, 26, 32);
delay(1000);
void loop() {
recvBytesWithStartEndMarkers();
showNewData();
}
void recvBytesWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
byte startMarker = 0x7C; // "|"
byte endMarker = 0x0A; // (LF) "/n"
// byte startMarker = 0x0A; // "/n"
// byte endMarker = 0x0D; // (CR) "/r"
byte rb;
while (Serial2.available() > 0 && newData == false) {
rb = Serial2.read();
if (recvInProgress == true) {
if (rb != endMarker) {
receivedBytes[ndx] = rb;
ndx++;
if (ndx >= numBytes) {
ndx = numBytes - 1;
}
}
else {
receivedBytes[ndx] = '\0'; // terminate the string
recvInProgress = false;
numReceived = ndx; // save the number for use when printing
ndx = 0;
newData = true;
}
}
else if (rb == startMarker) {
recvInProgress = true;
}
}
}
void showNewData() {
if (newData == true) {
// Serial.print(" ... HEX Value.. ");
for (byte n = 0; n < numReceived; n++) {
Serial.print(receivedBytes[n], HEX);
Serial.print(' ');
if (receivedBytes[numReceived - 3] == 0x36 && receivedBytes[numReceived - 6] == 0x30) {
unit = 0; // CM ARRI 60
}
if (receivedBytes[numReceived - 3] == 0x37 && receivedBytes[numReceived - 6] == 0x30) {
unit = 1; //FT ARRI 70
}
if (unit == 0 && receivedBytes[numReceived - 6] == 0x31) {
cm = receivedBytes[numReceived - 4] * 100 + receivedBytes[numReceived - 3] * 10 + receivedBytes[numReceived - 2];
}
if (unit == 1 && receivedBytes[numReceived - 6] == 0x31) {
ft = receivedBytes[numReceived - 5] * 10 + receivedBytes[numReceived - 4];
inc = receivedBytes[numReceived - 3] * 10 + receivedBytes[numReceived - 2];
}
}
Serial.println();
Serial.print ("cm ");
Serial.println (cm);
Serial.print ("ft ");
Serial.println (ft);
Serial.print ("inc ");
Serial.println (inc);
newData = false;
}
}
ho un bel output sul monitor seriale dei dati che escono dall'apparecchio!!
il "network" è composto dall'apparecchio e dall'arduino e basta.
la domanda è:
posso sostituire l'adattatore RS422/TTL con un RS485/TTL(SP485EEN-L)?
grazie mille e scusate per la domanda forse banale ... ma su questi due protocolli in rete c'è troppa roba e mi confondo....