bladox.com Forum Index bladox.com
Turbo SIM Toolkit Adapter Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do I interface Turbo Lite 2 to an RS-232 device?

 
Post new topic   Reply to topic    bladox.com Forum Index -> Development
View previous topic :: View next topic  
Author Message
rhamel
Guest





PostPosted: Fri Aug 04, 2006 1:23 am    Post subject: How do I interface Turbo Lite 2 to an RS-232 device? Reply with quote

I need to interface the Turbo Lite 2 to a barcode scanner using an RS-232 port.

I have no idea how to go about reading the data from the barcode scanner. I have used RS-232 devices before, but never without a device driver provided by an operating system.

Is there some sample code which shows how to read data from a RS-232 port?

Where on the Turbo Lite 2 would I attach the TXD, RXD and GND pins of the RS-232 port? What would I have to do, to avoid damaging the Turbo Lite 2 with respect to voltages coming from the RS-232 device?

Please forgive my ignorance of the subject.

Thanks in advance for your help,

Richard
Back to top
pz
Guest





PostPosted: Fri Aug 04, 2006 8:55 am    Post subject: Re: How do I interface Turbo Lite 2 to an RS-232 device? Reply with quote

rhamel wrote:
I need to interface the Turbo Lite 2 to a barcode scanner using an RS-232 port.

I have no idea how to go about reading the data from the barcode scanner. I have used RS-232 devices before, but never without a device driver provided by an operating system.

Is there some sample code which shows how to read data from a RS-232 port?
Quote:


Yes, please download latest AA source: http://www.bladox.com/pub/autoalarm-1.9.26.tar.gz

The TM2 board has extra "baby"mcu atmega168 doing rs232 and handling accelerometer. It communicates with main mcu via i2c. For you the interface is in uart2i2c.[ch] - you can see how it's used in autoalarm.c
The "baby" kernel has also gps parser build in so that AA gets parsed data already. It can be turned off (not used) and then you have raw access to rs232 data.


Where on the Turbo Lite 2 would I attach the TXD, RXD and GND pins of the RS-232 port? What would I have to do, to avoid damaging the Turbo Lite 2 with respect to voltages coming from the RS-232 device?


There are 8 holes for optocoupled i/o and 5 for RS232 (read from optocouplers):
gnd
n.c.
n.c
rxd(in)
txd(out)

As for voltage - there is max232 to have proper rs232, please see datasheet of max232.

Quote:

Please forgive my ignorance of the subject.

Thanks in advance for your help,

Richard


Our fault to document things properly.
Back to top
rhamel
Guest





PostPosted: Sun Aug 13, 2006 12:37 am    Post subject: Error in compiling autoalarm-1.9.26 Reply with quote

Pavel,
when I try compiling autoalarm-1.9.26, I get the following error messages:-

autoalarm.c:587: error: `TURBO_SIM_TOOLKIT_ADAPTER' undeclared (first use in this function)
autoalarm.c:596: error: `TURBO_SIM_TOOLKIT_ADAPTER_EVO' undeclared (first use in this function)

These errors are repeated several times in the make output (along with some warnings).

I cannot find any file containing these macros. What values should I provide for them?

Thanks for your help,
Richard
Back to top
pz
Guest





PostPosted: Sun Aug 13, 2006 9:10 am    Post subject: Reply with quote

Please download latest tubo-devel at:
http://www.bladox.com/pub/turbo-devel-1.2.1-060809.tar.gz

It's headers/lib only, no examples.
Back to top
rhamel
Guest





PostPosted: Sat Aug 19, 2006 11:44 am    Post subject: I'm desperate now Reply with quote

Pavel,
sorry to be such a wimp, but I've been going at this for a couple weeks now and I'm not getting anywhere. I don't have any experience with embedded programming, so I need to know which functions do what.

What I'm trying to do is,

1. initialise communication between a barcode reader via its serial port -> to an AutoAlarm board with an RS232 port

2. go into a 'forever' loop waiting for someone to scan a barcode

3. pass the result to the send_sms() function so the barcode can be sent to some other phone

Now, I can send data via SMS without problem, but
I can't figure out the sequence of steps to get the UART to barcode serial port working.

Is it one of the uart2i2c functions? Which one?

Something like :-

init_uart2i2c(<some_address>)
for(;; )
{
buf[0] = '\0';
uart2i2c_rx_data(buf, sizeof(buf));
if (strlen(buf) != 0)
{
send_sms(buf);
}
}

That's conceptually what I want to do, but for the life of me, I can't get it to work.

Any guidance you can give, would be great. A snippet of source code would be fantastic Smile

Thanks again,
Richard
Back to top
pz
Guest





PostPosted: Sat Aug 19, 2006 3:35 pm    Post subject: Re: I'm desperate now Reply with quote

rhamel wrote:
Pavel,
sorry to be such a wimp, but I've been going at this for a couple weeks now and I'm not getting anywhere. I don't have any experience with embedded programming, so I need to know which functions do what.


Hey, don't call yourself wimp and always ask immediately rather then wasting weeks!

Quote:

What I'm trying to do is,

1. initialise communication between a barcode reader via its serial port -> to an AutoAlarm board with an RS232 port


Can you post some info on protocol used by the barcode reader? What is being sent? How long is the data? What is the init procedure? And esp. what speed and uart setting are used?

Quote:

2. go into a 'forever' loop waiting for someone to scan a barcode


You will have to use ACTION_STATUS and set the polling to e.g. 5 sec. It means that you will check if there was anything sent by reader and be able to send sms in 5 sec intervals. But nothing will be lost even if there were scan between this period - the baby mcu (m168) doesn't sleep and buffers it for you.

The question now is the barcode protocol on serial port. In case reader just sends a data (like gps does) then you can use existing code. If not - i.e. there is some handshake or what - then new firmware for baby mcu will be needed. This is no problem, you can reflash baby from main mcu as you wish. You can e.g. leave baby to do some parsing if needed (as current baby kernel does for gps).

But let's see if the current (for you simpler) option can be used, post info on barcode protocol.

Maybe the easiest would be connect PC serial port and just see if you can send/receive chars.

Quote:

3. pass the result to the send_sms() function so the barcode can be sent to some other phone

Now, I can send data via SMS without problem, but
I can't figure out the sequence of steps to get the UART to barcode serial port working.

Is it one of the uart2i2c functions? Which one?

Something like :-

init_uart2i2c(<some_address>)
for(;; )
{
buf[0] = '\0';
uart2i2c_rx_data(buf, sizeof(buf));
if (strlen(buf) != 0)
{
send_sms(buf);
}
}

That's conceptually what I want to do, but for the life of me, I can't get it to work.

Any guidance you can give, would be great. A snippet of source code would be fantastic Smile

Thanks again,
Richard


In autoalarm.c in ACTION_INIT there is something like this:
Code:

       init_uart2i2c (0x7E);
//        enableParserGps ();  don't enable parser for barcode reader
//      clearGpsDataGps ();
        if (rb (&pers_mem->baudrate) == 1)
        {                       //4800
          uart2i2c_write_ubrr (103);
        }
        else if (rb (&pers_mem->baudrate) == 2)
        {                       //9600
          uart2i2c_write_ubrr (51);
        }
        else if (rb (&pers_mem->baudrate) == 3)
        {                       //19200
          uart2i2c_write_ubrr (25);
        }
        else
        {                       //38400
          uart2i2c_write_ubrr (12);
        }


Then use uart2i2c_read_rxfifo_data() to read data, uart2i2c_read_rxchars() to get number of chars in buffer.

Btw. have you tried GPS? You would see how it works, there is also some code commented out in autoalrm.c to send something to uart - you could try to catch this on PC. So you would see something is going on.
Back to top
rhamel
Guest





PostPosted: Sun Aug 20, 2006 12:31 pm    Post subject: Reply with quote

Hi Pavel,
I'm not sure I understand what you mean by ACTION_STATUS and poll interval. I see ACTION_STATUS in some of the sample code, but I don't understand how it is invoked. Nor am I sure how to set the poll interval.

