Go Down

Topic: Yun 9bit hardware + bridge? (Read 1 time) previous topic - next topic

aggrav8d

Hi!

I am making an Arduino talk to my vending machine.  The vending machine communicates in a 9 bit protocol.  On an UNO that's natively supported only through the hardware serial.  If the hardware serial is in use I can't get Serial.print() messages for debugging.  I've got a workaround that's really ugly and makes development crawl (vending > hardware serial > UNO A > software serial > UNO B > hardware serial > PC).  I'd like to find a better solution. 

Please confirm a few YUN features before I make my purchase(s):

(1) If I'm using a YUN to talk via hardware serial to the vending machine, can I still send/recv data through the bridge?
(2) Can activity on the linux-side cause the arduino side to reboot?  That's one of my least favorite features of the UNOs and MEGAs.  I can't have that in this application.

Thank you!
Dance as if no one is watching; post as if it is going to be read aloud as evidence in court.

sonnyyu

(1) If I'm using a YUN to talk via hardware serial to the vending machine, can I still send/recv data through the bridge?
Not with Yun but with Dragino Yun shield plus Mega.






sonnyyu

...
(2) Can activity on the linux-side cause the arduino side to reboot?  That's one of my least favorite features of the UNOs and MEGAs.  I can't have that in this application.
...
No.

Robin2

I am making an Arduino talk to my vending machine.  The vending machine communicates in a 9 bit protocol. 
Do you mean that there is a parity bit as well as 8 data bits?

I suspect my yet another software serial could be adapted to work with a parity bit.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

aggrav8d

@Robbin2 - yes, parity bit.  I have tried Alt9softserial.  I'll give yet another soft serial a test.
Dance as if no one is watching; post as if it is going to be read aloud as evidence in court.

sonnyyu

#5
Aug 08, 2015, 12:40 am Last Edit: Aug 08, 2015, 12:53 am by sonnyyu
@Robbin2 - yes, parity bit.  I have tried Alt9softserial.  I'll give yet another soft serial a test.

I thought "9 bit protocol" means cs9  ( data length is 9 bits, with parity bit will be 10 bits) and required special hardware support. if cs8 then a lot of hardware will support it?




aggrav8d

I may have got that wrong.  Here's the code that sets up the hardware serial to read the 9bit correctly.

Code: [Select]
#define BAUD 9600
#include <Wire.h>
#include <util/setbaud.h>

void setup() {
  // Set baud rate
  UBRR0H = UBRRH_VALUE;
  UBRR0L = UBRRL_VALUE;
  // Disable USART rate doubler (arduino bootloader leaves it enabled...)
  UCSR0A &= ~(1 << U2X0);

  // Set asynchronous
  UCSR0C = (0<<UMSEL01)|(0<<UMSEL00);
  // set 1 stop bit
  UCSR0C |= (0<<USBS0);
  // set no parity
  UCSR0C |= (0<<UPM01)|(0<<UPM00);
  // set 9bit
  UCSR0C |=(1<<UCSZ01)|(1<<UCSZ00);
  UCSR0B |= (1<<UCSZ02);

  UCSR0B |= (1<<RXEN0);  // Enable RX
  UCSR0B |= (1<<TXEN0);  // Enable TX
}


How would I do this with sss.ino?  set (sssBitCount == 9) and change the data types accordingly?

Thank you for your help.  I really appreciate that you take the time.

The MDB 4.2 protocol says the format is 1 start bit, 9 data bits, and 1 stop bit.
Dance as if no one is watching; post as if it is going to be read aloud as evidence in court.

aggrav8d

#7
Aug 08, 2015, 10:24 pm Last Edit: Aug 08, 2015, 10:26 pm by aggrav8d
Also, FWIW, here's the UART receive that goes with the previous code.

Code: [Select]

