Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Serial Port Communication White Paper (http://www.chiefdelphi.com/forums/showthread.php?t=4574)

Ian W. 07-06-2002 17:43

grr, i tried my mess of a program out today, and it didn't work. there seems to have been a problem with writing to a file. i' not sure why, but that seems to be where the error was from. there was no syntax errors, so there was something wrong somewhere else. could i actually need two threads? in that case, i think i know what to do, i just need someone to help me on what method i need to take.

rbayer 07-06-2002 18:31

I'd be happy to help. Just send your source code my way, and I'll help with any parts you need. ICQ: 152894206 (file transfers probably won't work, but just attach them to a message here, and I'll take a look.)

Greg Ross 07-06-2002 19:37

How to monitor the keyboard without blocking on every cycle? Try in_avail()
 
Quote:

Originally posted by Ian W.
ok thanks. only one more problem then. how can i moniter the keyboard without stopping every loop cycle? i know about cin and fin (fstream). that's all. both stop until data is put in and the 'enter' key is hit. that wouldn't do. there must be some way around this, anyone know how? cause if that works, then the program will actually work better than i expected :D
I haven't tried this with C++, but in C you use kbhit(). It's possible this might work in C++ too, but it's risky.

Check out in_avail. That may do what you want. I believe if you call cin.in_avail() it will return > 0 if a key has been struck. You can simply avoid doing the keyboard read if there's nothing to read.

VanWEric 07-06-2002 21:53

keybd_event
 
I wrote 2 apps that talked to eachother (just for fun). One app used kb_event() to put keyboard events out for the other to pick up with getch(). Basically, do keybd_event(VK_(a letter or function),0,0,0); Have the recieving proggie active, use getch and look at the char it returns. IE to send the letter A to the other progg have app 1 go keybd_event(VK_A,0,0,0). The other is waiting with a myrecievingchar = getch(); I wrote a header file to include the letters and numbers (( windows.h has all the kb_event crap, but only defines the keys that arent useful)) I can get that posted if you want it, but it is really just a bunch of #define VK_A 65, #define VK_B...
Oh, and getch is in conio.h

Hope that helped, but i am guessing it didnt.

Mayhaps the unique data sending approach was vaguely interesting though

Ian W. 07-06-2002 22:02

hmm, i tried the in_avail thing, and it doesn't work. heck, it's not even recognized by my compiler (VC++). as for the rest of my code, i don't use ICQ, so the number you gave me means nothing to me :D. if you want, i do have AIM (yeah, i hate AOL too).

Greg Ross 07-06-2002 22:31

Quote:

Originally posted by Ian W.
hmm, i tried the in_avail thing, and it doesn't work. heck, it's not even recognized by my compiler (VC++). as for the rest of my code, i don't use ICQ, so the number you gave me means nothing to me :D. if you want, i do have AIM (yeah, i hate AOL too).
As far as I can tell, in_avail is not non-standard or a new feature. Try calling cin.rdbuf( )->in_avail( ) instead of cin.in_avail( ). I assume you must already be #including <iostream>.

Ian W. 07-06-2002 22:38

hmm, that syntax works, but how would i run that in a while loop? like, what number does 'cin.rdbuf( )->in_avail( )' equal when no key is pressed? i'm not sure of that.

Ian W. 07-06-2002 23:14

ok, i've figured out the 'cin.rdbuf( )->in_avail( )' line seems to always equal 0, no matter what i do. how do i change this?

rbayer 07-06-2002 23:21

after you've done the cin.rdbuf( )->in_avail( ), you must actually read in a byte. a simple 'cin>>myChar' should do nicely. the problem is, in_avail() on checks to see if data is there, it doesn't clear it from the buffer. Therefore, that same data will be there the next time and you will always get 0 back. If you're already doing this, post the offending code and I'll take a look at it.

Ian W. 07-06-2002 23:33

is there anyway for the computer to skip the cin>>myChar if no data is entered? cause otherwise, the loop will stop once every cycle for me to hit enter. that's no good. could i use the 'keybd_event(i,0,0,0);' event to hit enter, if no data is entered? my whole problem hinges around the fact that i know no way of skipping a cin without putting in data. any help?

rbayer 08-06-2002 00:13

I think I've got the solution...
 
Going back to a previous post (by gwross), your best method will be to use kbdhit(). It will return 0 if no key has been pressed, and 1 if a key has been pressed. Since I assume you only want it to quit when a certain key is pressed, you can then issue a getch() which will return the key that was pressed. Just compare it, and you should be good to go. Just make sure to include <conio.h>.
Here's the code I tested it with:
while(true){
if(kbdhit()){
cout<<"key pressed:";
myChar=getch();
cout<<myChar<<"\n";
if(myChar=='q')
return 0;
}
else{
cout<<"no key pressed\n";
}
cout.sync_with_stdio()
}

BTW, that cout.sync_with_stdio() is because I was having problems with the output being very sluggish.

Dave Flowerday 08-06-2002 17:46

Quote:

It wasn't me! I got the picture in an email, and I think the billboard was somewhere in England
I wasn't trying to imply that it was you by any means...

Greg Ross 10-06-2002 01:19

Quote:

Originally posted by Ian W.
ok, i've figured out the 'cin.rdbuf( )->in_avail( )' line seems to always equal 0, no matter what i do. how do i change this?
cin.rdbuf( )->in_avail( ) should return non zero (greater than zero) if there is data in the keyboard buffer. Is this not the case? When you say "no matter what i do" does that include pressing keyboard keys (other than Ctrl-C which is probably handled differently than other keys)? (I know that may sound insulting. That's not my intention. I have worked on a help line, and I've learned to not take anything for granted.) If you are able to read data from the keyboard using cin, then in_avail() should also work.

BTW, for readability (probably not efficiency) you could save the return value of cin.rdbuf() in a variable (say cinBuffer) -- outside the loop and then only call cinBuffer->in_avail() inside the loop.

rbayer 10-06-2002 01:33

I've been playing around with this too, and I've run into the same problems. It seems that the only way to get data to cin so that in_avail() will notify you is to do a cin! For example, if you call cin<<x, but at the prompt type something like "5 10 15", then in_avail() will return the number of chars that are still in the buffer. In this case, either 6 or 7 (I can't remember which), because the first one has been eaten by your cin statement. As far as I can tell, this would be useful in figuring out how much data a user input, but only after the user actually input the data.

Or I could be completely wrong... Anyway, the code I posted above seems to work (based on your earlier suggestion). If anyone can get this working with cin, I'd be interested to hear how.

Ian W. 10-06-2002 17:51

well, i tried the _getch () and _getche () functions (from conio.h). just a simple loop to test it, and it worked pefectly, doing exactly what i wanted. from what i understand, it reads the I/O stream, and you don't even have to hit enter. it will just go on and on until there is input on the keyboard, i think. if that's the case, i just need to identify a certain key, and all should work as i want.


All times are GMT -5. The time now is 22:53.

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