BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Wed Jul 18, 2018 2:57 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Serial communication between BAP28 and PC
PostPosted: Sun Nov 11, 2012 10:01 pm 
Offline
Citizen

Joined: Sun Nov 04, 2012 12:05 am
Posts: 7
Hey guys, I'm trying to read the 'serout' output from a BAP28 into a C program I've written. But my problem is nothing is being read by the program, and it just hangs after the 'read' function. I was wondering if you guys had any information of the serial port settings needed to correctly read the information from the BAP28? Or any Ideas about where my program might be going wrong if you've done something similar.

Currently I have this code running on the BAP28:

Code:
string var byte(20)
string = "Hello World",0
index var word

Main
Index = 10
Do
   index = index - 1
   serout s_out,i9600,[str string\20\0,13]
   pause 75
While index
End


It outputs "Hello World" ten times and then stops. I know it works because I can read the output from the chip in the Basic Micro Studio terminal. However what I want to do is to read this information in a C Program so I used a couple of guides and managed to hack this code together, with what I think the serial port settings should be (but are probably wrong):

Code:
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

#define BAUDRATE B9600
#define BOTBOARDII "/dev/ttyUSB0"

int fd;

int openPort(void)
{
   int fd;
   fd = open(BOTBOARDII, O_RDWR | O_NOCTTY);
   if (fd == -1)
   {
      perror("Unable to open /dev/ttyUSB0");
   }
   printf("Port Opened.\n");
   return (fd);
}


int configurePort(int fd)
{
   struct termios config;

   //Set input and output speeds to 9600 bauds.
        
   cfsetispeed(&config, BAUDRATE);   
   cfsetospeed(&config, BAUDRATE);

   //Settings for 8 bits no parity.
   
   config.c_cflag &= ~PARENB;   
   config.c_cflag &= ~CSTOPB;
   config.c_cflag &= ~CSIZE;
   config.c_cflag |= CS8;

   //No hardware flow control, Raw Input, No software flow control and Raw output.

   config.c_cflag &= ~CRTSCTS;
   config.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
   config.c_iflag &= ~(IXON | IXOFF | IXANY);
   config.c_oflag &= ~OPOST;

   //Only one character needs to be read, no time between characters.

   config.c_cc[VMIN]  = 1;
   config.c_cc[VTIME] = 0;

   //Control characters, end of line, and quit.

   config.c_cc[VEOL] = 0;
   config.c_cc[VQUIT]    = 0; 
   
   if(tcsetattr(fd, TCSAFLUSH, &config) < 0)
   {
      printf( "Configuration Not Applied!\n");
   }
   return 1;
}

int main(void)
{

   int count;
   int fd = openPort();
   
   int m = configurePort(fd);
      
   unsigned char buffer[1024];

   //This loop was to clear the array.

   for (count = 0; count < sizeof(buffer); count++)
   {
      buffer[count] = 0;
   }
   
   int n = read(fd, buffer, sizeof(buffer));

   if (n < 0)
   {
      fputs("read failed!\n", stderr);
   }

   for (count = 0; count < sizeof(buffer); count++)
   {
      printf("%d",buffer[count]);
   }
      
   return (fd);
}



Top
 Profile  
 
 Post subject: Re: Serial communication between BAP28 and PC
PostPosted: Mon Nov 12, 2012 3:04 pm 
Offline
Citizen

Joined: Sun Nov 04, 2012 12:05 am
Posts: 7
Okay, through some further research, I've found that if I add the code:

Code:
config.c_cflag |= (CLOCAL | CREAD);


Into the serial port configuration, it will work and read data.

However, the program will still hang during the read part of the program, it only continues for some reason when I have pressed connect on the terminal in Basic Micro Studio. So my question is now: What is happening when I press connect on the terminal, is it sending a signal to the BAP28, so that it sends back the 'serout' data in return, or is my serial port configuration still incomplete?


Top
 Profile  
 
 Post subject: Re: Serial communication between BAP28 and PC
PostPosted: Mon Nov 12, 2012 3:44 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
Hi,

My quick look through your BAP28 code, has it only outputting the string to the serial port 10 maybe 11 times and then hanging. When you do a connect from the PC, it often will change the DTR line which resets the processor so it restarts...

Kurt


Top
 Profile  
 
 Post subject: Re: Serial communication between BAP28 and PC
PostPosted: Mon Nov 12, 2012 7:54 pm 
Offline
Citizen

Joined: Sun Nov 04, 2012 12:05 am
Posts: 7
Yes I know it repeats only 10 times, I originally had the code repeating constantly however I thought it might be interfering with the C program reading the data because the data kept coming. But that didn't do the trick so it has just been left in the code.

You say when you connect to the processor it changes the DTR line and resets the processor, do you know what else it does when it connects? Does use any flow control? So that it knows when the device and the other end of the serial connection is read to receive the 'serout' data?


Top
 Profile  
 
 Post subject: Re: Serial communication between BAP28 and PC
PostPosted: Mon Nov 12, 2012 9:42 pm 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
When you are using serout it is not using any hardware support at all, it is simply doing it's own timings to output the data to the IO pin... If there is nothing waiting for it at the other side it usually goes off to nowhere...

Kurt


Top
 Profile  
 
 Post subject: Re: Serial communication between BAP28 and PC
PostPosted: Tue Nov 13, 2012 12:40 am 
Offline
Citizen

Joined: Sun Nov 04, 2012 12:05 am
Posts: 7
I know it would be better to use hserout. However, I'm limited by what I have. I just thought that because the basic micro studio terminal is able to receive the data output by serout, then I should able to write a C program that would emulate the terminal and receive data in the same way.


Top
 Profile  
 
 Post subject: Re: Serial communication between BAP28 and PC
PostPosted: Wed Nov 14, 2012 12:59 am 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
You should have no problem having a PC receive data from software serial. I think what Kurt is saying is that if you need to send data to the module then you have to be sure the program is in a serin command when you send the data or it may get lost.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Serial communication between BAP28 and PC
PostPosted: Wed Nov 14, 2012 2:25 am 
Offline
Citizen

Joined: Sun Nov 04, 2012 12:05 am
Posts: 7
Thanks for the help guys, I managed to get my read program to work. Like KurtEck said it was to do the the DTR line, I had to set it in my program before the BAP28 would start to transmit the serout data. Cheers.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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 post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO