The difference between TCP and UDP

What is the difference between TCP and UDP?

Both TCP and UDP are protocols used for sending bits of data — known as packets — over the Internet. They both build on top of the Internet protocol. In other words, whether you are sending a packet via TCP or UDP, that packet is sent to an IP address. These packets are treated similarly, as they are forwarded from your computer to intermediary routers and on to the destination.

TCP and UDP are not the only protocols that work on top of IP. However, they are the most widely used. The widely used term “TCP/IP” refers to TCP over IP. UDP over IP could just as well be referred to as “UDP/IP”, although this is not a common term.

TCP

TCP stands for Transmission Control Protocol. It is the most commonly used protocol on the Internet.

When you load a web page, your computer sends TCP packets to the web server’s address, asking it to send the web page to you. The web server responds by sending a stream of TCP packets, which your web browser stitches together to form the web page and display it to you. When you click a link, sign in, post a comment, or do anything else, your web browser sends TCP packets to the server and the server sends TCP packets back. TCP is not just one way communication — the remote system sends packets back to acknowledge it is received your packets.

TCP guarantees the recipient will receive the packets in order by numbering them. The recipient sends messages back to the sender saying it received the messages. If the sender does not get a correct response, it will resend the packets to ensure the recipient received them. Packets are also checked for errors. TCP is all about this reliability — packets sent with TCP are tracked so no data is lost or corrupted in transit. This is why file downloads do not become corrupted even if there are network hiccups. Of course, if the recipient is completely offline, your computer will give up and you will see an error message saying it can not communicate with the remote host.

UDP

UDP stands for User Datagram Protocol — a datagram is the same thing as a packet of information. The UDP protocol works similarly to TCP, but it throws all the error-checking stuff out. All the back-and-forth communication and deliverability guarantees slow things down.

When using UDP, packets are just sent to the recipient. The sender will not wait to make sure the recipient received the packet — it will just continue sending the next packets. If you are the recipient and you miss some UDP packets, too bad — you can not ask for those packets again. There is no guarantee you are getting all the packets and there is no way to ask for a packet again if you miss it, but losing all this overhead means the computers can communicate more quickly.

UDP is used when speed is desirable and error correction is not necessary. For example, UDP is frequently used for live broadcasts and online games.

avrdude

Avrdude is a library that allows you to perform functions and upload files to the AVR series of microcontrollers. It is open source software and is freely available to download. With an interface it makes it easy to upload files and interrogate microcontrollers from the command line. It is the software that the Arduino IDE uses in the background.

Example of erasing a chip completely: (Warning this will erase the bootloader too!)

avrdude -p m328p - c usbtiny -e

Will result in output similar to the below, confirming that your chip indeed has been erased.

1
2
3
4
5
6
7
8
9
10
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip

avrdude: safemode: Fuses OK (E:FD, H:DE, L:FF)

avrdude done. Thank you.

Programming fuses with avrdude and USBTiny

Fuse programming on the ATMega328 is very straightforward with the USBTiny. Connect the USBTiny to the AVR with the ICSP (In Circuit Serial Programming) connections. You need to ensure that you have avrdude installed on your computer. Once connected the following commands apply.

avrdude -c usbtiny -p ATmega328p

the above line reads the fuse settings on an ATmega328p

avrdude -p m328p -c usbtiny -U efuse:w:0xff:m -U hfuse:w:0xd9:m -U lfuse:w:0xe2:m

the above line writes the fuse bits on an ATmega328p

The fuse settings for standard 16MHz ext clock are E:FD H:DE L:FF

The fuse settings for standard 8MHz int clock are E:FF H:D9 L:E2

The fuse settings for standard 1MHz int clock are E:FF H:D9 L:62

AVR HVPP (High Voltage Parallel Programmer)

This post is a work in progress. All information contained within is incomplete.

High Voltage Parallel Programmer

Occasionally the fusebits get mis-programmed and render an AVR chip virtually useless. If this happens it can seem like the chip has died and is unrecoverable. Using A HVPP you can reset the chip and teh fue bits and bring back an AVR chip to life. I have successfully performed this operation though have lost my notes. All that I have so far is the connection map. I will aim to update this in future in case anyone needs any help. Ultimately all that is missing is the program used and the basic circuit of allowing 12V to hit the programming pin on the chip. (Hence the name High Voltage.)

Connection Map

// Arduino pins Target pins
// ———— ———————–
12 6, // 14 (PB0) (data bit 0)
13 7, // 15 (PB1) (data bit 1)
14 8, // 16 (PB2) (data bit 2)
15 9, // 17 (PB3) (data bit 3)
16 10, // 18 (PB4) (data bit 4)
17 11, // 19 (PB5) (data bit 5)
18 12, // 23 (PC0) (data bit 6)
19 13 // 24 (PC1) (data bit 7)

23 RDY = A0, // 3 (PD1) (low means busy)
24 OE = A1, // 4 (PD2) (low means output enabled)
25 WR = A2, // 5 (PD3) (low means write)
26 BS1 = A3, // 6 (PD4)
27 XTAL1 = A4, // 9 (XTAL1)
28 XA0 = A5, // 11 (PD5)
4 XA1 = 2, // 12 (PD6)
5 PAGEL = 3, // 13 (PD7)
6 BS2 = 4, // 25 (PC2)
11 VCC = 5, // 7 and 20 (VCC and AVCC)

AVR Introduction

Introduction

The AVR family of chips are a set of microprocessors manufactured by Atmel. They are probably most commonly known by their iplementation on Arduino boards.

Programming methods

There are several ways to program the AVR chips.

Using the Arduino

This is easily done and the most simple way to program your AVR. The chip is inserted on to an Arduino board which is connected to your computer via a usb cable. Using free downloadable Arduino software the chip can have your firmware uploaded easily using the Arduino’s built in FTDI interface. The one downside to this is that a section of the AVR must have a bootloader hosted on it. This takes up space on the chip, but is usually not a problem.

Using an external programmer

There are several external programmer options available. An example is the USBTiny programmer which can be picked up extremely cheaply from the likes of Ebay. This will allow you to program your AVR without the need of an arduino. i.e. You can program the chip directly using minimal components, for example on a breadboard or embedded in a circuit, as long as the programming pins are exposed. This method does not require a bootloader, therefore you have access to the full range of the AVR’s memory.

Using Atmel ICE

This is Atmels grand version of an external programmer. It allows you to do everything you should ever need to do with the AVR. Not only does it allow you to program the chip, but it allows you to set fusebits and also perform onchip debugging. It also fully integrates with Atmel Studio which is free software from Atmel.

Control Huanyang VFD with LinuxCNC

This is a brief post to record some notes for my own reference on the steps I had to follow to be able to control my 2.2kW water cooled spindle with Linux CNC via a Huanyang VFD (Variable Frequency Drive)

I had to set the following menus on the huanyang VFD

PD001: 2 (Accept RS485 commands)
PD002: 2 (Accept freq commands)
PD163: (1 RS485 Slave address :1)
PD164: 1 (RS485 Baud Rate: 9600)
PD165: 3 (8 bit, no parity, 1 stop)

In Stepconf Wizard on LinuxCNC

If necessary set spindle pins to unused i.e. Pin14 and Pin 16

Select spindle speed display to activate the spindle speed display on LinuxCNC

Edit custom.hal

First you will need to find the port that the usb-RS485 dongle is using. In my case it is /dev/ttyUSB0

Then edit the custom.hal script found in the config/setup folder of LinuxCNCand add the following 5 lines. Remember to change your usb port to what is relevant for your setup.

1
2
3
4
5
loadusr -Wn vfd hy_vfd -n vfd -d /dev/ttyUSB0 -p none -r 9600
net spindle-cmd-rpm-abs => vfd.speed-command
net spindle-cw motion.spindle-forward => vfd.spindle-forward
net spindle-ccw motion.spindle-reverse => vfd.spindle-reverse
net on motion.spindle-on => vfd.spindle

Edit custom_postgui.hal

Change line:

sets spindle-at-speed true

to

net spindle-at-speed => vfd.spindle-at-speed

add the following line to the bottom of the file.

setp vfd.enable 1

^