You asked about the protocol the barcode reader uses. I'm not sure about that either. I can tell you that when I use 'minicom' to connect to the serial port that the barcode reader is on, and I swipe a barcode through it, minicom displays 'A12345'. The '12345' is what the barcode is supposed to return. I have no idea why it generates an 'A'.

The pitiful documentation I have on the barcode reader says :-

RS232, full-duplex, 8N1, 9600

then, it says

1. The start character is factory defined as an "STX" (02h).
2. The CR/LF character serve to bring the received screen text back to the left.
3. The "ETX" (03h) character denotes the end of current transmission.

There's a diagram which I guess, represent a packet of some sort.

STX (02h)
CARD ID (which I guess is the barcode itself)
CR (0Dh)
LF (0Ah)
ETX (03h)

Anyhow, I am attaching my code here. Can you take a look and tell me where I am grossly mistaken, please?

Quote:
/*
* Copyright (C) 2003 BLADOX, s.r.o. All rights reserved.
*
* This file is part of an example program for Turbo. This example
* program may be used, distributed and modified without limitation.
*
*/

#include <config.h>
#include <turbo/turbo.h>

// not sure if these are needed
#include <avr/sfr_defs.h>
#include <avr/io.h>

#include <stdlib.h>
#include <string.h>

#include "uart2i2c.h"

u8 PROGMEM msg[] = "BARCODE GOES HERE"; // SMS Msg
u8 PROGMEM phonenum[] = "1234567" ; // phone number

//===============================================
//
// secret parameter X that makes send_sms() work
//
//===============================================

#define PARAMX 0x12
#define MY_SLAVE_ADDRESS 0x7E

void action_menu (Menu_selection_data *data)
{
if (data->item == 0) // Scan barcode here
{
display_text_raw("SCAN BARCODE here", Q_DISPLAY_TEXT_USER_CLEAR);
action_sms ();
}
else // add phone number here
{
//========================================================
//
// placeholder for adding phone number to internal storage
//
//========================================================
display_text_raw("add phone here", Q_DISPLAY_TEXT_USER_CLEAR);
}
}

void action_sms (void *data)
{
//=============================
//
// allocate space for SMS text
//
//=============================
u8 *tmp = malloc (100);
u8 i;
u8 barcode[6];

//===========================================================
//
// convert string to phone number that cellphone understands
//
//===========================================================

u8 *fone = str2msisdn (phonenum, MSISDN_ADN, MEM_R);

//===============================================
//
// display_text_raw
// clears the screen of the phone
// positions the cursor at the first line
// displays the text
// waits for user to press 'ok'
//
//===============================================
display_text_raw("hit here", Q_DISPLAY_TEXT_USER_CLEAR);
barcode[0] = 0;
//=============================================================
//
// supposed to be scanning here
// do I have to send something to the scanner first? RTS? CTS?
//
// the presence of this while loop here causes the phone
// to display "WAIT" and a progress bar for about 10 seconds
// then it clears the screen and displays OK/BACK
// pressing OK does nothing
// pressing BACK returns to the menu level before I entered
// the app
// so it looks like the app crashes completely
// there must be another mechanism to do a loop
// IDLE TASK??
//
//=============================================================
while(barcode[0] == 0)
uart2i2c_read_rxfifo_data (barcode, 6);
barcode[6] = '\0';

// app crashes at this point

display_text_raw("after scan", Q_DISPLAY_TEXT_USER_CLEAR);
display_text_raw(barcode, Q_DISPLAY_TEXT_USER_CLEAR);

//=========================================
//
// copy the SMS message to the text buffer
//
//=========================================

memcpy (tmp, barcode, sizeof(barcode));
// memcpy (tmp, msg, sizeof(barcode));
// memcpy (tmp, msg, sizeof (msg));

//===========================
//
// change message encoding??
//
//===========================

dcs_78 (tmp, sizeof(barcode), DCS_8_TO_7);
// dcs_78 (tmp, sizeof(msg), DCS_8_TO_7);

//======================
//
// send the SMS message
//
//======================

send_sms (tmp, sizeof(barcode), fone, MSISDN_ADN, PARAMX, 0, NULL, NULL);
// send_sms (tmp, sizeof (msg), fone, MSISDN_ADN, PARAMX, 0, NULL, NULL);
display_text_raw("SMS sent", Q_DISPLAY_TEXT_USER_CLEAR);

//==========
//
// clean up
//
//==========

free (tmp);
free (fone);
}

