gandrewstone / LIN

The LIN protocol implemented over Arduino APIs (Serial and Digital IO)
119 stars 46 forks source link

Software Serial #4

Open tjb8907 opened 8 years ago

tjb8907 commented 8 years ago

I am trying to convert you code to use software serial, so I can use it the the analyzer board I built based on Zapta's project (https://github.com/zapta/linbus/tree/master/analyzer)

It uses a ATA6631 Lin Transceiver with the RX pin hooked do D2 and TX hooked to C2 (Analog 2, but used at digital)

Goal is to be able send a full frame at a given time. I have already reverse engineered the frame that I need.

Could you maybe give me some pointers?

Lin(LIN_SERIAL& ser=Serial,uint8_t txPin=1);

I know I would change the TX pin to 16 (A2). I tried defining a software serial by including the Library, and then

'Software Serial mySerial(2,16);'

and then changing ser=Serial to ser=mySerial, but it said mySerial was not not declared in this scope.

gandrewstone commented 8 years ago

In lin.h change this:

define LIN_SERIAL HardwareSerial

to:

define LIN_SERIAL SoftwareSerial

Now my software will use the SoftwareSerial object. This is only going to work if SoftwareSerial has the same member functions as HardwareSerial (I think it does but I forget).

Now in your program, construct your SoftwareSerial object, and pass it into my LIN code:

setup() { SoftwareSerial mySerial(2,16); Lin myLin(mySerial, 16); ... }

(and BTW your error is c++ conceptual, not specific to this LIN library -- study default parameters a bit more)

tjb8907 commented 8 years ago

Awesome. Got it to work. Thanks! Had to make a couple minor changes.

I put code you wrote about outside the setup function on my sketch, and I also had to change this:

Lin(LIN_SERIAL& ser=Serial ,uint8_t txPin=16); to this:

Lin(LIN_SERIAL& ser,uint8_t txPin=16);

I had some luck with it. I get frame errors a lot which I am guessing is a conflict with the actual module sending a frame with the same ID I am sending. But it did work some. the response would be delayed sometimes. Guessing maybe every once in a my frame hits it just right and responds to the master before the other??

Also noticed that when I captured data with my logic analyzer, the interbyte spaces are much larger on the frames I am sending vs the frames on the LIN. Is that just a limitation of my hardware? It can only transmit so fast. I did make sure the baud was right. Both are at 19200.

tjb8907 commented 8 years ago

So changed set up a hardware serial again, and that seems to make the spaces smaller. however I did notice that the bits aren't quite the same duration. the 0 bits are about 62 microseconds and the 1 bits are about 42 microseconds. Wondering if that makes a difference.

Anyway, I am going to play around with it a little more tomorrow and see if I can get it to respond faster. I want to avoid intercepting the existing module that produces the same frame ID because I need that to function at all times. I could probably get away with a transistor that switched off and interrupts the LIN going to that module for just a split second while I send the frame I want.

Logic Analyzer pic.

image