将数据从并行写入串行端口

时间:2014-08-05 06:43:51

标签: c# c++ winapi serial-port parallel-port

我们编写了一个程序,将简单数据从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();
    }
}

0 个答案:

没有答案