我们编写了一个程序,将简单数据从 LPT1 发送到 RS232C。基本上,LPT 到串行转换电缆用于将两台机器连接在一起。程序将数据写入并行端口没有问题,但串行端没有收到任何内容。
我想知道这种交流是否可以在基本层面上进行,或者我的程序编写不正确。
DataSender(写入 LPT1)
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE m_hCommPort = ::CreateFile( "LPT1",
GENERIC_READ|GENERIC_WRITE, // access ( read and write)
0, // (share) 0:cannot share the
// COM port
0, // security (None)
OPEN_EXISTING, // creation : open_existing
FILE_FLAG_OVERLAPPED, // we want overlapped operation
0 // no templates file for
// COM port...
);
if(m_hCommPort) {
printf("OK\n");
}
char data[] = "123481278349789\r\n";
DWORD dwSize = strlen(data);
DWORD dwBytesWritten;
DWORD dwBytesRead;
while(true) {
if(WriteFile (m_hCommPort,data,dwSize,&dwBytesWritten ,0)) {
printf("sended: %d\n", dwBytesWritten);
} else {
printf("send fail: %d\n", dwBytesWritten);
}
printf("send done\n");
int i;
scanf("%d", &i);
}
return 0;
}
DataReceiver(从 COM3 接收)
class Program
{
static void Main(string[] args)
{
SerialPort sendingPort = new SerialPort("COM3", 9600);
try
{
sendingPort.Open();
char c = (char)sendingPort.ReadChar();
Console.WriteLine("Readed:" + c);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
}
}