在C中帮助串行COMM [英] Help with Serial COMM in C

查看:104
本文介绍了在C中帮助串行COMM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,我是编程的初学者,我有一个arduino板,编码接收串行数据,如果收到1,将触发一个动作,但如果它收到0,则另一个动作,漂亮简单。随着arduino程序附带的串行监视器,它可以工作,但我想做一个C程序。



我的问题是,看了很多相关材料,我不能把它付诸实践。我无法使用Dev C ++ 4.9.9.2编译任何示例程序。缺少库和函数总是存在问题。



所以我的问题是,我需要使用什么编译器才能使它工作?如果Dev C ++不是问题,是否有人粘贴了可以执行此操作的代码,将0或1发送到串口?





我使用的是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.

So my question is, what compiler do i need to make it work? If Dev C++ is not the problem, could someone paste a code that could do it, send 0 or 1 to the serial port?


Im using Windows 7 64bits... is this a problem too?

推荐答案

为什么你不用 Visual C ++ (您甚至可以使用 C# Express Edition

您可以使用以下代码示例正确配置COM设备:

配置通信资源(Windows) [ ^ ]和简单的问题

Why don''t you use Visual C++ (you may even use C#) Express Edition?
You may use the following code sample for configuring correctly the COM device:
"Configuring a Communications Resource (Windows)"[^] and the simply issue
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... :-)


最后找到它..它编译并使用dev c ++ / Windows



Found it at last.. it compiles and works with dev c++ / Windows

#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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