Ensure connection to a POSPrinter connected via COM - vb.net

I need to make sure that the connection to a POS printer is successful before writing data to the database and then printing a receipt. The POSprinter is normally of type BTP 2002NP but may differ. The common thing is that they are all connected via COM-port and NOT usb, so no drivers installed at all on the client.
Can I send some kind of "ping" on a COM-port and check if a device is connected and turned on? Any help or suggestions are very much appreciated.
Additional information, the application is developed in VB.net and Visual Studio 2008

About all you can do is write out a character string to the com port and wait and see if your printer responds with a reply. However the string you write and the string you expect will depend on the printer itself.
Refer to the BTP 2002NP printers programming manual for examples (the first link in google that I looked at)
From looking at the manual an appropriate string to send to the printer is the "DLE EOT n" command which requests that the printer send back its current status.
As for other printers in the range, check out this list of drivers and manuals

btw, this is what i came up with in the end.
Public Function ComTest() As Byte()
Dim TXT As String
TXT = Chr(&H10S) & Chr(&H4S) & Chr(1) 'DLE EOT 1
If OpenCom() Then 'Connect to com
moRS232.PurgeBuffer(Rs232.PurgeBuffers.TxClear Or Rs232.PurgeBuffers.RXClear)
moRS232.Write(TXT)
moRS232.Read(1)
Return moRS232.InputStream
Else
Return Nothing
End If
End Function
the function returns 1 byte. i can then from the manual translate this byte into what state the printer is currently in. this probably works for all ESC/P printers.

Related

How do I write VB.NET program to remotely control 6060B (electric load)

I am writing a vb.net program to remotely control the 6060B (electric load). So far I have successfully connected my pc to the 6060B and I am able to query information from the load. Below is the part of the code I wrote:
Dim mbSession As MessageBasedSession;
mbSession = ResourceManager.GetLocalManager().Open("GPIB::6::INSTR");
Dim responseString As String = mbSession.Query("*idn?");
This returns me the information of the load -- "responseString is HEWLETT-PACKARD...". However, I don't know what should I do to change/set the current, voltage ect just as I normally do from the panel. I search on the internet and I found that I could use HPSL programming language but what should I remotely control the 6060B using vb.net? I am using NI-VISA.NET API.
You need to find the command reference. There is a standard, SCPI, which has commands generally in the form
MEASure:CURRent?
Again this is a standard, and you should find the specific command reference for your device, HP (Keysight / Agilent) 6060B 300 Watt DC Electronic Load.
A search engine result for hp 6060b manual should yield some good results. Look for an operating or programming manual which usually has the command reference.
This should work for you:
' This example sets the current level to 0.75 amps
' and then reads back the actual current value.
' set input off
mbSession.Write("INPUT OFF")
' set mode to current
mbSession.Write("MODE:CURR")
' set current range
mbSession.Write("CURR:RANG 1")
' set current value
mbSession.Write("CURR 0.75")
' set input on
mbSession.Write("INPUT ON")
' measure current
Dim result As String
result = mbSession.Query("MEAS:CURR?")
Dim measuredCurrent As Single = Single.Parse(result)
Example taken from page 70 of this operating manual I found.
In general, things are usually easier if you are provided example code. I will usually use the example code to get a baseline working operation, then copy the code to my project and manipulate as needed.

TelnetExpect Reading Output from Stream in VB.NET

Good Morning,
I have been tasked with writing a program that locates the local port a MAC Address is registered to, and to do that I need to SSH and Telnet into Cisco devices and receive the output of the commands so I can parse it and decide what to do with the information.
So far I have the SSH connection and parsing done no problem thanks to Renci.SshNET. But I am having trouble with the Telnet, I set it up to use Telnetlib.dll and I know it works because I can do "show users" on the switch I'm connecting to and see that one of the vty lines are being occupied by my application. But in the application I'm writing all I get is the application freezing up, none of the buttons work, and I can't see the mouse when I hover over the window, so I'm guessing that the application isn't liking something at run-time.
The code I'm using, in VB.NET is:
Using client As TcpClient = New TcpClient(host, 23)
Dim tel As TelnetStream = New TelnetStream(client.GetStream)
Dim buff(client.ReceiveBufferSize) As Byte
Dim result As String
tel.SetRemoteMode(TelnetOption.Echo, False)
Dim exp As Expector = New Expector(tel)
exp.Expect("username: ")
exp.SendLine(username)
exp.Expect("password: ")
exp.SendLine(password)
exp.Expect(">")
tel.Read(buff, 0, buff.Length)
result = System.Text.Encoding.ASCII.GetString(buff)
MessageBox.Show(result)
End Using
Telnetlib.dll include the definitions for the Expector, SendLine, Expect, and TelnetStream.
I did some debugging and found that it freezes when it goes to execute the result = System.Text.Encoding.ASCII.GetString(buff)
command.
Thanks for any help.

Crystal report printing to wrong printer

I'm troubleshooting an issue with a VB.NET app that I inherited.
The following lines execute print operation:
Me.rptShippingLabel1.PrintOptions.PrinterName = "LabelPrinter"
Me.rptShippingLabel1.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Landscape
Me.rptShippingLabel1.PrintToPrinter(Me.txtLabelQty.Text, False, 1, 1)
There is a Zebra ZDesign TLP2844 connected to workstations via direct USB and named LabelPrinter. However, despite target printer being specified in the code, that Zebra must be set as default printer in Windows, otherwise the job would go to any other printer set as default.
What's even more frustrating is that on some computers, with exactly the same configuration jobs go to correct printer but I can't identify controllable pattern.
Any suggestions why might that be?
Reports in question are disassociated from printer in Design>Page Settings.
Look into your report. In page settings see if report is optimized for display. If yes, uncheck it. If report is optimized for displaying only, PrinterOptions enumeration is being discarded. You could still assign to Printer name property in older framework but not anymore.
Try the following code
Dim rptShippingLabel1 As New CrystalReport1
Dim PrinterSettings1 As New Printing.PrinterSettings
Dim PageSettings1 As New Printing.PageSettings
'Replace it with your printer name
PrinterSettings1.PrinterName = "Microsoft XPS Document Writer"
rptShippingLabel1.PrintToPrinter(PrinterSettings1, PageSettings1, False)
to get the printer name, don't read it from rptShippingLabel1.PrintOptions.PrinterName it will show blank. Try reading from PrinterSettings1.PrinterName.
This is tested with Crystal Reports runtime 13.0.9

Agilent Power Supply Programming using GPIB

On looking at the examples provided in the documentation of the Power supply. The Programming has been done by adding two libraries AgilentRMLib and VisComLib in the C#. When
i try to add the AgilentRMLib by Selecting the Add Reference->Agilent VISA COM Resourse Manager 1.0, an error is shown at the reference.
I tried adding the agtRM.dll directly from the Program Files. Still the error persists. Has anyone faced this problem before? Any Solutions for this? Do you have any other method to program the Power Supply from PC using Agilent IO.
I was able to use the VisaComLib(GlobMgr.dll) instead to program the GPIB using C# programming language.
The pdf file link!
was used as reference.
If you don't mind using VBA, this code can help you accomplish what you are trying to do, make sure it has VISA COM 488.2 Formatted I/O in the References:
Public Sub TestVISA()
Dim Dev_IO As VisaComLib.FormattedIO488
Dim io_manager As VisaComLib.ResourceManager
'Start of Open GPIB port (or any VISA resource)
Set io_manager = New VisaComLib.ResourceManager
Set Dev_IO = New VisaComLib.FormattedIO488
Set Dev_IO.IO = io_manager.Open("GPIB0::x::INSTR") ' x is the GPIB address number of the Dev_IOument
Set io_manager = Nothing
Dev_IO.IO.Timeout = 10000 'set time out to 10 seconds, use this line to change timeout to any time out value per VISA spec
'End of Open GPIB port
'Send some SCPI command to the Dev_IOumnet
Dev_IO.WriteString ("*IDN?")
MsgBox ("Connected to: " & Dev_IO.ReadString)
'Close the port upon completion
Dev_IO.IO.Close
Set Dev_IO = Nothing 'release the object
End Sub
As you are using GPIB protocol, better use GPIB libraries and wrapper then code with native SCPI commands. That way your software will be more dependent to your applications and you can control almost everything. With VISA interface you have to worry about another layer but with this approach you can directly control your devices efficiently. I worked with VISA for couple of years but after that hardworking times now I can build my measurement systems with direct GPIB programming. You can find required libraries from NI's or Agilent's website.

