How to Convert I / O Port Access from Turbo Pascal for Win32 to C # .Net

I have a coin counter (SC350 / 360) that connects to a computer via the RS232C interface. I have technical documentation that describes communication protocols, and a pascal work program is also included to control the machine. I copied the pascal code and tested it on Turbo Pascal using DosBox, for Windows 7 64 bit, and the code compiles successfully. What I wanted to achieve was to convert these codes to C # .Net, but it was difficult for me to convert several lines to C #, since I do not have much experience programming on the serial port.

This was the code in Pascal to initialize communication with the machine. (Set baud rate to 9600, 8 bit, no parity, 1 stop bit)

uses crt;
const
{ COM1: RS232 port address }
RXTX = $3F8; { $2F8 if COM2: is used }
ACK = 6;
NAK = 21;
ESC = 27;
var
dummy,
checkSum : integer;
key : char;
protocol : integer;

    var i : integer;
begin
i := 1843200 div 9600 div 16;
port[RXTX + 3] := $80;
port[RXTX + 1] := hi(i);
port[RXTX]:= lo(i);
port[RXTX + 3] := 3;
port[RXTX + 4] := $A;
while odd(port[RXTX + 5]) do
begin
dummy := port[RXTX];
delay(10);
end;
end; { InitComm }

# ; (, , )

SerialPort port=new SerialPort("COM1",9600,Parity.None,8,StopBits.One);

, pascal. , , - :

procedure Tx(data : integer);
{ Transmit a character on serial channel }
begin
while port[RXTX + 5] and $20 = 0 do;
port[RXTX] := data and $FF;
end; { Tx }

function RxWait : integer;
{ Waits for a character from serial channel }
begin
while not odd(port[RXTX + 5]) do;
RxWait := port[RXTX];
end; { RxWait }

procedure Tx2(data : integer);
{ Transmit a char on serial channel + Calculate check sum }
begin
Tx(data);
checkSum := (checkSum + data) and $FF;
end; { Tx2 }

, , , #? , "port.Write", - (, port[RXTX + 3] := $80;). , "RXTX +3 ' #.

, , , .:)

# pascal, , . , , .

            public void Tx(int data)
            {
                if (!port.IsOpen)
                    port.Open();
                port.Write(new byte[] { (byte)(data & 0xFF) }, 0, 1);
                port.Close();
            }
            /// <summary>
            /// Wait for a character from serial channel
            /// </summary>
            /// <returns></returns>
            public int RxWait()
            {
                if (!port.IsOpen)
                    port.Open();
                int readByte = port.ReadByte();
                port.Close();
                return readByte;
            }
            /// <summary>
            /// Transmit a char on serial channel + Calculate check sum
            /// </summary>
            /// <param name="data"></param>
            public void Tx2(int data)
            {
                Tx(data);
                checkSum = (checkSum + data) & 0xFF;
            }

, , .

Computer SC 350/360
–––––––> ESC (message start)
–––––––> Command
<––––––> Data (direction depends on command)
<––––––> Check sum (direction depends on command)
<––––––– Receipt:
- ACK (if check sum is correct) or
- NAK (if check sum is incorrect)

, .

 /// <summary>
    /// Transmit command (no data) on serial channel
    /// </summary>
        /// <param name="c1"></param>
        /// <param name="c2"></param>
        /// <param name="sendCheckSum"></param>
        public void TxCommand(char c1, char c2, bool sendCheckSum)
        {
            Tx(ESC);
            checkSum = 0;
            Tx2((int)c1);
            Tx2((int)c2);
            if (sendCheckSum)
            {
                Tx2(checkSum);
                dummy = RxWait();
            }
        }
        /// <summary>
        /// Read n bytes from serial channel
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public double ReadNumber(int n)
        {
            double number;
            int i;
            number = checkSum = 0;
            for (i = 0; i < n; i++)
                number = number * 256 + RxWait();
            dummy = RxWait();
            return number;
        }

        /// <summary>
        /// Read the number of Coins counted
        /// </summary>
        /// <returns>Number of Coins</returns>
        public double ReadCountReg()
        {
            TxCommand('R', 'C', false);
            double coinsCounted = ReadNumber(4);
            dummy = RxWait();
            return coinsCounted;
        }

,

double coinsCounted = ReadCountReg();
 Console.WriteLine(Math.Round(coinsCounted, 0) + " coins counted");

:

Computer SC 350/360
–––––––> ESC
–––––––> "R"
–––––––> "C"
<––––––– CountRegister (CR)
<––––––– (CR ^ FF00,000016 + CR ^ FF,000016 + CR ^ FF0016 +
CR ^ FF16) ^ FF16
<––––––– ACK
+4
2

, , - ( , COM1). , , + 5 8250 UART , 16450 16550. . .

, - Windows (, , , , , USB), DOS-, , , (DOS - ) , . , Windows ( .Net framework). , , ( Write ). .

.Net API SerialPort, API "" DOS.

+4

. 8250 UART . UART Windows . SerialPort . , , COM1, . USB- . SerialPort.GetPortNames(), .

Tx() . SerialPort.Write() , , . .

RxWait() . SerialPort.ReadByte().

Tx2() - , , .

+6

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


All Articles