Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29281 Discussions

Serial Port Communication and Terminate Program

sumitm
Beginner
650 Views
Hi,
I have two different problems. I am trying to get data from a measuring device connected to a com port. It came with an original software which runs at the command prompt and spits out the data. I am trying to replace that with a CVF6.6 program that other than collecting the data also does other things with it.
So far I have been able to successfully capture the data with CVF except that when I first start up my laptop. I have to start that consle program and quit it by pressing Control C. After that my application runs fine.
So I believe my program does not do some initialization of the com 1 port that the console app does. Can anyone guess what that could be.
My code uses
SPORT_SET_STATE(1,19200,0,8,1)
SPORT_CONNECT(1,0)
SPORT_READ_LINE
and once done reading the data it does
SPORT_CANCEL_IO(1)
and SPORT_RELEASE(1)
If anyone can point out what else is required for the proper initialization, that would be very helpful.
2. I thought of a way out by launching the console application prior to my data collection and terminating it. So I have used ShellExecute and the console application can be launched. To kill it I need to press Control C and I want to do that keystroke programatically.
So I invoked
iret=SendMessage(NULL,WM_IME_KEYDOWN,VK_CONTROL,0)
and
iret=SendMessage(NULL,WM_IME_KEYDOWN_VK_C,0)
The compiler gives me an error saying VK_C does not have a type and must have an explicit type. It does not give any error for VK_CONTROL or VK_RETURN.
Is there something I am doing wrong and also what is the procedure to emulate a Cntrl+C keystroke.
Also is there another way of killing a process automatically that has been started with Shell Execute.
Thanks for any help.
Sumit
0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
650 Views
I'm not familiar with serial port communications and SPORT routines. However, there's also SPORT_SET_TIMEOUTS which seems relevant.
I assume you have1) ran the console application and 2) called SPORT_GET* from a test app to retrieve the settings, then 3) copied these into your SPORT_SET* settings, right?
Having the settings appropriately set via SPORT_SET is of course better than cludge with Ctrl+C. However:
1) You cannot SendMessage to a console application, especially not with first parameter null 2) even if it were a GUI app, it should be WM_KEYDOWN, not WM_IME_KEYDOWN
You need keybd_event or SendInput API instead, which really simulates keyboard strokes:
interface
   subroutine keybd_event(i1,i2,i3,i4)
   !dec$attributes stdcall, decorate, alias: "keybd_event":: keybd_event
   integer::   i1, i2, i3, i4
   end subroutine
end interface

call keybd_event(VK_CONTROL, 0, 0, 0)
call keybd_event(IACHAR("c"), 0, 0, 0)
call keybd_event(IACHAR("c"), 0, KEYEVENTF_KEYUP, 0)
call keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0)
IACHAR is obviously the missing chain -- VK_virtual-keycodes for alphabetic characters are equal to ASCII codes.
Jugoslav
0 Kudos
sumitm
Beginner
650 Views

Thanks Jugoslav,

Your comment about get_sport_state got me thinking and I found that my iresult value on set_sport_state was non zero indicating an error. Then I realised that the set_sport_state should be invoked after SPORT_CONNECT and not before.

Now everything works fine.

Thanks for leading the thought process.

0 Kudos
Reply