|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||||
|
|||||
|
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);
}
What am I doing wrong? |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
||||
|
||||
|
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 |
|
#4
|
|||||
|
|||||
|
Re: Does reading from the serial port work?
Quote:
As I understand it, the OperatorControl() function is already inside a while loop. Quote:
Quote:
|
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||||
|
|||||
|
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
|
||||
|
||||
|
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.
|
|
#8
|
|||||
|
|||||
|
Re: Does reading from the serial port work?
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
|
||||
|
||||
|
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 ) ;
}
}
}
}
}
Last edited by Kingofl337 : 28-01-2008 at 16:09. |
|
#10
|
|||||
|
|||||
|
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++ ) 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
|
||||
|
||||
|
Re: Does reading from the serial port work?
Code:
for ( x = 0 ; byteswaiting == x ; x++ ) 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 )
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 Last edited by Kingofl337 : 25-01-2008 at 14:29. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |