Star TSP100Lan Receipt Printer + PHP + CentOS - php

We have a number of StarTSP100 receipt printers and can print to them fine from a browser using HTML and the jZebra Java Applet. Some customers, however, would like to have a printer without a physical till (when they have a large dining area and just want to print the check without going to the bar etc). To do this we are using a Star TSP100Lan printer, exactly the same a USB version we use, except for the connection.
As we're no longer communicating to the printer in a Windows environment via Java, but directly from the server (CentOS 6.4 with CUPS), the HTML is no parsed. While this is a shame, the manual for print shows that it can do a range formatting options which should suffice.
My editor of choice is vim and my code looks like this:
$ToPrint = sprintf("^[-%s^[-", 'My Text');
This is then sent to a known good process that that runs something similar to:
lpr -p star100 print.php
The part that looks like '^[' is the ASCII representation of what I think is the ESC (escape) code, which I make by doing CTRL+v then typing x1b, then typing -
Sadly my output does not come out underlined as expected.
My question is 2 fold:
1. Why doesn't the text underline?
2. How would I represent a more complex code, such as "ESC * r m l" (which would set the left margin)?
Thanks in advance,
James

Related

How do I make a PHP CLI script "call for attention" on Windows 10?

Icons on the Taskbar, or in an icon group in the Taskbar, can "blink" for attention. All kinds of programs do this all the time when they need human attention.
Now I want to do that for my own PHP CLI scripts (running as cmd.exe windows).
How do I accomplish this? I've tried several things with that ASCII "bell" thing, after being told about this, but it never works. The bell character neither makes the icon blink nor play a sound on Windows 10. So that is not the right solution.
I've searched for this for a long time, but I never get an answer.
The blink think may not be possible in Windows 10. But perhaps you can use color. See this answer to learn how to turn on Ansi Terminal Control in Win 10.
You may find this Wikipedia article helpful for learning the escape codes.
The blink "command" would be ESC [5m. I could not get it to work, and a search around the internet did not find any indication that it can work.
Here is an example in php that uses color.
echo chr(7); //Bell
echo chr(27)."[1;31;103m"; // 1= bright 31 = foreground color red 103 = background color yellow
echo "\nDANGER WILL ROBINSON\n\n\n\n\n\n\n\n\n";
echo "PAY ATTENTION\n\n\n\n\n\n\n\n\n\n";
echo chr(27)."[0m"; // turn off formatting

swap text/add specific word html/php code

I am looking to translate some very specific encoding to something that will improve its readability.
Example input
OUT PUT:
Copper Box v3;
S/N:25304;FW:1.07.12;
;a-b;a-GND;b-GND;
U=;0.74 V;3.23 V;0.48 V;
U~;0.03 V;0.02 V;0.02 V;
C;232.5 nF;11.87 nF;30.73 nF;
ISO;2.28 MΩ;237 kΩ;2.19 MΩ;
R;- -;
ΔC;- -;
Length; - m;
Desired output
OUT PUT:
U=;
A-B 0.74 V
A-G 3.23 V
B-G 0.48 V
U~;
A-B 0.03 V
A-G 0.02 V
B-G 0.02 V
C;
A-B 232.5 nF
A-G 11.87 nF
B-G 30.73 nF
ISO;
A-B 20.28 MΩ
A-G 237,1 kΩ
B-G 20.19 MΩ
Background
In my spare time I work with printed circuit boards as a hobby and try to fix broken machines for people, like coffee machines.
I have a new device with which I can measure things in an easy way.
But the device only gives a QR code that I can scan. It looks like the "example input" provided above.
I want to make it easier to read such text.
Manually changing it is a possibility; but it takes a lot of time. Sometimes I have 5 such measurements per hour.
Question
I would love to make a textarea box where I could paste it in, press a "beautify" button, and have code that makes the translation.
I was reading this, and even found a thing called the hanna code or so, but that did not give the little part I am looking for...
I know a little bit of PHP and HTML. Is there a way to use it in that language or do I need to learn how to use JavaScript or whatever?
Could you point me in the right direction? I would love to get this puzzle solved, but I don't even know where to start....
Here is a JavaScript solution. You can directly paste the output in the first box, and the second box will immediately give the beautified translation.
Of course, I had to make some assumptions about the syntax of the original text, so let me know how this meets your needs:
document.querySelector("#source").addEventListener("input", function () {
const lines = this.value.split(/^;/m).slice(1).join("").split("\n");
const cols = lines.shift().toUpperCase().split(";")
.filter(Boolean).map(code => code.slice(0,3));
document.querySelector("#target").value = "OUT PUT:\n" +
lines.filter(line => /;\d/.test(line))
.map(line => line.split(";").filter(Boolean))
.map(([s, ...v]) => s + "\n" + v.map((value, i) =>
cols[i] + " " + value
).join("\n"))
.join("\n");
});
<table>
<tr>
<th>Paste output here:</th><th>Beautified:</th>
</tr>
<tr>
<td><textarea id="source" cols="30" rows="18"></textarea></td>
<td><textarea id="target" cols="30" rows="18" readonly></textarea></td>
</tr>
</table>
Your question is very broad but reading it carefully I'm going to assume that you are not interested in a specific coding example but moreso a strategy. We're I looking at a problem like this in HTML I would without question code it in JavaScript.
The main difference between JavaScript (JS) and PHP in the example you've stated (ie, in HTML) is that PHP is a server-side language while JS is client based. This means that for PHP to solve the problem you need a webserver (a place to host the script), a way to post to the server, a PHP script on the server that can make the conversion for you.
In addition to that for the PHP solution you need a way for HTML to make the server side request to make the translation for you (translation meaning converting the first form of the data into the second) ie the HTML page needs to ask the webserver for the answer, post the original data to the server and receive (catch) the result and display it... generally this is all done in JavaScript using a technology called Ajax.
Another approach to solving the server problem w/out JavaScript is to use the HTML FORM tag with an associated INPUT to submit the data to the server. Then the server will render an entirely new page in HTML that contains the changed data and return that to the client (your web browser) replacing the old HTML page with the updated one. A lot of work!
A simpler solution would be to just use JavaScript which will allow you to do all the work in your client (the webbrowser) and will not require a webserver, re-rendering the page, etc. Far simpler and a better place to start if you're just exploring into coding - which from your question seems the case.
I would research JavaScript and some simple tutorials for reading/setting values on the page. As for how to actually code the function in JS to make the conversion of the data... that's relatively straight forward IF you can assume that the data is "fair" (ie, has no errors, bugs, etc) otherwise the problem of determining the veracity of the data is a far far more complex problem than the actual conversion (this is often the case in Computer Science).
All that said, I think it is an awesome project for starting to learn JS and think it's a great place to start. Take a look at these examples for some basics of JS and how to move data from one place to another.
https://formidableforms.com/knowledgebase/javascript-examples/

Does anybody know how to add hook for php storm for changing . to ->? [duplicate]

Let's say I want to type the following in PhpStorm:
$longObjectName->propertyName = 'some value';
Seems innocent enough, no? If I start typing longOb, code completion kicks in and helpfully provides $longObjectName as a suggestion, since it exists elsewhere in my project. I think to myself, "Ah perfect, that's exactly what I wanted", so I hit Enter or Tab to accept the suggestion. At this point, I'm feeling pretty happy.
But now I want to insert ->, PHP's awkward but familiar object operator. If I type it manually, that's three whole keystrokes (including Shift), which makes me feel just a bit sad. A distant, nagging doubt begins to enter my mind. "Three keystrokes? What kind of evil IDE is this? Who are these ruthless dictators at JetBrains??"
The emotional roller coaster continues when I see the following in PhpStorm's Tip of the Day dialog, bringing a minuscule but insistent glimmer of hope to my dark, Monokai-schemed world:
When using Code Completion, you can accept the currently highlighted
selection in the popup list with the period character (.), comma (,),
semicolon (;), space and other characters.
The selected name is
automatically entered in the editor followed by the entered character.
In JavaScript, this means I can type longOb and hit . to both accept the first code completion suggestion and insert the JS object operator, resulting in longObjectName., at which point I can keep typing a property name and go on autocompleting all day long without ever hitting Enter. Amazing. Revolutionary even.
Now for some devastating news: it doesn't seem to work in PHP. (Fret not children—this harrowing tale is almost at its end.)
If I type longOb and then hit -, I get this:
longOb- // :(
I'm pretty sure the PHP interpreter wouldn't like me very much if I tried to execute that.
(Side note: ., ,, and ; exhibit pretty much the same behavior, contrary to the quoted Tip of the Day above.)
So here's what I would get if I were to make my fantasy world a reality:
$longObjectName->[handy dandy code completion list, primed and ready for action]
Wouldn't that be flipping awesome?
So finally, we arrive at the ultimate question, with some redundant stuff added for those who didn't bother to read my action-packed, heartwrenching story:
Is there a single keyboard shortcut in PhpStorm for "Accept the currently highlighted code completion suggestion and insert the PHP object operator (->)"?
Or is this just a bug?
Well, I solved this problem by recording a Macro and then binding it with a keyboard shortcut:
Go to Edit | Macros | Start Macro Recording
Type '->'
Stop Macro Recording using the button in the lower right corner, then name it whatever you want.
Assign Shortcut to it:
Go to File | Settings | Keymap | Macros |
Right click and choose 'Add Keyboard Shortcut'
I chose Ctrl+. as the shortcut and now I am more than happy. :)
You can use autohotkey (http://www.autohotkey.com/) to create new keystrokes and replace PHP Object Operator for period or anything else.
For example, with a script like this:
^.::
Send ->
return
Will replace (ctrl + .) with (->), in anywhere in Windows.
Or
#IfWinActive ahk_class SunAwtFrame
.::
Send ->
return
Will replace . (period) with (->) only in PhpStorm Window (Or others with ahk_class SunAwtFrame).
Use Window Spy to see the ahk_class class of any Windows Window.
You can use CTRL + . as . (period)
#IfWinActive ahk_class SunAwtFrame
^.::
Send .
return
1) As far as I remember this kind of completion was asked to be removed for PHP code (too unusual) .. but I cannot find such ticket right now (already spend over 20 mins on this) .. so I could be wrong (it was quite some time ago .. so possibly I'm confused with another code-completion improvement which was hard-configured to behave differently for PHP code only).
Kind of related to "why it was disabled/what problems where there in the past":
http://youtrack.jetbrains.com/issue/WI-7013
http://youtrack.jetbrains.com/issue/IDEA-88179
http://youtrack.jetbrains.com/issue/IDEA-59718
In any case: there is an open ticket to actually having it working: http://youtrack.jetbrains.com/issue/WI-21481 (only 1 vote so far).
2) There is no "complete with ->" action available. Related ticket: http://youtrack.jetbrains.com/issue/WI-17658 (star/vote/comment to get notified on progress)

