📜 ⬆️ ⬇️

One possible problem when working with RS232

In this topic, I would like to share my experience of “fighting” with one Chinese device. At work, the order was received and it was decided to change the old card readers to new ones. The bottom line is that the old readers were active, i.e. in an endless cycle they tried to count the map, but the new ones were already passive - they would not work until you gave them a command. Model of the device CR501AU V3 (if someone come in handy). Chinese device, there is no documentation (only in Chinese). And all I had on my hands was a box with a model label and some source. The source code, by the way, was from our other department, which was written in 1C. But, since the staff of programmers in our office has been updated, there are no people left who worked on these old sources, so we had to figure it out. At first, everything was simple. In the source code 1C was inserted a piece of the script in Visual Basic, which was fully responsible for working with the reader. There was nothing at hand to work with VB, so I used the Excel compiler.

Function ReedCard() MSComm1.CommPort = 1 '  MSComm1.Settings = "19200,N,8,1" '  MSComm1.InputLen = 0 On Error Resume Next MSComm1.PortOpen = True MSComm1.Output = Chr(&HAA) & Chr(&HBB) & Chr(&H6) & Chr(&H0) & Chr(&H0) & Chr(&H0) & Chr(&H1) & Chr(&H2) & Chr(&H52) & Chr(&H51) '      .    "" .    hex  sleep (0.1) ' ,    (     ) Answer = MSComm1.Input MSComm1.Output = Chr(&HAA) & Chr(&HBB) & Chr(&H5) & Chr(&H0) & Chr(&H0) & Chr(&H0) & Chr(&H2) & Chr(&H2) & Chr(&H0) '    .  ,    —  . ,   ASCII- sleep (0.1) Answer = MSComm1.Input Dim ReadPort As String If Len(Answer) > 10 Then '       ,         14- .        ,      ,      5 . ,   >10,    Dim a: a = s2a(Answer) 'StringToArray-.      14   [14] Dim i For i = 0 To UBound(a) a(i) = Right(0 & Hex(Asc(a(i))), 2) '    ""    - ASCII-.      ,   ascii-     hex Next ReadPort = Join(a) MSComm1.Output = Chr(170) & Chr(187) & Chr(6) & Chr(0) & Chr(0) & Chr(0) & Chr(6) & Chr(1) & Chr(7) & Chr(0) '      ,  "" :) ..    (  ,   ,    ) ''''     ,       . : AA BB CC DD -> 2864434397.   ,      —      . End If MSComm1.PortOpen = False End Sub Function s2a(s) ReDim a(Len(s) - 1) Dim i For i = 0 To UBound(a) a(i) = Mid(s, i + 1, 1) Next s2a = a End Function Sub sleep(sk) PauseTime = sk Start = Timer Do While Timer < Start + PauseTime Loop End Sub 

Not so difficult. But the problem was that I needed to transfer this code to VB to Pascal in MS-DOS. For several days he struggled with this, but feeding these commands to the Com-port reader did not show signs of life. Then I decided to first implement it in my “native” language - C #. I also had to sweat, but in the end I understood what the key lies!

 serialPort1.Open(); serialPort1.Write(((char)0xAA) + "" + ((char)0xBB) + "" + ((char)0x6) + "" + ((char)0x0) + "" + ((char)0x0) + "" + ((char)0x0) + "" + ((char)0x6) + "" + ((char)0x1) + "" + ((char)0x7) + "" + ((char)0x0)); serialPort1.Close(); 

The code above is incorrect. I experimented with him for some time, but he didn’t want to work at all. And quotes removed. And .ToString () added. And he wrote a letter to the port. Nothing. And then suddenly it came:

 serialPort1.Open(); byte[] ar = new byte[10]; ar[0] = 170; ar[1] = 187; ar[2] = 6; ar[3] = 0; ar[4] = 0; ar[5] = 0; ar[6] = 6; ar[7] = 1; ar[8] = 7; ar[9] = 0; serialPort1.Write(ar, 0, ar.Length); serialPort1.Close(); 

And it all worked! It remains only to transfer it to Pascal:
 var ar: array of Byte[10]; begin OpenCom (ComNo, B_19200, Bits_8+Stops_1+Parity_No, 2048); ar[0]:=170; ar[1]:=187; ar[2]:=6; ar[3]:=0; ar[4]:=0; ar[5]:=0; ar[6]:=6; ar[7]:=1; ar[8]:=7; ar[9]:=0; for i:=0 to 10 do begin WriteCom(ComNo,ar[i]); end; 

The code above makes the reader "peep". Further remake everything for themselves is very simple.
')
PS I didn’t find any information on this problem at all in the ru segment, but there are some English-speaking ones, but there are clumps. From myself information on working with RS232 on the example of a Chinese card reader collected. I hope someone will help. Good luck!

Source: https://habr.com/ru/post/322422/


All Articles