Go to Post [on practice robots:] Twice the work and twice the practice building seem to equal twice the experience so far. - MrBasse [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-01-2008, 00:07
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Does reading from the serial port work?

With a fresh crop of beginning programmers on our team this year, we decided EasyC was what we'd use. Alas, a lot of my accumulated tools and tricks didn't quite make the transition, and I'm trying to reimplement a few of them.

Specifically, I want to implement something like the menus in Kevin Watson's "Bells & Whistles" camera code last year. I've found references to ReadSerialPortOne() and tried to use it, but it doesn't seem to work. Here's the code I put in a fresh OperatorControl function:
Code:
unsigned char keypress;
keypress = ReadSerialPortOne();
if (keypress != 0)
{
   WriteSerialPortOne(keypress);
}
I expect that to echo anything I type at the terminal. What I get is nothing. I've tried HyperTerminal instead of the EasyC Pro Terminal window, with no change. I know the terminal itself is working, as I can see the IFI> prompt show up when I reset the Robot Controller. I have the RC and OI tethered. While trying to figure out what wasn't working, I added a timer and called WriteSerialPortOne(65) every 100 milliseconds, and saw an "A" show up ten times a second, so I know the code is running.

What am I doing wrong?
  #2   Spotlight this post!  
Unread 23-01-2008, 00:27
BradAMiller BradAMiller is offline
Registered User
AKA: Brad
#0190 ( Gompei and the Herd)
Team Role: Mentor
 
Join Date: Mar 2004
Location: Worcester, MA
Posts: 592
BradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant futureBradAMiller has a brilliant future
Re: Does reading from the serial port work?

Alan -

I'm at home and don't have access to a robot controller here. But, one question... were you using the IFI loader terminal window or the easyC terminal window to try to talk to the serial ports? I'm wondering if the easyC version will listen for keyboard input.

You might try the other one. In any case I can give it a try sometime tomorrow to see what's up. I know that the code works, at least for port 2 because all the camera routines use reading and writing to the serial ports.
__________________
Brad Miller
Robotics Resource Center
Worcester Polytechnic Institute
  #3   Spotlight this post!  
Unread 23-01-2008, 09:07
lynca's Avatar
lynca lynca is offline
Andrew Lynch
FRC #2587 (DiscoBots)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Houston, TX
Posts: 1,612
lynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond repute
Send a message via AIM to lynca
Re: Does reading from the serial port work?

Have you tried a while loop instead of and if just too make sure it is not a timing problem.

I've done a custom echo terminal and usually I start with a while() loop that constantly reads the serial port and then writes back when the input is none null. If the while loop works, you might want to consider putting timestamps to see if there is a latency wait before checking the ReadSerialPort
__________________
History: 624 - Cryptonite --> 118 - Robonauts --> 2158 - AusTIN CANS --> 2587 DiscoBots
Bio: Andrew Lynch "How I Work" ---- LinkedIn , Facebook, Twitter
  #4   Spotlight this post!  
Unread 23-01-2008, 09:25
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Does reading from the serial port work?

Quote:
Originally Posted by lynca View Post
Have you tried a while loop instead of and if just too make sure it is not a timing problem.
Can you rephrase that? I can't tell what you mean.

As I understand it, the OperatorControl() function is already inside a while loop.

Quote:
I've done a custom echo terminal and usually I start with a while() loop that constantly reads the serial port and then writes back when the input is none null.
I thought that's exactly what I did (assuming you mean "not zero" where you wrote "none null").

Quote:
If the while loop works, you might want to consider putting timestamps to see if there is a latency wait before checking the ReadSerialPort
Could you elaborate on that?
  #5   Spotlight this post!  
Unread 24-01-2008, 01:46
lynca's Avatar
lynca lynca is offline
Andrew Lynch
FRC #2587 (DiscoBots)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2001
Location: Houston, TX
Posts: 1,612
lynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond reputelynca has a reputation beyond repute
Send a message via AIM to lynca
Re: Does reading from the serial port work?

Sorry about the rather vague post,

I understand now that you are running in the OperatorControl while loop,
that was my original question.
__________________
History: 624 - Cryptonite --> 118 - Robonauts --> 2158 - AusTIN CANS --> 2587 DiscoBots
Bio: Andrew Lynch "How I Work" ---- LinkedIn , Facebook, Twitter
  #6   Spotlight this post!  
Unread 25-01-2008, 09:43
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Does reading from the serial port work?

I'm still not getting what I expect from ReadSerialPortOne(). It always returns zero, no matter how I try to send characters from the computer to the program port. Can someone point me at some working code, so I can rule out issues with hardware or terminal emulation software on the PC?
  #7   Spotlight this post!  
Unread 25-01-2008, 10:39
Kingofl337's Avatar
Kingofl337 Kingofl337 is offline
You didn't see anything....
AKA: Adam
FRC #0501 (Power Knights)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 1998
Location: Manchester, NH
Posts: 861
Kingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond repute
Send a message via Yahoo to Kingofl337
Re: Does reading from the serial port work?

easyC's terminal window doesn't accept data. I'll make a program and check it out I had it working a few months ago.
__________________
FIRST Team 501 PowerKnights - Mentor
FIRST Team 40 Checkmate - Mentor Alum
FIRST Team 146 Blue Lightning - Alumni
  #8   Spotlight this post!  
Unread 25-01-2008, 11:22
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Does reading from the serial port work?

Quote:
Originally Posted by Kingofl337 View Post
easyC's terminal window doesn't accept data.
What does it do? I would expect the purpose of a "terminal window" would be to act like a terminal, i.e. to send typed data and display received data. But that's not important right now, since I've been using HyperTerminal and other programs to send characters out the serial port with no effect on the ReadSerialPortOne() result.

I look forward to seeing EasyC serial port code that's supposed to work, so I can figure out what I'm doing wrong. (I just hope that what I'm doing wrong isn't something silly like expecting things to act in a way other than how they are designed.)
  #9   Spotlight this post!  
Unread 25-01-2008, 11:58
Kingofl337's Avatar
Kingofl337 Kingofl337 is offline
You didn't see anything....
AKA: Adam
FRC #0501 (Power Knights)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 1998
Location: Manchester, NH
Posts: 861
Kingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond repute
Send a message via Yahoo to Kingofl337
Re: Does reading from the serial port work?

Here you go this is the basics. If you want it to echo the actual character used use WriteSerialPortOne(unsigned char). Other wise you get the ascii byte code. If you compare byte == "X" for instance it will work.

EDIT: DON'T USE THIS, IT DOESN'T WORK, I WAS A DOLT! SEE BELOW

Code:
void main ( void )
{
      unsigned char byteswaiting; 
      unsigned char byte; 
      unsigned char x; 

      while ( 1 )
      {
            byteswaiting =  GetSerialPort1ByteCount( );
            if ( byteswaiting != 0 )
            {
                  PrintToScreen ( "Output: " ) ;
                  for ( x = 0 ; byteswaiting == x ; x++ )
                  {
                        byte = ReadSerialPortOne( );
                        if ( byte == 13 )
                        {
                              PrintToScreen ( "\n" ) ;
                        }
                        else
                        {
                              PrintToScreen ( "%d" , byte ) ;
                        }
                  }
            }
      }
}
__________________
FIRST Team 501 PowerKnights - Mentor
FIRST Team 40 Checkmate - Mentor Alum
FIRST Team 146 Blue Lightning - Alumni

Last edited by Kingofl337 : 28-01-2008 at 16:09.
  #10   Spotlight this post!  
Unread 25-01-2008, 12:56
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Does reading from the serial port work?

Whoa, this is a lot weirder than I expected.

Is there some "magic" involved in this line?
Code:
for ( x = byteswaiting ; byteswaiting == x ; x++ )
Its purpose is not at all obvious. Regardless of the value of byteswaiting, the code in braces following the line will be executed exactly once. The variable x appears to have no function inside the braces. I'll do it that way, but I have to assume there's something happening behind the scenes that isn't being explained.

I was always told that the %d format specifier expects an int variable, and that you had to cast chars with (int) in order to avoid problems. Is this not the case with EasyC's PrintToScreen() function?

I'm also confused by the fact that you've named the function main(). I thought that name was already taken. Is this another "magic" feature of EasyC that does something undocumented, or am I completely misunderstanding what's going on here?
  #11   Spotlight this post!  
Unread 25-01-2008, 14:25
Kingofl337's Avatar
Kingofl337 Kingofl337 is offline
You didn't see anything....
AKA: Adam
FRC #0501 (Power Knights)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 1998
Location: Manchester, NH
Posts: 861
Kingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond repute
Send a message via Yahoo to Kingofl337
Re: Does reading from the serial port work?

Code:
for ( x = 0 ; byteswaiting == x ; x++ )
This for line contains no magic I just threw this together in a rush. I'm not going to lie to you Alan, while I'm an ok programmer most of what I know has been through trial and error, research PID for instance and web programming html/php/cgi. I just bought a Microchip PIC kit to learn more about the lower lever programming.

In PrintToScreen %d does expect a integer, I've never had a issue using %d with int or char without casting data types. I have have had problems with mixing data types for instance %ld with a int or %d with long.

My function is in main() because that was the only function of the whole program. I wrote it in VEX without a competition template in FRC it's called a Standalone Project in the File menu.

I had a hard time making it work and Brad sent me a program like a year
ago to me and this is how it worked for the most part.

1.) Call GetSerialPort1ByteCount( ) to see if there are any bytes waiting in the buffer.
Code:
 byteswaiting =  GetSerialPort1ByteCount( );
            if ( byteswaiting != 0 )
