Servizi UDP in visual basic 2005

venerdì 05 settembre 2008 - 19.07

dream Profilo | Newbie

Ciao a tutti.

Ho la necessità di stampare mediante una stampante epson con interfaccia di rete UB-E02.

Per la stampa non ho nessun problema, soltanto che, prima di stampare, vorrei verificare lo stato della stampante (coperchio chiuso, carta finita ecc).

Nella documentazione epson ho trovato un esempio in c++ che sfrutta le api di windows e vorrei tradurlo in visual basic.
/* UDP3289 programming sample for win32 * HOW TO BUILD * cc wudp.cpp wsock32.lib */ #include <stdio.h> #include <winsock.h> #define MAXBUF 512 char buf[MAXBUF]; int main(int argc, char* argv[]) { WSADATA data; SOCKET sock; struct sockaddr_in addr; int i, len, fromlen; if (argc != 2) { printf("usage: wudp <IP address>\n"); exit(1); } /* initialize windows socket */ WSAStartup(0x0101, &data); /* Create socket */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) { fprintf(stderr, "Error socket(): %d\n", WSAGetLastError()); exit(1); } /* initialize the parameter */ memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(3289); addr.sin_addr.s_addr = inet_addr(argv[1]); /* make a packet (PRINTER STATUS) */ buf[0] = 'E'; buf[1] = 'P'; buf[2] = 'S'; buf[3] = 'O'; buf[4] = 'N'; buf[5] = 'Q'; // PacketType(Q) buf[6] = 0x03; // DeviceType(3) buf[7] = 0x00; // DeviceNumber(0) buf[8] = 0x00; // Function(0010h) buf[9] = 0x10; buf[10] = 0x00; // Result buf[11] = 0x00; buf[12] = 0x00; // Parameter length buf[13] = 0x00; /* send a packet */ i = sendto(sock, buf, 14, 0, (struct sockaddr*)&addr, sizeof(addr)); /* receive packet */ fromlen = sizeof(addr); len = recvfrom(sock, buf, MAXBUF, 0, (struct sockaddr*)&addr, &fromlen); /* print receive packet */ if (len) { if ((buf[10] == 0x00) && (buf[11] == 0x00)) for (i = 0; i < len; i++) printf("%3d:%02Xh\n", i, buf[i] & 0xff); } /* close socket */ closesocket(sock); return 0; }

Ho provato a tradurlo così:

Imports System.io Imports System.Drawing.Printing Imports System.Runtime.InteropServices Imports System.Net Imports System.Net.Sockets Imports System.Text Dim listener As New UdpClient(9100) Dim done As Boolean = False Dim groupEP As New IPEndPoint("10.0.0.202", 9100) 'l'IP della stampante Dim buffer(13) As Byte buffer(0) = 69 'valore dec di E buffer(1) = 80 'valore dec di P buffer(2) = 83 'valore dec di S buffer(3) = 79 'valore dec di O buffer(4) = 78 'valore dec di N buffer(5) = 81 'valore dec di Q buffer(6) = 3 'valore dec di 3 buffer(7) = 0 buffer(8) = 0 buffer(9) = 16 'valore decimale di 10 buffer(10) = 0 buffer(11) = 0 buffer(12) = 0 buffer(13) = 0 'invio Try Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) Dim ep As New IPEndPoint("10.0.0.202", 9100) s.SendTo(buffer, ep) MessageBox.Show("inviato") Catch ex As Exception MessageBox.Show(ex.ToString) End Try Threading.Thread.Sleep(2000) 'ricezione - qui si blocca Try Dim bytes As Byte() = listener.Receive(groupEP) MessageBox.Show(Encoding.ASCII.GetString(bytes, 0, bytes.Length)) Catch ex As Exception MessageBox.Show(ex.ToString()) Finally MessageBox.Show("chiuso") listener.Close() End Try End Sub

LO scopo del programma è inviare la stringa di controll EPSONQ in modo da avere una risposta dalla stampante.

Spero di essere stato abbastanza chiaro.
Grazie per l'aiuto.
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5