Go Down

Topic: Proof of concept (Read 2 times) previous topic - next topic

ThunderBird

Well, I hopefully got it to work now, sorta. My idea was to first read nine bytes on the eth port, the length of the id string each reader sends out. then the first nine bytes are passed into the idstring variable, after which next 16 bytes are read, which are passed to the serial port, as they are the payload code. This should ensure that the two strings are properly separated.
The compiler gives me an error, as the eth port reads into a separate variable to buffer it, which is initialised without a size, but is increased as the port reads data (the code contains an incrementing counter, which at one point is used as the size of the buffer string).

Code: [Select]
int bytesread;
char bufferstring[] = {}
later:
bufferstring[bytesread]


This means the program compiles with an error that conversion from char[0] to char[9] is invalid, but I think this is actually just a result of the compiler being unable to follow the program as it actually executes, so it should not be a problem when actually deployed, no?

I know it will all be easier to imainge and understand once I get some code up, but that will only happen tomorrow, as I'm working without a net access on my own computer this weekend.

AlphaBeta

Code: [Select]
int bytesread;
char bufferstring[] = {}
later:
bufferstring[bytesread]


Need to be more like:

Code: [Select]
const byte bytesread = 9;
char bufferstring[bytesread] = {0};


This way the compiler knows the size of bufferstring when creating space (allocating) for it.
:)


ThunderBird

#17
Jul 26, 2009, 12:12 pm Last Edit: Jul 26, 2009, 01:33 pm by thunderbird Reason: 1
Yes, I thought of that too, but all input from the eth port is buffered in that string, both the 9-byte ID, and the 16-byte payload. That's why I had to work around like this, as it has to deal with both a 9 and a 16-byte string.

Code: [Select]
 char idstring [9] = {};
char bufferstring[] = {};
 char payloadstring[16] ={};
 int temp = 0;
 int bytesread = 0;
 if (client.available() > 0)
 {
   if ((temp = client.read()) == 27)
   {
     bytesread = 0;
     while (bytesread<9)
     {
       if (client.available() > 0)
       {
         temp = client.read();
         if (temp == 9)
         {
           break;
         }
         bufferstring[bytesread] = temp;
         bytesread++;
         if (bytesread == 9)
         {
           idstring = bufferstring;
         }
       }
     }
     bytesread = 0
       while (bytesread<16)
     {
       if (client.available() > 0)
       {
         temp = client.read();
         if (temp == 16)
         {
           break;
         }
         bufferstring[bytesread] = temp;
         bytesread++;
         if (bytesread == 9)
         {
           payloadstring = bufferstring;
         }
       }
     }
   }
 }


 lcd.print(payloadstring);
 Serial.print(payloadstring);
 bufferstring = 0;
}


Here's the whole separating code part. Kinda convoluted and messy, but I've just started programming in C :D

AlphaBeta

#18
Jul 26, 2009, 06:57 pm Last Edit: Jul 26, 2009, 06:59 pm by AlphaBeta Reason: 1
You still need to define the bufferstring with a size.

char bufferstring[16] = {'\0'}; //replace 16 with the maximum length of bufferstring

ThunderBird

Well, it's been a while since I last visited this forum, or at least it feels like a lot of time. Since then, this project went from speculation into an actual, implementable coding project, being done on both the Arduino platform, and on PC in C/C++/mySQL. So yeah, it developed a bit...:D

Still, since this would be my very first time programming something of this kind, I would like a bit more help: the codes seem to compile fine, but I would like to see if human eyes can spot any irregularities/improvements in my code.

Code: [Select]
Reader code: http://pastebin.com/m19ee2851
Mediator code: http://pastebin.com/m5c2f236f


Thank you very much for your time, and your names will be included in the source. :)

madrang

#20
Aug 04, 2009, 11:07 am Last Edit: Aug 04, 2009, 11:31 am by madrang Reason: 1
instead of using different char for each arduino you could use a device id store on the eeprom and read it a start, each recived message would be sent this way

**edited [8bit function] [id] [data] [ack]

i currently have a class that can handdle this if you want, it was started as a project for the serial port but you can use it for any thing... so you would be able to have up to 255 client and also if you want help i can provide a windows vb.net server code for you....

i will make a demo on the arduino soon for the id thing and i am still working on the wiki for my class..

http://code.google.com/p/serialhandler/

also your arduino code i really repetitive i could be much more smaller.... and i cant find void loop(){}

i don't understand why you use a mediator one arduino could = one device that can read talk to the server and trigger door alarm and all for one turnstiles

ThunderBird

You mean an Arduino would be capable of doing a mySQL query? I needed the mediator because the code that the does the actual verification runs on a PC, and this way, I could have fewer connections to the actual server, and I could handle all the readers with in one station. Although I've been looking into using an XBee module on each one to transmit data wirelessly to the PC, but that would probably mean rewriting the code from scratch (unless the XBee appears to the PC as an ordinary serial port).

I have no loops because I'm using a state machine architecture, the looping is done by a call to the first state at the end of the last one. AlphaBeta's FSM library made it very easy to construct such a state machine, it's way harder on PC. Plus, this makes for easy automatic flow control of the execution, easier than if-then-else statements

madrang

#22
Aug 05, 2009, 11:32 am Last Edit: Aug 05, 2009, 11:42 am by madrang Reason: 1
an xbee is a wireless serial... also you dont need serial, if you have a network port on your arduino, because each device could connect to your server and talk to it directly. but if you use a serial port the mediator is still not needed as i can make a server using the serial port and it will talk to you sql or have is own database. also if you want i can help you with the server part.

ThunderBird

The server help would probably be much appreciated, thank you :)

Yes, I do need serial, because the RFID reader itself, a parallax model, communicates via the serial protocol.

madrang

#24
Aug 05, 2009, 11:43 am Last Edit: Aug 05, 2009, 11:44 am by madrang Reason: 1
then it will be a server useing serial port

madrang

i just look at you reader code again and it look like yes you are using serial but not for talking to the pc only for the rfid, so you don't need a mediator as your ethernet shield can talk to your computer trough a server and do all the needed stuff.

madrang

now i have my tcp/ip server but i am not sure that the arduino can connect to it, but i just by an ethernet shield and i should receive it soon also if you want send me an email and i will send the program to you.

Go Up