2.) Create a for loop to empty out the buffer and load it into a array, struct,
or single variable
Code:
for ( x = 0 ; byteswaiting == x ; x++ )
                  {
                        byte = ReadSerialPortOne( );

3.) Do as you will with the data you just received
In this case check for return or print the ASCII code for the keypressed
__________________
FIRST Team 501 PowerKnights - Mentor
FIRST Team 40 Checkmate - Mentor Alum
FIRST Team 146 Blue Lightning - Alumni

Last edited by Kingofl337 : 25-01-2008 at 14:29.
  #12   Spotlight this post!  
Unread 25-01-2008, 15:10
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Does reading from the serial port work?

Quote:
Originally Posted by Kingofl337 View Post
Code:
for ( x = 0 ; byteswaiting == x ; x++ )
This for line contains no magic I just threw this together in a rush.
Did you test the program you posted? Does it do what you think it's supposed to? If there's nothing unobvious about this line of code, then it looks to me like its only effect is to waste time. If you intended for this example to help me, I must disappoint you. It raises more questions than it answers.

I won't apologize for sounding frustrated here, because I am frustrated.

Quote:
My function is in main() because that was the only function of the whole program. I wrote it in VEX without a competition template in FRC it's called a Standalone Project in the File menu.
I'm afraid that's not going to be very useful for me. I'm not trying to communicate with a VEX system, and my goal is to use serial port I/O in a competition program.



