问题描述
亲爱的所有人,我是编程的初学者,我有一个arduino板,编码接收串行数据,如果收到1,将触发一个动作,但如果它收到0,则另一个动作,漂亮简单。随着arduino程序附带的串行监视器,它可以工作,但我想做一个C程序。
我的问题是,看了很多相关材料,我不能把它付诸实践。我无法使用Dev C ++ 4.9.9.2编译任何示例程序。缺少库和函数总是存在问题。
我使用的是Windows 7 64位...这也是一个问题吗?
Dear all, im a begginer in programming and I have an arduino board, coded to receive data trhough serial, if it receives "1", an action will be triggered but if it receives a "0" its another action, pretty simple. With the serial monitor that comes with the arduino program it works, but i would like to do it trhough a C program.
My problem is, after reading a lot of related material, i cant put it to work. I cant compile any of the sample programs with Dev C++ 4.9.9.2. Theres always problems with missing libraries and functions.
Im using Windows 7 64bits... is this a problem too?
推荐答案
CHAR c='1'; // change to CHAR c=1; if appropriate
DWORD dwWritten;
if ( ! WriteFile(hCom, &c, 1, &dwWritten, NULL))
{
// handle error
}
同步写字节''1''
(或 1
)。
当然写''0''
(或 0
)非常相似......: - )
to syncrhonously write the byte ''1''
(or 1
).
Of course writing ''0''
(or 0
) is very similar... :-)
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#define BAUDRATE 9600
#define MODEMDEVICE "com3"
int main()
{
// OPEN AND CONF COM PORT
HANDLE hCom;
char c;
DWORD nbytes;
// VARS
BOOL bPortReady;
DCB dcb;
COMMTIMEOUTS CommTimeouts;
//Config
hCom = CreateFile(MODEMDEVICE,
GENERIC_READ | GENERIC_WRITE,
0, // Exclusive acces
NULL, // sem seguranca
OPEN_EXISTING,
0, // no overlapped I/O
NULL); // null template
bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes
bPortReady = GetCommState(hCom, &dcb);
dcb.BaudRate = BAUDRATE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
// dcb.Parity = EVENPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fAbortOnError = TRUE;
// set XON/XOFF
dcb.fOutX = FALSE; // XON/XOFF off for transmit
dcb.fInX = FALSE; // XON/XOFF off for receive
// set RTSCTS
dcb.fOutxCtsFlow = FALSE; // turn off CTS flow control
dcb.fRtsControl = RTS_CONTROL_DISABLE; //
// set DSRDTR
dcb.fOutxDsrFlow = FALSE; // turn off DSR flow control
dcb.fDtrControl = DTR_CONTROL_DISABLE; //
bPortReady = SetCommState(hCom, &dcb);
// Communication timeouts are optional
bPortReady = GetCommTimeouts (hCom, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
bPortReady = SetCommTimeouts (hCom, &CommTimeouts);
//Sending data
WriteFile(hCom, "0", 1, &nbytes,NULL);
WriteFile(hCom, "1", 1, &nbytes,NULL);
system("pause");
}
这篇关于在C中帮助串行COMM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!