Go Down

Topic: Serial problems when using common rx/tx cable (Read 5707 times) previous topic - next topic

AlexanderB

Jul 16, 2010, 12:29 pm Last Edit: Jul 16, 2010, 02:05 pm by AlexanderB Reason: 1
Hi,

I am working on a project to build a telemetry sensor for my Jeti Duplex RC System. The sensor can be connected to the so called Jeti Box which is nothing more than a dumb terminal. The data format is 9600 9O1.

I can also connect the sensor to the receiver in my RC plane and the 2x16 chars are automatically routed to the box that is connected to the transmitter. So far so good. Sending strings to the box does work. Also sending alarm beeps work.

After receiving the string the box is supposed to send an ack byte in which the pressed keys (there are four keys on the board) are pressed to allow a simple menu.

The problem is: sending works, receiving a byte does not work.  I'm not able to receive a valid ack from the box: the first call of JetiGetchar returns that there was a buffer overflow and the second time I call it I always get 0xFE (which is not in the scope of what can be expected).

This is a one wire (TTL) communication: the only serial line is connected to the RX pin 0. Pin 0 and Pin 1 (RX to TX) are shortened with a 100 Ohm resistor to prevent any destructive current.

I tried everything. Double and triple checked the registers. No way. I also asked a friend who works on a similiar project (but not with Arduino). There are examples where it works.

Code: [Select]

#include <stdio.h>
#include <avr/io.h>

#define TX_COMPLETE (1<<TXC0)
#define GETCHAR_TIMEOUT_us 500000  // 50ms timeout bei Getchar

int lastJetiAnswer;

void jetiUartInit(void) {
 Serial.begin(9600);          // Set correct baud rate with Arduino Lib
 PORTD=0x03;                  //UART RX->PD0, TX->PD1
 DDRD=0x00;                   // Set all to input
 UCSR0A=0x00;
 UCSR0B=0x1C;
 UCSR0C=B00111110; // 00=asynch, 11=Odd, 1 1Stop, 11 9 Bit , 0 default
 UCSR0B &= ~(1<<TXEN0);  //disable TX  
 // UCSR0B &= ~(1<<RXEN0);  //disable RX  
 UCSR0B |= (1<<RXEN0);   //enable RX
}

void JetiTransmitByte(unsigned char data, boolean setBit9)
{
 if (setBit9 == true)
   UCSR0B |= (1<<TXB80); // Set Bit 9
 else
   UCSR0B &= ~(1<<TXB80);    //delete 9th Bit
 /* Put data into buffer, sends the data */
 UDR0 = data;
 while ((UCSR0A & TX_COMPLETE)==0); //wait for TX complete
 UCSR0A |= TX_COMPLETE; //clear TXC Flag
}


unsigned char JetiSensor(char line1[17], char line2[17], char alarmCode)
{
 unsigned char ack;
 int i;
 UCSR0B |= (1<<TXEN0);  //enable TX

 // Send Alarm Code first, if alarmcode <> 0
 if (alarmCode != 0) {
   JetiTransmitByte(0x7E, false);
   JetiTransmitByte(0x92, false);
   JetiTransmitByte(0x23, false);
   JetiTransmitByte(alarmCode, true);
 }
 
 // 34 Byte Data Package
 JetiTransmitByte(0xFE, false);
 for (i=0; i<16; i++) JetiTransmitByte(line1[i], true);
 for (i=0; i<16; i++) JetiTransmitByte(line2[i], true);
 JetiTransmitByte(0xFF, false);    
 JetiTransmitByte(0x00, false);    
 
 UCSR0B &= ~(1<<TXEN0);  //disable TX
//  UCSR0B &= ~(1<<RXEN0);  //disable RX  
//  UCSR0B |= (1<<RXEN0);   //enable RX

 ack = JetiGetChar();
 if (ack == 14) ack = JetiGetChar();
 return ack;
}