void turbo_handler (u8 action, void *data)
{
switch (action)
{
case ACTION_APP_INIT:
init_uart2i2c (MY_SLAVE_ADDRESS);

//===================================
//
// set baud rate to 9600
//
//===================================

uart2i2c_write_ubrr(51);

break;

case ACTION_INSERT_MENU:
insert_menu ("SCAN BARCODE");
insert_menu ("ADD PHONENUM");
break;

case ACTION_MENU_SELECTION:
stk_thread (action_menu, data);

// reg_action(ACTION_STATUS);

break;

case ACTION_STATUS:

display_text_raw("ACTION_STATUS", Q_DISPLAY_TEXT_USER_CLEAR);
break;

default:
break;
}
}


Thanks again for your help,
Richard
Back to top
pz
Guest





PostPosted: Mon Aug 21, 2006 8:35 am    Post subject: Reply with quote

rhamel wrote:
Hi Pavel,
I'm not sure I understand what you mean by ACTION_STATUS and poll interval. I see ACTION_STATUS in some of the sample code, but I don't understand how it is invoked. Nor am I sure how to set the poll interval.


Do reg_action(ACTION_STATUS) in ACTION_APP_INIT. Then ACTION_STATUS is invoked in every STATUS APDU send by phone. This is usually every 30 sec but can be modified with POLL_INTERVAL STK command, the lowset value is (usually) 5 sec.

Please look at my_poll_interval() and ACTION_STATUS in autoalarm.c.

I would suggest to have your code in ACTION_STATUS with semaphore:
Code:


my_status_fnc()
{
my_status=1;
check reader
send sms
my_status=0;
}

ACTION_INIT:
my_status=0;

ACTION_STATUS:
if(my_status==0)stk_thread(my_status_fnc, NULL);


So that it's not spawned more than once.

Quote:

You asked about the protocol the barcode reader uses. I'm not sure about that either. I can tell you that when I use 'minicom' to connect to the serial port that the barcode reader is on, and I swipe a barcode through it, minicom displays 'A12345'. The '12345' is what the barcode is supposed to return. I have no idea why it generates an 'A'.

The pitiful documentation I have on the barcode reader says :-

RS232, full-duplex, 8N1, 9600

then, it says

1. The start character is factory defined as an "STX" (02h).
2. The CR/LF character serve to bring the received screen text back to the left.
3. The "ETX" (03h) character denotes the end of current transmission.

There's a diagram which I guess, represent a packet of some sort.

STX (02h)
CARD ID (which I guess is the barcode itself)
CR (0Dh)
LF (0Ah)
ETX (03h)


Are they needed? If you conenct reader to minicom do you have to use them or you just get the string you mention above?

Anyway it seems that no modification to baby will be needed.

Maybe you can try your code with gps instead of reader firsy to see if all other works.

Quote:

Anyhow, I am attaching my code here. Can you take a look and tell me where I am grossly mistaken, please?


Avoid
Code:

while(barcode[0] == 0)
        uart2i2c_read_rxfifo_data (barcode, 6);


It blocks the phone for ever (until there is char).

Do just
Code:


u16 nr_chars;

uart2i2c_read_rxchars(&nr_chars);
if(nr_chars!=0)
{
uart2i2c_read_rxfifo_data(buf, nr_chars);
parse
send sms
}

in my_status_fnc()

Quote:

Code:
/*
 * Copyright (C) 2003 BLADOX, s.r.o.  All rights reserved.
 *
 * This file is part of an example program for Turbo. This example
 * program may be used, distributed and modified without limitation.
 *
 */

#include <config.h>
#include <turbo/turbo.h>

// not sure if these are needed
#include <avr/sfr_defs.h>
#include <avr/io.h>

#include <stdlib.h>
#include <string.h>

#include "uart2i2c.h"

u8 PROGMEM msg[] = "BARCODE GOES HERE"; // SMS Msg
u8 PROGMEM phonenum[] = "1234567" ;     //  phone number

