Hello, I'm a beginner with english and C programing, I'm making a program to control a servo-sonnar, I need comunicate with the servo-sonnar using the serial port (Com 1), in linux it's easy, but in windows I haven't an idea about using Windows Api's and adding libraries to Dev-C++, can anybody help me send a example code's? Thank you very much.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2006-03-11
You can access serial ports through stdio, iostream, or Win32 file I/O. You simply open the 'file' on the device name ("COM1" in this instance).
If you need to control baud rate and frame format, then you can use the Win32 API, it is somewhat cumbersome, so the quick and dirty method is to invoke the MODE command via a system() call.
The code runs ok for send commands, because for read Com1 don't work right. I need the program do this:
Send Command to serial port --> Wait for ack from serial port --> Send second command
I wait the ack (character A) with:
while(ok!="A"){
ok=fgetc(portfp);
}
But don't work, any ideas?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
OP, your English is very good! I hope that if I pick up a second language I do as well as you.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2006-03-13
"A" is character string constant (of type const char*), wheras fgetc() returns an int; so you are comparing an int with a pointer. I would be surprised if the compiler did not at least issue a warning. You need to compare ok against the character constant 'A'.
while(ok!='A'){
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2006-03-14
Pedant ;) - I can play this game:
The code:
include <stdlib.h>
int main()
{
int ok = "A" ;
if( ok == "A" ){}
}
When compiled as C++ gives the error:
main.cpp:5: error: invalid conversion from const char*' toint'
main.cpp:6: error: ISO C++ forbids comparison between pointer and integer
And in C:
ctest.c:5: warning: initialization makes integer from pointer without a cast
ctest.c:6: warning: comparison between pointer and integer
I might suppose that when a char[] is an operand of == or = it degrades to a char* just as it does when you pass it to a function - i.e. it looses its 'sizeof' information. Arrays in C/C++ are fragile things, and will degrade thus at the drop of a that!
So that while "A" is of type const char[2], (indeed sizeof("A") == 2, where sizeof(const char) == 4), when the compiler gets to assign or compare it, it is const char. As evidenced by the failure of code such as:
char Astring[2] ;
Astring = "A" ;
wheras
char Astring[2] = "A" ;
is fine (initialization being a different matter than assignment).
But you of course knew all that already, and you are of course correct, but that was what I meant (which you also knew). But someone may be interested.
Still as demonstarted by the code above, the OP should have got at least warning. I hope he is not one of those people who ignore warnings with a cavalier "oh, that's only a warning"! But at the same time I hope he is not one of those who types code directly into the forum, rather than cutting & pasting the actual code. After all that I will be really annoyed if he had 'A' all along!
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
you may be surprised at how quickly an array can degrade. i know i was. when i first wrote this--in real code--i had expected it to work--and portably.
"typeof" as written is a gcc extension but i have support for it with templates. it would make the code to complicated to show such a simple example.
Hello, I'm a beginner with english and C programing, I'm making a program to control a servo-sonnar, I need comunicate with the servo-sonnar using the serial port (Com 1), in linux it's easy, but in windows I haven't an idea about using Windows Api's and adding libraries to Dev-C++, can anybody help me send a example code's? Thank you very much.
You can access serial ports through stdio, iostream, or Win32 file I/O. You simply open the 'file' on the device name ("COM1" in this instance).
If you need to control baud rate and frame format, then you can use the Win32 API, it is somewhat cumbersome, so the quick and dirty method is to invoke the MODE command via a system() call.
e.g.
FILE* portfp ;
system( "mode com1: baud=115200 parity=0 data=8 stop=1 to=off xon=off"
portfp = fopen( "com1", "wb+" ) ;
fprintf( portfp, "Hello, world!\n" ) ;
fclose( portfp ) ;
In most cases this is adequate, more complex serial I/O can be achieved through the Win32 API, but it is not very straightforward. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp. There are a number of third party libraries to simplify the process.
Clifford
Thank you very much Clifford!!! The code runs ok!!! :D
Note:
Put ); before the system line -->
system( "mode com1: baud=115200 parity=0 data=8 stop=1 to=off xon=off" );
...oops.
BTW, I meant to ask what is a servo-sonnar? Did you mean a servo-motor?
Clifford
No, jejeje, "Servo-Sonnar" is a ultrasonic distance sensor mounted on a servo-motor.
Ahh... servo-sonar
Clifford
The code runs ok for send commands, because for read Com1 don't work right. I need the program do this:
Send Command to serial port --> Wait for ack from serial port --> Send second command
I wait the ack (character A) with:
while(ok!="A"){
ok=fgetc(portfp);
}
But don't work, any ideas?
OP, your English is very good! I hope that if I pick up a second language I do as well as you.
"A" is character string constant (of type const char*), wheras fgetc() returns an int; so you are comparing an int with a pointer. I would be surprised if the compiler did not at least issue a warning. You need to compare ok against the character constant 'A'.
while(ok!='A'){
Clifford
""A" is character string constant (of type const char*)"
funny, i thought "A" was of type "const char[2]."
snork
include <iostream>
int main(){std::cout << typeid("A").name();}
O_o
my smile was left out. ^_~
snork
Pedant ;) - I can play this game:
The code:
include <stdlib.h>
int main()
{
int ok = "A" ;
if( ok == "A" ){}
}
When compiled as C++ gives the error:
main.cpp:5: error: invalid conversion from
const char*' to
int'main.cpp:6: error: ISO C++ forbids comparison between pointer and integer
And in C:
ctest.c:5: warning: initialization makes integer from pointer without a cast
ctest.c:6: warning: comparison between pointer and integer
I might suppose that when a char[] is an operand of == or = it degrades to a char* just as it does when you pass it to a function - i.e. it looses its 'sizeof' information. Arrays in C/C++ are fragile things, and will degrade thus at the drop of a that!
So that while "A" is of type const char[2], (indeed sizeof("A") == 2, where sizeof(const char) == 4), when the compiler gets to assign or compare it, it is const char. As evidenced by the failure of code such as:
char Astring[2] ;
Astring = "A" ;
wheras
char Astring[2] = "A" ;
is fine (initialization being a different matter than assignment).
But you of course knew all that already, and you are of course correct, but that was what I meant (which you also knew). But someone may be interested.
Still as demonstarted by the code above, the OP should have got at least warning. I hope he is not one of those people who ignore warnings with a cavalier "oh, that's only a warning"! But at the same time I hope he is not one of those who types code directly into the forum, rather than cutting & pasting the actual code. After all that I will be really annoyed if he had 'A' all along!
Clifford
you may be surprised at how quickly an array can degrade. i know i was. when i first wrote this--in real code--i had expected it to work--and portably.
"typeof" as written is a gcc extension but i have support for it with templates. it would make the code to complicated to show such a simple example.
to save you the trouble.
Array
Pointer
Pointer
Pointer
snork
include <iostream>
template <typename T> struct foo;
template <typename T> struct foo<T *>
{
operator const char * ()
{
return("Pointer");
};
};
template <typename T, unsigned int S> struct foo<T[S]>
{
operator const char * ()
{
return("Array");
};
};
template <typename T> const char * bar(T *)
{
return("Pointer");
}
template <typename T, unsigned int S> const char * bar(T[S])
{
return("Array");
}
int main()
{
char array[2];
char * pointer;
std::cout << foo<typeof(array)>() << '\n';
std::cout << foo<typeof(pointer)>() << '\n';
std::cout << bar(array) << '\n';
std::cout << bar(pointer) << '\n';
return(0);
}