socket.BeginReceive() 导致打印机无响应

发布于 2024-11-07 02:53:12 字数 2575 浏览 0 评论 0原文

我一直在开发一个应用程序,该应用程序可以在 PDF 文件到达时自动在特定网络打印机上打印并监视 @PJL USTATUS JOB 消息。但是,每当我启动应用程序并且代码命中 socket.BeginReceive() 时,打印机无法从任何工作站接收任何打印作业,这也意味着永远不会调用回调。但是,如果我关闭应用程序或仅关闭套接字,打印机将再次开始工作。

我错过了什么吗?喜欢阻塞/非阻塞或同步或异步套接字?

public class SocketData {
    public Socket socket;
    public byte[] dataBuffer = new byte[1024];
}

public void Start() {
    // start the thread that checks the printer status
    thread = new Thread(new ThreadStart(ConnectAndListen));
    thread.Priority = ThreadPriority.Lowest;
    thread.Start();

    // start the file listener
    listener = new FileSystemWatcher(this.WatchPath);
    listener.Created += new FileSystemEventHandler(listener_Created);
    listener.EnableRaisingEvents = true;
}

public void ConnectAndListen() {
        ipEndPoint = new IPEndPoint(Dns.GetHostEntry(this.IPAddress).AddressList[0], 9100);
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(ipEndPoint);

        //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS DEVICE = ON\r\n\x1B%-12345X\r\n"));
        //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS JOB = ON\r\n\x1B%-12345X\r\n"));
        socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS PAGE = ON\r\n\x1B%-12345X\r\n"));
        //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS TIMED = 0\r\n\x1B%-12345X\r\n"));

        ListenForPrinterReadBack(socket);
}

public void ListenForPrinterReadBack(Socket socket) {
    if (DataRecievedCallBack == null) {
        DataRecievedCallBack = new AsyncCallback(OnDataReceived);
    }

    SocketData socketData = new SocketData();
    socketData.socket = socket;

    m_asynResult = socket.BeginReceive(socketData.dataBuffer, 0, socketData.dataBuffer.Length, SocketFlags.None, DataRecievedCallBack, socketData);
}

public void OnDataReceived(IAsyncResult asyn) {
    SocketData socketData = (SocketData)asyn.AsyncState;

    int result = socketData.socket.EndReceive(asyn);

    char[] chars = new char[result + 1];
    Decoder d = Encoding.UTF8.GetDecoder(); //creating object for decoding
    int charLen = d.GetChars(socketData.dataBuffer, 0, result, chars, 0);
    String recievedData = new String(chars);

    this.Status = recievedData;

    ListenForPrinterReadBack(socketData.socket);
}

我知道我可以进行轮询,但我无法接收未经请求的打印机消息。

提前致谢!

更新: 我感觉打印机上只允许一个套接字连接。由于 socket.BeginReceive() 仅在套接字已连接时才能工作,因此无法发送任何其他请求,因为该套接字已被使用。也许还有另一种方法来处理未经请求的打印机读回?

I've been developing an application that automatically prints PDF files on specific network printers upon arrival and monitoring @PJL USTATUS JOB messages. However, whenever I begin my application and the code hits socket.BeginReceive(), the printer is unable to receive any print jobs from any workstation which also means that the callback will never be called. However, if I close my application or just the socket, the printer begins working again.

Am I missing anything? Like blocking/non-blocking or synchronous or asynchronous sockets?

public class SocketData {
    public Socket socket;
    public byte[] dataBuffer = new byte[1024];
}

public void Start() {
    // start the thread that checks the printer status
    thread = new Thread(new ThreadStart(ConnectAndListen));
    thread.Priority = ThreadPriority.Lowest;
    thread.Start();

    // start the file listener
    listener = new FileSystemWatcher(this.WatchPath);
    listener.Created += new FileSystemEventHandler(listener_Created);
    listener.EnableRaisingEvents = true;
}

public void ConnectAndListen() {
        ipEndPoint = new IPEndPoint(Dns.GetHostEntry(this.IPAddress).AddressList[0], 9100);
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(ipEndPoint);

        //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS DEVICE = ON\r\n\x1B%-12345X\r\n"));
        //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS JOB = ON\r\n\x1B%-12345X\r\n"));
        socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS PAGE = ON\r\n\x1B%-12345X\r\n"));
        //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS TIMED = 0\r\n\x1B%-12345X\r\n"));

        ListenForPrinterReadBack(socket);
}

public void ListenForPrinterReadBack(Socket socket) {
    if (DataRecievedCallBack == null) {
        DataRecievedCallBack = new AsyncCallback(OnDataReceived);
    }

    SocketData socketData = new SocketData();
    socketData.socket = socket;

    m_asynResult = socket.BeginReceive(socketData.dataBuffer, 0, socketData.dataBuffer.Length, SocketFlags.None, DataRecievedCallBack, socketData);
}

public void OnDataReceived(IAsyncResult asyn) {
    SocketData socketData = (SocketData)asyn.AsyncState;

    int result = socketData.socket.EndReceive(asyn);

    char[] chars = new char[result + 1];
    Decoder d = Encoding.UTF8.GetDecoder(); //creating object for decoding
    int charLen = d.GetChars(socketData.dataBuffer, 0, result, chars, 0);
    String recievedData = new String(chars);

    this.Status = recievedData;

    ListenForPrinterReadBack(socketData.socket);
}

I know I could do polling, but i won't be able to receive unsolicited printer messages.

Thanks in advance!

UPDATE:
I'm getting the feeling that only one socket connection is allowed on the printers. Since socket.BeginReceive() can only work if the socket is connected, then any other requests cannot be sent since the socket is already being used. Maybe there's another way to handle unsolicited printer readbacks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若言繁花未落 2024-11-14 02:53:12

Send 是否报告所有字节已发送(检查其返回值)?我无法发现任何会阻止另一端接收您的消息的错误。您是否 100% 确定您发送的字符串是正确的?

Do Send report that all bytes have been sent (check it's return value)? I can't spot any errors that would prevent the other end from receiving your message. Are you 100% sure that the string you are sending is correct?

绻影浮沉 2024-11-14 02:53:12

您好,我在使用打印机时发现与您相同的问题,

首先:确保打印机处于“双向”模式并尝试此代码:

  private void SendCommand(byte[] data, string ip, int port)
    {
        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.NoDelay = true;

        var ipAddress = IPAddress.Parse(ip);
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

        socket.Connect(remoteEP);
        socket.Send(data);
        socket.Close();
    }

它会正常工作。

此致

Hi I found same problem as yours when using printer

Firstly : Be sure the printer is on "Bi-Directional" mode and try this code:

  private void SendCommand(byte[] data, string ip, int port)
    {
        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.NoDelay = true;

        var ipAddress = IPAddress.Parse(ip);
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

        socket.Connect(remoteEP);
        socket.Send(data);
        socket.Close();
    }

and it will work good.

Best Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文