//===============================================
//
// secret parameter X that makes send_sms() work
//
//===============================================

#define PARAMX 0x12
#define MY_SLAVE_ADDRESS 0x7E

void action_menu (Menu_selection_data *data)
{
   if (data->item == 0) // Scan barcode here
   {
        display_text_raw("SCAN BARCODE here", Q_DISPLAY_TEXT_USER_CLEAR);
      action_sms ();
   }
   else // add phone number here
   {
      //========================================================
      //
      // placeholder for adding phone number to internal storage
      //
      //========================================================
        display_text_raw("add phone here", Q_DISPLAY_TEXT_USER_CLEAR);
   }
}

void action_sms (void *data)
{
//=============================
//
// allocate space for SMS text
//
//=============================
  u8 *tmp = malloc (100);
  u8 i;
  u8 barcode[6];

//===========================================================
//
// convert string to phone number that cellphone understands
//
//===========================================================

  u8 *fone  = str2msisdn (phonenum, MSISDN_ADN, MEM_R);

//===============================================
//
// display_text_raw
//    clears the screen of the phone
//    positions the cursor at the first line
//    displays the text
//    waits for user to press 'ok'
//
//===============================================
  display_text_raw("hit here", Q_DISPLAY_TEXT_USER_CLEAR);
  barcode[0] = 0;
//=============================================================
//
// supposed to be scanning here
// do I have to send something to the scanner first? RTS? CTS?
//
// the presence of this while loop here causes the phone
// to display "WAIT" and a progress bar for about 10 seconds
// then it clears the screen and displays OK/BACK
// pressing OK does nothing
// pressing BACK returns to the menu level before I entered
// the app
// so it looks like the app crashes completely
// there must be another mechanism to do a loop
//  IDLE TASK??
//
//=============================================================
   while(barcode[0] == 0)
        uart2i2c_read_rxfifo_data (barcode, 6);
   barcode[6] = '\0';

// app crashes at this point

  display_text_raw("after scan", Q_DISPLAY_TEXT_USER_CLEAR);
  display_text_raw(barcode, Q_DISPLAY_TEXT_USER_CLEAR);

//=========================================
//
// copy the SMS message to the text buffer
//
//=========================================

  memcpy (tmp, barcode, sizeof(barcode));
  // memcpy (tmp, msg, sizeof(barcode));
  // memcpy (tmp, msg, sizeof (msg));

//===========================
//
// change message encoding??
//
//===========================

  dcs_78 (tmp, sizeof(barcode), DCS_8_TO_7);
  // dcs_78 (tmp, sizeof(msg), DCS_8_TO_7);

//======================
//
// send the SMS message
//
//======================

  send_sms (tmp, sizeof(barcode), fone, MSISDN_ADN, PARAMX, 0, NULL, NULL);
  // send_sms (tmp, sizeof (msg), fone, MSISDN_ADN, PARAMX, 0, NULL, NULL);
  display_text_raw("SMS sent", Q_DISPLAY_TEXT_USER_CLEAR);

//==========
//
// clean up
//
//==========

  free (tmp);
  free (fone);
}

void turbo_handler (u8 action, void *data)
{
  switch (action)
  {
    case ACTION_APP_INIT:
      init_uart2i2c (MY_SLAVE_ADDRESS);

      //===================================
      //
      // set baud rate to 9600
      //
      //===================================

      uart2i2c_write_ubrr(51);

      break;

    case ACTION_INSERT_MENU:
      insert_menu ("SCAN BARCODE");
      insert_menu ("ADD PHONENUM");
      break;

    case ACTION_MENU_SELECTION:
      stk_thread (action_menu, data);

      // reg_action(ACTION_STATUS);

      break;

    case ACTION_STATUS:

      display_text_raw("ACTION_STATUS", Q_DISPLAY_TEXT_USER_CLEAR);
      break;

    default:
      break;
  }
}


Thanks again for your help,
Richard


display_text_raw() in ACTION_STATUS needs stk_thread()
Back to top
Display posts from previous:   
Post new topic   Reply to topic    bladox.com Forum Index -> Development All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group