Serial Port Coding Basics w/ Four Parts

Part 1:
How do you initialize Com2 ?

SerialPort xSerialPort = new SerialPort("Com2",9600,0,8,0,0)

is this correct?

Part 2:
How do you use it?

xserialport.open()

then

Part 3:
How do you write to it?
I need to send this command:

// 02 01 80 00 03 80   which i believe is = to  { 0x02, 0x01, 0x50, 0x00, 0x03, 0x50 }

i declared it using this:

public static readonly byte[] Dispense1Card = new byte[] { 0x02, 0x01, 0x50, 0x00, 0x03, 0x50 };
xserialport.write(Dispense1Card)

is this correct?

Finally the big question:
Electrical Engineer set up board with four com ports Com 1 and Com 2 then Com 3 is shared between two devices. with PA22 decided between the two
So in normal operation PA22 is set

switchSerial.write(false)

but when we need to dispense card i need do this

switchSerial.write(true)
xserialport.write(Dispense1Card)
switchSerial.write(false)

Do I need to do anything else like setup the com3 after switching?
Thanks in advance for you help.

See this: http://wiki.tinyclr.com/index.php?title=UART_-_PC_Communication

Ok read that and all associated articles so I think that Part 1, 2 are correct, however changed part two
to:

Dispense1Card = Encoding.UTF8.GetBytes("02 01 80 00 03 80 ")

Still on Part Four when I switch the pin high do I need to reopen the serial port?

no you do not need to do anything special but only switch when no data is being transmitted.

still having problems with the serial port.
device api says that device ie card dispenser needs following packet:
Command Packet: <01><80h><00h><00> (02 01 80 00 00 03 80)
in C++ sample code they defined this as:

    //Default global declarations
    API.STX = 0x02;
    API.AddressHex = 0x01;
    API.Code = 0x30;
    API.Length = 0x00;
    API.Data[0] = 0x00;
    API.ETX = 0x03;
    API.CheckSum = 0x00;

and used it as follows:

SendBuffer[0] = API.STX;
    SendBuffer[1] = API.AddressHex;
    SendBuffer[2] = SendCode = 0x80;
    SendBuffer[3] = 0x00;
    SendBuffer[4] = API.ETX;
    SendBuffer[5] = XORChecksum ( SendBuffer, 5);
    SendBuffer[6] = NULL;

    APIHexToASCIIBufferView (6);
    DataSentBuffer->Lines->Clear();
    DataReceivedBuffer->Lines->Clear();
    DataSentBuffer->Lines->Add(APIASCIIView);

    if ( CommON1->Checked == false ) //comm port is disabled
        CommStatus->Caption = "Packet is not sent.";
    else {
          nrComm1->ClearDeviceBuffers();
          LEDStatus1->LightColor = lcMarine;
          API.Length = RecIndex = RecIndexPos = 0; //reset index pointers
          nrComm1->SendData(SendBuffer, 6);
          CommStatus->Caption = "Packet is sent.";
          APIStatus->Caption = "80h Dispense - Sent";
          Status1->Caption = "";
          Sleep(5);
      }        

So what is wrong with my code here that is now the same in C#:

 
        public void Send(byte[] bytes)
        {
            try
            {
                if (!IsOpen)
                    Open();

                FlushBuffers();
                serialPort.Write(bytes, 0, bytes.Length);
                serialPort.DiscardInBuffer();
            }
            catch (Exception exception)
blabla

public static readonly byte[] Dispense1Card = new byte[] { 0x02, 0x01, 0x50, 0x00, 0x03, 0x50 };
SerialPortConnection.Send(Dispense1Card);

Really banging head into wall :wall:

Time to bring the analyzer out and see what is happening.

Here is my idea:
take the tx pin of the rs232 port on our board and connect it to the rx pin of a usb to serial converter that i have, then connect the grounds to both then send signal and see what the computer is recieving from the chipworkx/ourboard… Does this seem logical?

you can always (and should always!) use a PC serial port to check what data you’re sending from a Fez. The only challenge is that you potentially need a non-standard application on the PC to read the data, since those control characters won’t really work in a normal terminal program.

Since your board seems highly customised you might have a problem with the following, but I would also suggest considering connecting TX and RX on the port which means when you send data, you actually get it back on the same port.

And I’d always make sure you’re coding a simple test application rather than trying to incorporate this into your main application too early. Get the bugs sorted out separately then integrate the code.

where would I find an example of such non standard program, would that be a console application in VS 2010 coded in C#

yes, write a windows forms or console app for the PC in C#, and have it interpret non-ASCII bytes and display them inside < and > (or whatever you like).
Here’s a quick pointer: [url]Microsoft Learn: Build skills that open doors in your career (may not be relevant, I don’t know enough PC prgramming to know !)

I suggest using TeraTerm on the PC to get the protocol working and then port to the board. It can display the data in Hex.

Connect Tx on your board to RX on the dispenser AND the PC att the same time. You should be able to “sniff” the conversation and see if the data is identical to the hand crafted packet you made in TeraTerm. If you can also run the reference C program and look at the data it might help.

Are you sure the data is going over the wire as ASCII?

Is your baud rate correct?

Are your levels correct? RS232 both sides? Tx to Rx and Rx to Tx? Sometimes documentation is confusing on this.

Are you sending MSB first or LSB first? Make sure of the byte order the dispenser expects.

Thanks for the input: Solution was as follows.
Find incredibly small out of the way (undocumented)jumper on device that changed baud rate from 19,000 to 9600. Then using tera term and terminal in conjunction with a null modem, watch output. Correct syntax since for some reason 0x50 is not 80 on byte buffer 3 but instead 0x80, then watch output again. Strings were overlaying so only final string came out right. set a 95 mil timer to allow each string to send. This because we were erasing the send buffer when we called each send string method. all of our components need custom timing since chipworkx is so fast. then realize that after enable on the device its modem takes 2.56 seconds to come active for receive.

Again hope this helps some one working with a vendapin card dispenser 203 with C# and chipwork.

And MANY Thanks to all who helped finish this project with your sage advice.
Thank you so much. mark