protecting software to run only on one computer in vb.net

I have developed a small application and now i want to protect it.
I want to run it only on my own computer and i have developed it for myself.
How can i do that?
A. Don't publish it.
B. Hard-code your computer name in the code, and make the first thing the program does to be verifying that System.Environment.MachineName matches it.
You could always check the processor ID or motherboard serial number.
Private Function SystemSerialNumber() As String
' Get the Windows Management Instrumentation object.
Dim wmi As Object = GetObject("WinMgmts:")
' Get the "base boards" (mother boards).
Dim serial_numbers As String = ""
Dim mother_boards As Object = _
wmi.InstancesOf("Win32_BaseBoard")
For Each board As Object In mother_boards
serial_numbers &= ", " & board.SerialNumber
Next board
If serial_numbers.Length > 0 Then serial_numbers = _
serial_numbers.Substring(2)
Return serial_numbers
End Function
Private Function CpuId() As String
Dim computer As String = "."
Dim wmi As Object = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
computer & "\root\cimv2")
Dim processors As Object = wmi.ExecQuery("Select * from " & _
"Win32_Processor")
Dim cpu_ids As String = ""
For Each cpu As Object In processors
cpu_ids = cpu_ids & ", " & cpu.ProcessorId
Next cpu
If cpu_ids.Length > 0 Then cpu_ids = _
cpu_ids.Substring(2)
Return cpu_ids
End Function
Was taken from where: http://www.vb-helper.com/howto_net_get_cpu_serial_number_id.html
Here's a question by Jim to convert this for Option Strict.
It really depends on who is the "enemy".
If you wish to protect it from your greedy, non-cracker, friends, then you can simply have the application run only if a certain password is found in the registry (using a cryptographically secure hash function), or use the MachineName as Jay suggested.
But if you're thinking of protecting it from serious "enemies", do notice: It has been mathematically proven that as long as the hardware is insecure, any software running on it is inherently insecure. That means that every piece of software is crackable, any protection mechanism is bypassable (even secured-hardware devices such as Alladin's Finjan USB product key, since the rest of the hardware is insecure).
Since most (if not all) of today's hardware is insecure, you simply cannot get 100% security in a software.
In between, there are lots of security solutions for licensing and copy-protection. It all comes down to who is the enemy and what is the threat.
No matter how hard you try, if someone really want to run it on another computer, they will.
All need to do is reverse engineer your protection to
remove it
play with it
Another option might be to have your program ask the USER a question that has a derived answer. Here's a brain dead example....
Your Program: "What time is it now?"
You Enter: (TheYear + 10 - theDay + 11) Mod 13
In this way its actually ONLY YOU that can run the program instead of it being MACHINE dependent.
I have made things like this in VB DOS.
I either made a non-deletable file that is key to a specific machine with a code inside, and/or read the .pwl files and have several checks, that are only on your machine. The non-editable file is made with extended character sets like char 233 so when a person tries to look at it, it will open a blank copy (edit) (write.ex), so data cannot be read and it cannot be edited moved or deleted.
It needs to be certain characters; I am not sure if every charter between 128 and 255 will work it, some extended characters work to do this some will not, also it can be defeated, but it will keep some people out,
But it can be read or checked in a program environment. Nothing is totally secure, this is some of the things I mess with.
Note: the file will be very hard to delete, maybe make a test directory to test this.
I hope this is OK I am not very good at conveying info to people; I have programmed since 1982.
Another idea ... I wrote a program that cannot be run directly, it is only ran by an external file, so you could add in a password entry section to it and encrypt password so it cannot be read very easily ,I made an executable version of a vb program to test. it writes in to slack space a character so if the program sees that value it will not run, BUT the runner program has a different character, and it changes it to that character ,and the program is designed to only let in if the character is the proper one ,made only by the runner , then when it enters it changes it back so it is not left open , I have made this sorta thing, and it does work, there is always a way to defeat any protection , the goal is to slow them down or discourage them from running or using your program if you do not want them to.I may include examples at a later date.