Go Down

Topic: sharing Serial between instances of Library objs (Read 542 times) previous topic - next topic


Apr 08, 2009, 07:18 am Last Edit: Apr 08, 2009, 07:23 am by roypardi Reason: 1
Hi -

I created a Library of for a particular motor controller board because there is a lot of reading/writing to registers on the board and because I need two of these boards in a project, I figured it would be easier to create instances of my library object and just pass in the specific address/ID to each instance. Oh - this is with the Wiring board because I need two serial ports - one to the motor controllers and one to a PC.

It's working but the wrinkle comes with accessing/sharing the Serial port so each instance can do the communication it needs. I solved that by including two methods in my sketch and referencing them in the Library like this

Code: [Select]
extern void ReadBytes(unsigned char*, long*, int);
extern void WriteBytes(unsigned char*, int);

And it works! - for one instance at least - haven't hooked up the other board yet. The only problem (so far...) is that every other read comes back 0 - (I'll include the specific code below).

The deal with the board is to read a register, you first have to write to the board to tell it what register you want to read from. I tried putting in a delay between the write and the read but that didn't help.

If I have made any sense so far (...) does anyone have a suggestion of where I might put a delay, or some other method to insure that there my data reads come back with valid data.

A lot to wade through - but I thought going with a Library was a cleaner way to go. Maybe not?



Here's some code -

-- a method in the Library object to get the motor position:
Code: [Select]
int Gamoto::getmPosition(){
  return ReadReg(gxmPosition,3);

--the ReadReg method in that object
Code: [Select]
int Gamoto::ReadReg(int RegAdr, int RegLen) {
     // Reads a value from a Gamoto Register
  int Regvalue=0;
   long bytes_read;
     // Build the command
     wBuffer[0]= gxHEADER;
     wBuffer[1]= gxWRITE;
     wBuffer[2]= RegAdr;
     wBuffer[3]= RegLen;
     wBuffer[4]= CheckSum(wBuffer,4);
     // Send the command

     //delay(5); // this didn't work
     // Get the response
     if (bytes_read>0){
           // We got a response - now parse the result
     for (int i=0;i<RegLen;i++) {
           Regvalue += rBuffer[i+1] * (1<<(8*i));
           // Handle negative case
     if (Regvalue >= (1<<(8*RegLen))/2)
           Regvalue -= 1<<(8*RegLen);
     return Regvalue;

--and finally the ReadBytes routine in my sketch that gets called by the Library instance (this populates a buffer in the Library instance with the read data)

Code: [Select]
void ReadBytes(unsigned char* buffer, long* bytesRead, int len){

     int i;
     for(i = 0; i < len; i++){
       buffer[i] =  Serial1.read();

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131