Communicateur with client-sided serial port through web

I'm having an issue with my PHP website (which is using an API, that's why it has to be PHP).
This website is launched on a raspberry pi b+ which is connected to a thermal printer (through serial port), I used a python script to test the printer.
Now my question is: Is it possible to send data through the web to make the raspberry print some data ? So send an instruction like write to the port '/dev/ttyxxx' client sided?
Thanks for your help
If you mean: "I have a PHP application that needs to access the server's serial port":
It is possible for PHP to access the serial port on the server (in this case, your raspberry pi). PHP treats it like it is a normal file.
From the PHP Fopen page:
<?php
// Set timeout to 500 ms
$timeout=microtime(true)+0.5;
// Set device controle options (See man page for stty)
exec("/bin/stty -F /dev/ttyS0 19200 sane raw cs8 hupcl cread clocal -echo -onlcr ");
// Open serial port
$fp=fopen("/dev/ttyS0","c+");
if(!$fp) die("Can't open device");
// Set blocking mode for writing
stream_set_blocking($fp,1);
fwrite($fp,"foo\n");
// Set non blocking mode for reading
stream_set_blocking($fp,0);
do{
// Try to read one character from the device
$c=fgetc($fp);
// Wait for data to arive
if($c === false){
usleep(50000);
continue;
}
$line.=$c;
}while($c!="\n" && microtime(true)<$timeout);
echo "Responce: $line";
?>
If you mean: "I have a website that somehow needs to send something to the client's serial port"
Then the only solution is a browser App.
There's the Chrome Serial API which chrome apps can use.
Video Example
I come to think of several solutions; basically you would want your php-page to parse the data and create a trusted output that can be printed (i.e. a PDF-file, if your printer supports this).
Your next task is how to have this trusted output sent to the printer. Again, several solutions exists.
Have your php-script execute a system executable, e.g. cat output.pdf > /dev/ttyxxx (it is clear here, that I do not know how to print from unix). Notice that the executable is not dependent on input at all, i.e. you want to reduce the risk of injection attacks and the like. This point requires that the output.pdf, that you created, is trustworthy.
Have a cron-job look for output files and send them to the printer. Same considerations as above apply. This might be better as it can avoid bottlenecks if multiple php-sessions are trying to print a document.
Build a smaller framework around the above that can report back if errors occur etc. But still, basically option 1 + magic.
All in all, split the process into two steps. One that accepts the input, parses and checks for erroneousness/malevolent input, and creates the needed output for the printer. This can be done in a protected environment, which if hacked, does not expose the system (at least not more than the usual php would).
Step 2 then takes care of sending the output to the hardware, either bash-script, executable, or python.

integrating barcode scanner into php application?

We have been developing web application in php.
We need barcode scanner to be integrated into our application.
Our application is divided into two modules, users and merchant.
When user comes and scans the card, merchant should be identified the user by barcode number. Admin will give barcode number and that is being sent to the card manufacturer and the number will be assigned to the magnetic stripe.
As i know scanner can acts as a key board, can you please tell the method to integrate barcode scanner into this php web based application?
PHP can be easily utilized for reading bar codes printed on paper documents. Connecting manual barcode reader to the computer via USB significantly extends usability of PHP (or any other web programming language) into tasks involving document and product management, like finding a book records in the database or listing all bills for a particular customer.
Following sections briefly describe process of connecting and using manual bar code reader with PHP.
The usage of bar code scanners described in this article are in the
same way applicable to any web programming language, such as ASP,
Python or Perl. This article uses only PHP since all tests have been
done with PHP applications.
What is a bar code reader (scanner)
Bar code reader is a hardware pluggable into computer that sends decoded bar code strings into computer. The trick is to know how to catch that received string. With PHP (and any other web programming language) the string will be placed into focused input HTML element in browser. Thus to catch received bar code string, following must be done:
just before reading the bar code, proper input element, such as INPUT TEXT FIELD must be focused (mouse cursor is inside of the input field).
once focused, start reading the code
when the code is recognized (bar code reader usually shortly beeps), it is send to the focused input field. By default, most of bar code readers will append extra special character to decoded bar code string called CRLF (ENTER). For example, if decoded bar code is "12345AB", then computer will receive "12345ABENTER". Appended character ENTER (or CRLF) emulates pressing the key ENTER causing instant submission of the HTML form:
<form action="search.php" method="post">
<input name="documentID" onmouseover="this.focus();" type="text">
</form>
Choosing the right bar code scanner
When choosing bar code reader, one should consider what types of bar codes will be read with it. Some bar codes allow only numbers, others will not have checksum, some bar codes are difficult to print with inkjet printers, some barcode readers have narrow reading pane and cannot read for example barcodes with length over 10 cm. Most of barcode readers support common barcodes, such as EAN8, EAN13, CODE 39, Interleaved 2/5, Code 128 etc.
For office purposes, the most suitable barcodes seem to be those supporting full range of alphanumeric characters, which might be:
code 39 - supports 0-9, uppercased A-Z, and few special characters (dash, comma, space, $, /, +, %, *)
code 128 - supports 0-9, a-z, A-Z and other extended characters
Other important things to note:
make sure all standard barcodes are supported, at least CODE39, CODE128, Interleaved25, EAN8, EAN13, PDF417, QRCODE.
use only standard USB plugin cables. RS232 interfaces are meant for industrial usage, rather than connecting to single PC.
the cable should be long enough, at least 1.5 m - the longer the better.
bar code reader plugged into computer should not require other power supply - it should power up simply by connecting to PC via USB.
if you also need to print bar code into generated PDF documents, you can use TCPDF open source library that supports most of common 2D bar codes.
Installing scanner drivers
Installing manual bar code reader requires installing drivers for your particular operating system and should be normally supplied with purchased bar code reader.
Once installed and ready, bar code reader turns on signal LED light. Reading the barcode starts with pressing button for reading.
Scanning the barcode - how does it work?
STEP 1 - Focused input field ready for receiving character stream from bar code scanner:
STEP 2 - Received barcode string from bar code scanner is immediatelly submitted for search into database, which creates nice "automated" effect:
STEP 3 - Results returned after searching the database with submitted bar code:
Conclusion
It seems, that utilization of PHP (and actually any web programming language) for scanning the bar codes has been quite overlooked so far. However, with natural support of emulated keypress (ENTER/CRLF) it is very easy to automate collecting & processing recognized bar code strings via simple HTML (GUI) fomular.
The key is to understand, that recognized bar code string is instantly sent to the focused HTML element, such as INPUT text field with appended trailing character ASCII 13 (=ENTER/CRLF, configurable option), which instantly sends input text field with populated received barcode as a HTML formular to any other script for further processing.
Reference: http://www.synet.sk/php/en/280-barcode-reader-scanner-in-php
Hope this helps you :)
You can use AJAX for that. Whenever you scan a barcode, your scanner will act as if it is a keyboard typing into your input type="text" components. With JavaScript, capture the corresponding event, and send HTTP REQUEST and process responses accordingly.
I've been using something like this. Just set up a simple HTML page with an textinput. Make sure that the textinput always has focus. When you scan a barcode with your barcode scanner you will receive the code and after that a 'enter'. Realy simple then; just capture the incoming keystrokes and when the 'enter' comes in you can use AJAX to handle your code.
If you have Bluetooth, Use twedge on windows and getblue app on android, they also have a few videos of it. It's made by TEC-IT. I've got it to work by setting the interface option to bluetooth server in TWedge and setting the output setting in getblue to Bluetooth client and selecting my computer from the Bluetooth devices list. Make sure your computer and phone is paired. Also to get the barcode as input set the action setting in TWedge to Keyboard Wedge. This will allow for you to first click the input text box on said form, then scan said product with your phone and wait a sec for the barcode number to be put into the text box. Using this method requires no php that doesn't already exist in your current form processing, just process the text box as usual and viola your phone scans bar codes, sends them to your pc via Bluetooth wirelessly, your computer inserts the barcode into whatever text field is selected in any application or website. Hope this helps.

Categories