unsigned int USART_Receive() {
  // Wait for data to be received
  while (!(UCSR0A & (1<<RXC0)));
  // Get status and 9th bit, then data
  // from buffer
  unsigned char stat = UCSR0A;
  unsigned char resh = UCSR0B;
  unsigned char resl = UDR0;

  // If error, return -1
  if ( (stat & (1<<FE0)) | (stat & (1<<DOR0)) | (stat & (1<<UPE0))) {
    return -1;
  }

  // Filter the 9th bit, then return
  resh = (resh >> 1) & 0x01;
  return (((unsigned int)resh << 8) | resl);
}


am I reading those last two lines correctly - they're reversing the bit order?
Dance as if no one is watching; post as if it is going to be read aloud as evidence in court.

aggrav8d

Currently, I have your code modified like so:

Code: [Select]

  // This is one of the functions called by TimerFunctionP()
  // read the bit value first
  int sssNewBit = PIND & B00001000; // Arduino Pin 3 is pin3 in Port D
 
  // update the counter for the next bit interval
  OCR2A = TCNT2 + sssBaudTimerCount;
 
  if (sssBitCount == 11) {   // this is the stop bit
     // stop the bitIntervalTimer
     TIMSK2 &= B11111100;  // Disable Timer2 Compare Match A  and overflow Interrupts
     // enable Interrupt 0
     EIFR |= B00000010; // clear interrupt 1 flag - prevents any spurious carry-over
     EIMSK |=  B00000010;  // Enable Interrupt 1 to detect the next start bit
  }
 
  sssNewBit = sssNewBit >> 3; // gets the bit into posn 0
  sssNewBit = sssNewBit << (sssBitCount); // moves it to the correct place
  sssRecvByte |= sssNewBit;
 
  if (sssBitCount == 10) {
     // update the bufHead index
     // but prevent overwriting the tail of the buffer
     byte sssTestHead = (sssBufHead + 1) % sssBufSize;
     if (sssTestHead != sssBufTail) { // otherwise sssBufHead is unchanged
       sssBufHead = sssTestHead;
     }
     // and save the byte
     sssBuffer[sssBufHead] = (sssRecvByte >> 1)  & 0x1FF;
  }
 
  sssBitCount++;
}


I'm trying to throw out the start bit and the stop bit and keep just the relevant stuff, but I'm not 100% convinced yet that I'm doing it right.  Can you please confirm?
Dance as if no one is watching; post as if it is going to be read aloud as evidence in court.

mart256

What kind of communication port does your vening machine has? Is it a RS232 or a TTL UART?

aggrav8d

@mart256 - it has one that adheres to this protocol: http://www.coin-acceptor.com.cn/Upload/EditorFiles/technicalfile/Mdb_version_4-2.pdf
Dance as if no one is watching; post as if it is going to be read aloud as evidence in court.

mart256

I mean the voltage levels in the comm lines.

Robin2

How would I do this with sss.ino?  set (sssBitCount == 9) and change the data types accordingly?
It's a long time since I wrote it so I am no longer familiar with it. I suspect you are thinking along the correct lines.

Are you thinking of changing from byte to int to accommodate the 9 bits - that may be a solution.

However the parity bit is not part of the data - it is only there for quality control. You should and (?) the incoming bits and see if they match the parity bit (or something like that) and reject the byte if they don't match.

I know it is probably heresy to suggest this, but what would happen if you receive the 9 bits and just ignore the 9th bit ?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sonnyyu

#13
Aug 09, 2015, 05:34 pm Last Edit: Aug 09, 2015, 05:35 pm by sonnyyu
I may have got that wrong.  Here's the code that sets up the hardware serial to read the 9bit correctly.

Code: [Select]
#define BAUD 9600
#include <Wire.h>
...

...
If it ain't broke, don't fix it

move code into Mega

Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX); Serial 3: 15 (RX) and 14 (TX). Used to receive (RX) and transmit (TX) TTL serial data. Pins 0 and 1 are also connected to the corresponding pins of the ATmega16U2 USB-to-TTL Serial chip.

https://www.arduino.cc/en/Main/arduinoBoardMega2560

Make sure use Serial 1,2,3 but Serial, since Serial later will connect with Yun shield. Test it without Yun shield.







Go Up