unsigned char JetiGetChar(void)
{
 unsigned long time = micros();
 byte stat, erg;
 /* Wait for data to be received */
 while ( (UCSR0A & (1<<RXC0)) == 0 )
 {
   if (micros()-time >  GETCHAR_TIMEOUT_us)
     return 12; // return, if timout occures
 }
 /* Get and return received data from buffer */
 stat = UCSR0A;
 erg = UCSR0B; // first bit 9: discard
 erg = UDR0;
 if ( stat & (1<<FE0) ) return 13;
 if ( stat & (1<<DOR0) ) return 14;
 if ( stat & (1<<UPE0) ) return 15;
 
 return erg;
}

void setup(void) {
 jetiUartInit();
 lastJetiAnswer= 99;
}

void loop(void) {
 int i, bytecount;
 char line1[17] = "FLAPS: UP       ";  // 17: allow \0 at the end
 char line2[17] = "GEAR : DOWN     ";
 char alarm1[17] = "ALARM  LINE 1   ";
 char alarm2[17] = "ALARM  LINE 2   ";
 char msg[10] = "     ";
 char tm[10] = "     ";

 sprintf(msg, "%u----", lastJetiAnswer);
 sprintf(tm, "%u-----", millis()/1000);
 for (i=0; i<4; i++) {
   if (tm[i] < 32) tm[i]=32;
   line1[i+12] = tm[i];
 }

 for (i=0; i<4; i++) {
   if (msg[i] < 32) msg[i]=32;
   line2[i+12] = msg[i];
 }

 // Senden
 if (millis()/1000 % 10 != 0)
   lastJetiAnswer = JetiSensor(line1, line2, 0);
 else {
   // lastJetiAnswer = JetiSensor(alarm1, alarm2, 'B');
   // delay(1000); // leave the alarm on the display
 }
 delay(20);

}


Any ideas most welcome. Thank you very much for having a look at the code!

Cheers Alexander

AlexanderB

#1
Jul 16, 2010, 12:33 pm Last Edit: Jul 16, 2010, 02:05 pm by AlexanderB Reason: 1
Additional Information: I tried disabling the RX while sending. That makes matters worse. I run into the timeout.

Cheers Alexander

tkbyd

#2
Jul 16, 2010, 01:40 pm Last Edit: Jul 16, 2010, 01:42 pm by tkbyd Reason: 1
The term "one wire" may be hurting the rate at which you are getting answers.

"1-Wire" is a trademark of Dallas Semiconductor, often expressed "one wire"... but what you are talking about is nothing to do with "1-Wire".

"Single channel" or "one way communication" may be less ambiguous, more helpful to your quest.

You may find that it is not too late to re-view your original post, and then click the "modify" link (upper right). If it isn't too late, then you (and only you, or a forum moderator) can change the title of this thread. You can usually also add "[Solved]" to threads, once relevant, which is a help to others.

AlexanderB

#3
Jul 16, 2010, 02:06 pm Last Edit: Jul 16, 2010, 02:08 pm by AlexanderB Reason: 1
Thank you. I wasn't aware of that. Changed the title. Hope this is more descriptive.

But it's not one way communication: it's just that the system uses only one cable for the communication (plus GND). So RX pin and TX pin are connected to the same one and only cable. :)

Cheers Alexander

AlexanderB

To whom it may concern: I solved the problem with a little help...

When the device that is connected to the UART uses the same wire for RX and TX it does not work with the boards that have USB on board since there is a resistor internally connected (don't ask me about details) that prevents that szenario from working.

I now tested it on the Mini Pro 328 board (I bought the external USB connector to program it) and it works like a charm.

Cheers Alexander

MatrixGTI

Hi All,
I'm a noob in Arduino probgramming and I need an help in replacing the JetiBox Display with and Arduino 2009 + LCD Keypad Shield Display. I only need to read the JetiTx serial string and display the data on the LCD keypad Shield in different order respect the original Jeti.
I know that my A2009 board is not able to read the single wire serial from the TX because the serial is shared with the USB and there is a pullup resistor in the circuit, I think about using a SoftwareSerial but I'm not able to write the code to set the SoftwareSerial working on Jeti Protocol.  :'(

Any help will be appreciated

Thanks in advanced  ;)

Flavio

nettworker

Long time gone, anything new?

I am working at the same problem.....

Go Up