I don't yet fully regret making the decision to use EasyC this year, but I'm getting close. As I keep finding out, simple stuff is simple, but anything that doesn't fit the predefined blocks seems several times more difficult than straight C coding. If I keep running into these roadblocks I'm going to have to tell our software group to throw away everything we've done so far and go back to the MPLAB environment before the month is out.
  #13   Spotlight this post!  
Unread 25-01-2008, 15:31
Kingofl337's Avatar
Kingofl337 Kingofl337 is offline
You didn't see anything....
AKA: Adam
FRC #0501 (Power Knights)
Team Role: Mentor
 
Join Date: Feb 2005
Rookie Year: 1998
Location: Manchester, NH
Posts: 861
Kingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond reputeKingofl337 has a reputation beyond repute
Send a message via Yahoo to Kingofl337
Re: Does reading from the serial port work?

I did test it and it did work with no issues. The serial port works the same in FRC/VEX and Competition vs Non-Competition.
__________________
FIRST Team 501 PowerKnights - Mentor
FIRST Team 40 Checkmate - Mentor Alum
FIRST Team 146 Blue Lightning - Alumni
  #14   Spotlight this post!  
Unread 25-01-2008, 15:51
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Does reading from the serial port work?

FOUL!

You've edited the code you posted originally, without telling anyone you did it. Now I think I see the purpose of the for loop. Before you changed it, it would obviously not have worked as intended. It still looks wrong, but in a completely different way. Is it perhaps supposed to be this?
Code:
for ( x = 0 ; x < byteswaiting; x++ )
I don't believe you did it on purpose, but I feel like I've been tricked into quoting the modified line and commenting on it as if it were still the original.



So if I want to test this in an FRC competition project, I can just put it in the OperatorControl() function, right? And if I do that, it doesn't need the outer while(), right? I'll be trying this out first thing tomorrow when the team meets again.

Last edited by Alan Anderson : 25-01-2008 at 15:54.
  #15   Spotlight this post!  
Unread 26-01-2008, 20:30
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Does reading from the serial port work?

I tried the example code provided, and never saw GetSerialPort1ByteCount( ) return a nonzero value. I ran equivalent code that I wrote and compiled using MPLab, and it worked exactly as I expected.

I'm done with EasyC. I can't do what I want using it.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
No Serial Port G1279 Programming 5 10-01-2008 15:15
Programming the vex microcontroller to respond to commands on the serial port crashoverride02 Programming 1 20-10-2006 14:17
Scripting Setup and the Camera + Serial Port Drivers CJO Programming 22 11-01-2006 17:42
Programming Vex Starter Kit To Recieve From Serial Port firetrap FIRST Tech Challenge 0 26-09-2005 17:27
TTL port to a serial port on a demo board ImmortalAres Programming 16 09-07-2005 23:44


All times are GMT -5. The time now is 16:12.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi